Skip to contentSkip to conversation

nv50: H.264/MPEG2 decoding support via VP2, available on NV84-NV96, NVA0

PATCH Superseded Ilia Mirkin 3 participants 7 messages Jun 27, 2013 – Jul 16, 2013 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nv50: H.264/MPEG2 decoding support via VP2, available on NV84-NV96, NVA0

Adds H.264 and MPEG2 codec support via VP2, using firmware from the blob. Acceleration is supported at the bitstream level for H.264 and IDCT level for MPEG2.

Known issues:
- H.264 interlaced doesn't render properly
- H.264 shows very occasional artifacts on a small fraction of videos
- MPEG2 + VDPAU shows frequent but small artifacts, which aren't there when using XvMC on the same videos

---

I did try to work out the known issues above, but so far to no avail.

The kernel support for these engines is not in mainline yet, but it's likely going to appear in 3.11. I figured that there would be a bunch of feedback so I might as well send it out early.

I played around a lot with XvMC performance, the mb function shows up as #1 in the profiles, with the SSE4.2 optimizations I put in, it drops to #2. Something clever can likely be done to improve VDPAU performance as well, e.g. using SSE to do the inverse quantization operations, but I've left that out. (It gets tricky because a lot of the data is 0's, so it's unclear whether it's faster to use SSE to do operations on everything or one-at-a-time on the non-0's like I have it now.) Even with these, XvMC ends up ~20% faster than plain CPU decoding, and likely that percent improves on older CPUs that can't decode MPEG2 quite as quickly. VDPAU provides further improvements (likely because it is able to skip mb's while XvMC can't), but there are artifacts for reasons unknown.

Note that in order to get XvMC to work, you need my previous patch (or something similar) since otherwise libXvMCnouveau can't be dlopen'd:
http://lists.freedesktop.org/archives/mesa-dev/2013-June/040949.html

If you want to test it out, the kernel patch:
http://lists.freedesktop.org/archives/nouveau/2013-June/012821.html

Firmware:
https://github.com/imirkin/re-vp2/blob/master/extract_firmware.py

 src/gallium/drivers/nv50/Makefile.sources |   5 +-
 src/gallium/drivers/nv50/nv50_context.c   |  13 +-
 src/gallium/drivers/nv50/nv50_context.h   |  24 +
 src/gallium/drivers/nv50/nv50_miptree.c   |  27 ++
 src/gallium/drivers/nv50/nv50_resource.h  |   1 +
 src/gallium/drivers/nv50/nv50_screen.c    |  13 +-
 src/gallium/drivers/nv50/nv50_winsys.h    |   4 +
 src/gallium/drivers/nv50/nv84_video.c     | 778 ++++++++++++++++++++++++++++++
 src/gallium/drivers/nv50/nv84_video.h     | 134 +++++
 src/gallium/drivers/nv50/nv84_video_bsp.c | 251 ++++++++++
 src/gallium/drivers/nv50/nv84_video_vp.c  | 521 ++++++++++++++++++++
 11 files changed, 1768 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/drivers/nv50/nv84_video.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video.h
 create mode 100644 src/gallium/drivers/nv50/nv84_video_bsp.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video_vp.c
src/gallium/drivers/nv50/Makefile.sources +4 -1
index 1092570..0fdac51 100644
@@ -13,7 +13,10 @@ C_SOURCES := \
 	nv50_program.c \
 	nv50_shader_state.c \
 	nv50_push.c \
-	nv50_query.c
+	nv50_query.c \
+	nv84_video.c \
+	nv84_video_bsp.c \
+	nv84_video_vp.c
 
 CODEGEN_NV50_SOURCES := \
 	codegen/nv50_ir.cpp \
src/gallium/drivers/nv50/nv50_context.c +12 -1
index 5781c4b..79a0473 100644
@@ -258,7 +258,18 @@ nv50_create(struct pipe_screen *pscreen, void *priv)
    draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
 #endif
 
-   nouveau_context_init_vdec(&nv50->base);
+   if (screen->base.device->chipset < 0x84) {
+      /* PMPEG */
+      nouveau_context_init_vdec(&nv50->base);
+   } else if (screen->base.device->chipset < 0x98 ||
+              screen->base.device->chipset == 0xa0) {
+      /* VP2 */
+      pipe->create_video_decoder = nv84_create_decoder;
+      pipe->create_video_buffer = nv84_video_buffer_create;
+   } else {
+      /* Unsupported, but need to init pointers. */
+      nouveau_context_init_vdec(&nv50->base);
+   }
 
    flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
 
src/gallium/drivers/nv50/nv50_context.h +24 -0
index 0a83131..b204cc8 100644
@@ -289,4 +289,28 @@ void nv50_vertex_arrays_validate(struct nv50_context *nv50);
 /* nv50_push.c */
 void nv50_push_vbo(struct nv50_context *, const struct pipe_draw_info *);
 
+/* nv84_video.c */
+struct pipe_video_decoder *
+nv84_create_decoder(struct pipe_context *context,
+                    enum pipe_video_profile profile,
+                    enum pipe_video_entrypoint entrypoint,
+                    enum pipe_video_chroma_format chroma_format,
+                    unsigned width, unsigned height,
+                    unsigned max_references,
+                    bool expect_chunked_decode);
+
+struct pipe_video_buffer *
+nv84_video_buffer_create(struct pipe_context *pipe,
+                         const struct pipe_video_buffer *template);
+
+int
+nv84_screen_get_video_param(struct pipe_screen *pscreen,
+                            enum pipe_video_profile profile,
+                            enum pipe_video_cap param);
+
+boolean
+nv84_screen_video_supported(struct pipe_screen *screen,
+                            enum pipe_format format,
+                            enum pipe_video_profile profile);
+
 #endif
src/gallium/drivers/nv50/nv50_miptree.c +27 -0
index 036f1c7..28be768 100644
@@ -239,6 +239,28 @@ nv50_miptree_init_layout_linear(struct nv50_miptree *mt, unsigned pitch_align)
 }
 
 static void
+nv50_miptree_init_layout_video(struct nv50_miptree *mt)
+{
+   const struct pipe_resource *pt = &mt->base.base;
+   const unsigned blocksize = util_format_get_blocksize(pt->format);
+
+   assert(pt->last_level == 0);
+   assert(mt->ms_x == 0 && mt->ms_y == 0);
+   assert(!util_format_is_compressed(pt->format));
+
+   mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
+
+   mt->level[0].tile_mode = 0x20;
+   mt->level[0].pitch = align(pt->width0 * blocksize, 64);
+   mt->total_size = align(pt->height0, 16) * mt->level[0].pitch * (mt->layout_3d ? pt->depth0 : 1);
+
+   if (pt->array_size > 1) {
+      mt->layer_stride = align(mt->total_size, NV50_TILE_SIZE(0x20));
+      mt->total_size = mt->layer_stride * pt->array_size;
+   }
+}
+
+static void
 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
 {
    struct pipe_resource *pt = &mt->base.base;
@@ -311,6 +333,11 @@ nv50_miptree_create(struct pipe_screen *pscreen,
       return NULL;
    }
 
+   if (unlikely(pt->flags & NV50_RESOURCE_FLAG_VIDEO)) {
+      nv50_miptree_init_layout_video(mt);
+      /* BO allocation done by client */
+      return pt;
+   } else
    if (bo_config.nv50.memtype != 0) {
       nv50_miptree_init_layout_tiled(mt);
    } else
src/gallium/drivers/nv50/nv50_resource.h +1 -0
index 6b92463..c520a72 100644
@@ -16,6 +16,7 @@ nv50_init_resource_functions(struct pipe_context *pcontext);
 void
 nv50_screen_init_resource_functions(struct pipe_screen *pscreen);
 
+#define NV50_RESOURCE_FLAG_VIDEO (NOUVEAU_RESOURCE_FLAG_DRV_PRIV << 0)
 
 #define NV50_TILE_SHIFT_X(m) 6
 #define NV50_TILE_SHIFT_Y(m) ((((m) >> 4) & 0xf) + 2)
src/gallium/drivers/nv50/nv50_screen.c +12 -1
index b6da303..86bad6b 100644
@@ -646,7 +646,18 @@ nv50_screen_create(struct nouveau_device *dev)
 
    nv50_screen_init_resource_functions(pscreen);
 
-   nouveau_screen_init_vdec(&screen->base);
+   if (screen->base.device->chipset < 0x84) {
+      /* PMPEG */
+      nouveau_screen_init_vdec(&screen->base);
+   } else if (screen->base.device->chipset < 0x98 ||
+              screen->base.device->chipset == 0xa0) {
+      /* VP2 */
+      screen->base.base.get_video_param = nv84_screen_get_video_param;
+      screen->base.base.is_video_format_supported = nv84_screen_video_supported;
+   } else {
+      /* Unsupported, but need to init pointers. */
+      nouveau_screen_init_vdec(&screen->base);
+   }
 
    ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0, 4096,
                         NULL, &screen->fence.bo);
src/gallium/drivers/nv50/nv50_winsys.h +4 -0
index 145ee70..e04247b 100644
@@ -60,6 +60,10 @@ PUSH_REFN(struct nouveau_pushbuf *push, struct nouveau_bo *bo, uint32_t flags)
 #define SUBC_COMPUTE(m) 6, (m)
 #define NV50_COMPUTE(n) SUBC_COMPUTE(NV50_COMPUTE_##n)
 
+/* These are expected to be on their own pushbufs */
+#define SUBC_BSP(m) 2, (m)
+#define SUBC_VP(m) 2, (m)
+
 
 static INLINE uint32_t
 NV50_FIFO_PKHDR(int subc, int mthd, unsigned size)
src/gallium/drivers/nv50/nv84_video.c new file +778 -0

778 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video.h new file +134 -0

134 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video_bsp.c new file +251 -0

251 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video_vp.c new file +521 -0

521 lines not on this page · raw message

Signature · 1 line
1.8.1.5
#1 lore raw

Hi Ilia,

Quoted 11 lines · Ilia Mirkin

Adds H.264 and MPEG2 codec support via VP2, using firmware from the blob. Acceleration is supported at the bitstream level for H.264 and IDCT level for MPEG2.

Known issues:
- H.264 interlaced doesn't render properly
- H.264 shows very occasional artifacts on a small fraction of videos
- MPEG2 + VDPAU shows frequent but small artifacts, which aren't there when using XvMC on the same videos

Big thanks for working on this

I believe the hardware is capable of accelerating IDCT for VC1. Do you have any plans for it ?

As far as I know mesa in general is keen on keeping trailing statements on the next line, as well as 78(80) characters line length.

Quoted 4 lines

---

I did try to work out the known issues above, but so far to no avail.

Quoted 8 lines

The kernel support for these engines is not in mainline yet, but it's likely going to appear in 3.11. I figured that there would be a bunch of feedback so I might as well send it out early.

I played around a lot with XvMC performance, the mb function shows up as #1 in the profiles, with the SSE4.2 optimizations I put in, it drops to #2. Something clever can likely be done to improve VDPAU performance as well, e.g. using SSE to do the inverse quantization operations, but I've left that

Curious how many machines with vp2 card have a sse4.2 capable CPU? Mine is only sse4.1 ;(

Quoted 35 lines

out. (It gets tricky because a lot of the data is 0's, so it's unclear whether it's faster to use SSE to do operations on everything or one-at-a-time on the non-0's like I have it now.) Even with these, XvMC ends up ~20% faster than plain CPU decoding, and likely that percent improves on older CPUs that can't decode MPEG2 quite as quickly. VDPAU provides further improvements (likely because it is able to skip mb's while XvMC can't), but there are artifacts for reasons unknown.

Note that in order to get XvMC to work, you need my previous patch (or something similar) since otherwise libXvMCnouveau can't be dlopen'd:
http://lists.freedesktop.org/archives/mesa-dev/2013-June/040949.html

If you want to test it out, the kernel patch:
http://lists.freedesktop.org/archives/nouveau/2013-June/012821.html

Firmware:
https://github.com/imirkin/re-vp2/blob/master/extract_firmware.py

 src/gallium/drivers/nv50/Makefile.sources |   5 +-
 src/gallium/drivers/nv50/nv50_context.c   |  13 +-
 src/gallium/drivers/nv50/nv50_context.h   |  24 +
 src/gallium/drivers/nv50/nv50_miptree.c   |  27 ++
 src/gallium/drivers/nv50/nv50_resource.h  |   1 +
 src/gallium/drivers/nv50/nv50_screen.c    |  13 +-
 src/gallium/drivers/nv50/nv50_winsys.h    |   4 +
 src/gallium/drivers/nv50/nv84_video.c     | 778 ++++++++++++++++++++++++++++++
 src/gallium/drivers/nv50/nv84_video.h     | 134 +++++
 src/gallium/drivers/nv50/nv84_video_bsp.c | 251 ++++++++++
 src/gallium/drivers/nv50/nv84_video_vp.c  | 521 ++++++++++++++++++++
 11 files changed, 1768 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/drivers/nv50/nv84_video.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video.h
 create mode 100644 src/gallium/drivers/nv50/nv84_video_bsp.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video_vp.c

...

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +0 -0
new file mode 100644
index 0000000..064178c
@@ -0,0 +1,778 @@

...

src/gallium/drivers/nv50/nv84_video.c line 35

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+static int
+nv84_copy_firmware(const char *path, void *dest, size_t len)

ssize_t len
To prevent signed/unsigned issues in conditional below

src/gallium/drivers/nv50/nv84_video.c line 46

Quoted 11 lines
src/gallium/drivers/nv50/nv84_video.c new file +11 -0
+{
+   int fd = open(path, O_RDONLY | O_CLOEXEC);
+   ssize_t r;
+   if (fd < 0) {
+      fprintf(stderr, "opening firmware file %s failed: %m\n", path);
+      return 1;
+   }
+   r = read(fd, dest, len);
+   close(fd);
+
+   if (r != len) {

Here ^^

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+      fprintf(stderr, "reading firwmare file %s failed: %m\n", path);
+      return 1;
+   }
+
+   return 0;
+}
+

...

Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+static void
+nv84_decoder_decode_bitstream_mpeg12(struct pipe_video_decoder *decoder,
+                                     struct pipe_video_buffer *video_target,
+                                     struct pipe_picture_desc *picture,

src/gallium/drivers/nv50/nv84_video.c line 171

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+                                     unsigned num_buffers,
+                                     const void *const *data,
+                                     const unsigned *num_bytes)
+{
+   struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
+   struct nv84_video_buffer *target = (struct nv84_video_buffer *)video_target;
+
+   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
+
+   assert(target->base.buffer_format == PIPE_FORMAT_NV12);

This can be written as
assert(video_target->buffer_format == PIPE_FORMAT_NV12);

src/gallium/drivers/nv50/nv84_video.c line 178

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+
+   vl_mpg12_bs_decode(dec->mpeg12_bs,
+                      video_target,
+                      desc,
+                      num_buffers,
+                      data,
+                      num_bytes);

And then the temporary variables can be removed, as you've done in nv84_decoder_end_frame_mpeg12()

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+}
+

...

Quoted 68 lines
src/gallium/drivers/nv50/nv84_video.c new file +68 -0
+
+struct pipe_video_decoder *
+nv84_create_decoder(struct pipe_context *context,
+                    enum pipe_video_profile profile,
+                    enum pipe_video_entrypoint entrypoint,
+                    enum pipe_video_chroma_format chroma_format,
+                    unsigned width, unsigned height,
+                    unsigned max_references,
+                    bool chunked_decode)
+{
+   struct nv50_context *nv50 = (struct nv50_context *)context;
+   struct nouveau_screen *screen = &nv50->screen->base;
+   struct nv84_decoder *dec;
+   struct nouveau_pushbuf *bsp_push, *vp_push;
+   struct nv50_surface surf;
+   struct nv50_miptree mip;
+   union pipe_color_union color;
+   struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
+   int ret, i;
+   int is_h264 = u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG4_AVC;
+   int is_mpeg12 = u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12;
+   struct nouveau_pushbuf_refn fence_ref[] = {
+      { NULL, NOUVEAU_BO_RDWR | NOUVEAU_BO_VRAM },
+   };
+
+
+   if (getenv("XVMC_VL"))
+      return vl_create_decoder(context, profile, entrypoint,
+                               chroma_format, width, height,
+                               max_references, chunked_decode);
+
+   if ((is_h264 && entrypoint != PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
+       (is_mpeg12 && entrypoint > PIPE_VIDEO_ENTRYPOINT_IDCT)) {
+      debug_printf("%x\n", entrypoint);
+      return NULL;
+   }
+
+   if (!is_h264 && !is_mpeg12) {
+      debug_printf("invalid profile: %x\n", profile);
+      return NULL;
+   }
+
+   dec = CALLOC_STRUCT(nv84_decoder);
+   if (!dec) return NULL;
+
+   dec->base.context = context;
+   dec->base.profile = profile;
+   dec->base.entrypoint = entrypoint;
+   dec->base.chroma_format = chroma_format;
+   dec->base.width = width;
+   dec->base.height = height;
+   dec->base.max_references = max_references;
+   dec->base.destroy = nv84_decoder_destroy;
+   dec->base.flush = nv84_decoder_flush;
+   if (is_h264) {
+      dec->base.decode_bitstream = nv84_decoder_decode_bitstream_h264;
+      dec->base.begin_frame = nv84_decoder_begin_frame_h264;
+      dec->base.end_frame = nv84_decoder_end_frame_h264;
+
+      dec->frame_mbs = mb(dec->base.width) * mb_half(dec->base.height) * 2;
+      dec->frame_size = dec->frame_mbs << 8;
+      dec->vpring_deblock = align(0x30 * dec->frame_mbs, 0x100);
+      dec->vpring_residual = 0x2000 + MAX2(0x32000, 0x600 * dec->frame_mbs);
+      dec->vpring_ctrl = MAX2(0x10000, align(0x1080 + 0x144 * dec->frame_mbs, 0x100));
+   } else if (is_mpeg12) {
+      dec->base.decode_macroblock = nv84_decoder_decode_macroblock;
+      dec->base.begin_frame = nv84_decoder_begin_frame_mpeg12;
+      dec->base.end_frame = nv84_decoder_end_frame_mpeg12;

src/gallium/drivers/nv50/nv84_video.c line 340

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+
+      if (entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
+         dec->mpeg12_bs = CALLOC_STRUCT(vl_mpg12_bs);
+         if (!dec->mpeg12_bs)
+            goto fail;
+         vl_mpg12_bs_init(dec->mpeg12_bs, &dec->base);
+         dec->base.decode_bitstream = nv84_decoder_decode_bitstream_mpeg12;
+      }
+   } else {
+      goto fail;

Seems to be handled already by - if (!is_h264 && !is_mpeg12)...

src/gallium/drivers/nv50/nv84_video.c line 345

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video.c new file +5 -0
+   }
+
+   ret = nouveau_client_new(screen->device, &dec->client);
+   if (ret)
+      goto fail;

Is there any particular reason for using a variable to store the return value through this functions? Me thinks it can be safely purged, making the code a bit cleaner

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video.c new file +5 -0
+
+   if (is_h264) {
+      ret = nouveau_object_new(&screen->device->object, 0,
+                               NOUVEAU_FIFO_CHANNEL_CLASS,
+                               &nv04_data, sizeof(nv04_data), &dec->bsp_channel);

...

Quoted 36 lines
src/gallium/drivers/nv50/nv84_video.c new file +36 -0
+   if (is_h264) {
+      /* Zero out some parts of mbring/vpring. there's gotta be some cleaner way
+       * of doing this... perhaps makes sense to just copy the relevant logic
+       * here. */
+      color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
+      surf.offset = dec->frame_size;
+      surf.width = 64;
+      surf.height = (max_references + 1) * dec->frame_mbs / 4;
+      surf.depth = 1;
+      surf.base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      surf.base.u.tex.level = 0;
+      surf.base.texture = &mip.base.base;
+      mip.level[0].tile_mode = 0;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.domain = NOUVEAU_BO_VRAM;
+      mip.base.bo = dec->mbring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 64, 4760);
+      surf.offset = dec->vpring->size / 2 - 0x1000;
+      surf.width = 1024;
+      surf.height = 1;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.bo = dec->vpring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);
+      surf.offset = dec->vpring->size - 0x1000;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);
+
+      PUSH_SPACE(screen->pushbuf, 5);
+      fence_ref[0].bo = dec->fence;
+      nouveau_pushbuf_refn(screen->pushbuf, fence_ref, 1);
+      /* The clear_render_target is done via 3D engine, so use it to write to a
+       * sempahore to indicate that it's done.
+       */
+      BEGIN_NV04(screen->pushbuf, SUBC_3D(0x1b00), 4);
+      PUSH_DATAh(screen->pushbuf, dec->fence->offset);
+      PUSH_DATA (screen->pushbuf, dec->fence->offset);
+      PUSH_DATA (screen->pushbuf, 1);

src/gallium/drivers/nv50/nv84_video.c line 521

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+      PUSH_DATA (screen->pushbuf, 0xf010);
+      PUSH_KICK (screen->pushbuf);
+
+      PUSH_SPACE(bsp_push, 2 + 12 + 2 + 4 + 3);
+
+      BEGIN_NV04(bsp_push, SUBC_BSP(NV01_SUBCHAN_OBJECT), 1);
+      PUSH_DATA (bsp_push, dec->bsp->handle);
+
+      BEGIN_NV04(bsp_push, SUBC_BSP(0x180), 11);
+      for (i = 0; i < 11; i++)

Any idea where 11 comes from ? Is it related to some other parameter ?

Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+         PUSH_DATA(bsp_push, nv04_data.vram);
+      BEGIN_NV04(bsp_push, SUBC_BSP(0x1b8), 1);
+      PUSH_DATA (bsp_push, nv04_data.vram);
+

...

Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+struct pipe_video_buffer *
+nv84_video_buffer_create(struct pipe_context *pipe,
+                         const struct pipe_video_buffer *template)
+{

...

src/gallium/drivers/nv50/nv84_video.c line 653

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   buffer->base.buffer_format = template->buffer_format;
+   buffer->base.context = pipe;
+   buffer->base.destroy = nv84_video_buffer_destroy;
+   buffer->base.chroma_format = template->chroma_format;
+   buffer->base.width = template->width;
+   buffer->base.height = template->height;
+   buffer->base.get_sampler_view_planes = nv84_video_buffer_sampler_view_planes;
+   buffer->base.get_sampler_view_components = nv84_video_buffer_sampler_view_components;
+   buffer->base.get_surfaces = nv84_video_buffer_surfaces;
+   buffer->base.interlaced = true;

By storing the number of planes, will be able to demagic some constants later on
buffer->num_planes = 2;

Quoted 14 lines
src/gallium/drivers/nv50/nv84_video.c new file +14 -0
+
+   memset(&templ, 0, sizeof(templ));
+   templ.target = PIPE_TEXTURE_2D_ARRAY;
+   templ.depth0 = 1;
+   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
+   templ.format = PIPE_FORMAT_R8_UNORM;
+   templ.width0 = align(template->width, 2);
+   templ.height0 = align(template->height, 4) / 2;
+   templ.flags = NV50_RESOURCE_FLAG_VIDEO;
+   templ.array_size = 2;
+
+   cfg.nv50.tile_mode = 0x20;
+   cfg.nv50.memtype = 0x70;
+

src/gallium/drivers/nv50/nv84_video.c line 677

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
+   if (!buffer->resources[0])
+      goto error;
+
+   templ.format = PIPE_FORMAT_R8G8_UNORM;
+   templ.width0 /= 2;
+   templ.height0 /= 2;
+   buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
+   if (!buffer->resources[1])
+      goto error;

I believe that the nvc0 version of the code is easier to read (bikeshed)

for (i = 1; i < buffer->num_planes; ++i) {
buffer->resources[i] = pipe->screen->resource_create(pipe->screen,
&templ);
if (!buffer->resources[i])

      goto error;
}
Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +8 -0
+
+   mt0 = nv50_miptree(buffer->resources[0]);
+   mt1 = nv50_miptree(buffer->resources[1]);
+
+   bo_size = mt0->total_size + mt1->total_size;
+   if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
+                      bo_size, &cfg, &buffer->interlaced))
+      goto error;

src/gallium/drivers/nv50/nv84_video.c line 695

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   /* XXX Change reference frame management so that this is only allocated in
+    * the decoder when necessary. */
+   if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
+                      bo_size, &cfg, &buffer->full))
+      goto error;
+
+   mt0->base.bo = buffer->interlaced;
+   mt0->base.domain = NOUVEAU_BO_VRAM;
+   mt0->base.offset = 0;
+   mt0->base.address = buffer->interlaced->offset;

IMHO this looks a bit easier to grasp
mt0->base.address = buffer->interlaced->offset + mt0->base.offset;

src/gallium/drivers/nv50/nv84_video.c line 701

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+   nouveau_bo_ref(buffer->interlaced, &empty);
+
+   mt1->base.bo = buffer->interlaced;
+   mt1->base.domain = NOUVEAU_BO_VRAM;
+   mt1->base.offset = mt0->layer_stride * 2;
+   mt1->base.address = buffer->interlaced->offset + mt0->layer_stride * 2;

Similar
mt1->base.address = buffer->interlaced->offset + mt1->base.offset;

src/gallium/drivers/nv50/nv84_video.c line 705

Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+   nouveau_bo_ref(buffer->interlaced, &empty);
+
+   memset(&sv_templ, 0, sizeof(sv_templ));
+   for (component = 0, i = 0; i < 2; ++i ) {

for (component = 0, i = 0; i < buffer->num_planes; ++i ) {

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+      struct pipe_resource *res = buffer->resources[i];
+      unsigned nr_components = util_format_get_nr_components(res->format);
+
+      u_sampler_view_default_template(&sv_templ, res, res->format);
+      buffer->sampler_view_planes[i] = pipe->create_sampler_view(pipe, res, &sv_templ);
+      if (!buffer->sampler_view_planes[i])
+         goto error;
+
+      for (j = 0; j < nr_components; ++j, ++component) {
+         sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j;

src/gallium/drivers/nv50/nv84_video.c line 725

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+         sv_templ.swizzle_a = PIPE_SWIZZLE_ONE;
+
+         buffer->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
+         if (!buffer->sampler_view_components[component])
+            goto error;
+      }
+   }
+
+   memset(&surf_templ, 0, sizeof(surf_templ));
+   for (j = 0; j < 2; ++j) {

for (j = 0; j < buffer->num_planes; ++j) {

Quoted 18 lines
src/gallium/drivers/nv50/nv84_video.c new file +18 -0
+      surf_templ.format = buffer->resources[j]->format;
+      surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = 0;
+      buffer->surfaces[j * 2] = pipe->create_surface(pipe, buffer->resources[j], &surf_templ);
+      if (!buffer->surfaces[j * 2])
+         goto error;
+
+      surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = 1;
+      buffer->surfaces[j * 2 + 1] = pipe->create_surface(pipe, buffer->resources[j], &surf_templ);
+      if (!buffer->surfaces[j * 2 + 1])
+         goto error;
+   }
+
+   return &buffer->base;
+
+error:
+   nv84_video_buffer_destroy(&buffer->base);
+   return NULL;
+}

src/gallium/drivers/nv50/nv84_video.c line 753

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+
+int
+nv84_screen_get_video_param(struct pipe_screen *pscreen,
+                            enum pipe_video_profile profile,
+                            enum pipe_video_cap param)
+{
+   switch (param) {
+   case PIPE_VIDEO_CAP_SUPPORTED:
+      return u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG4_AVC ||
+         u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12;
      switch (u_reduce_video_profile(profile)) {
      case PIPE_VIDEO_CODEC_MPEG12:
      case PIPE_VIDEO_CODEC_MPEG4_AVC:
         return true;
      case PIPE_VIDEO_CODEC_VC1:
         /* TODO: Hardware is capable of IDCT acceleration for VC1*/
      case PIPE_VIDEO_CODEC_MPEG4:
      default:
         return false;
      }
Quoted 14 lines
src/gallium/drivers/nv50/nv84_video.c new file +14 -0
+   case PIPE_VIDEO_CAP_NPOT_TEXTURES:
+      return 1;
+   case PIPE_VIDEO_CAP_MAX_WIDTH:
+   case PIPE_VIDEO_CAP_MAX_HEIGHT:
+      return 2048;
+   case PIPE_VIDEO_CAP_PREFERED_FORMAT:
+      return PIPE_FORMAT_NV12;
+   case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED:
+   case PIPE_VIDEO_CAP_PREFERS_INTERLACED:
+      return true;
+   case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
+      return false;
+   default:
+      debug_printf("unknown video param: %d\n", param);

src/gallium/drivers/nv50/nv84_video.c line 777

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+      return 0;
+   }
+}
+
+boolean
+nv84_screen_video_supported(struct pipe_screen *screen,
+                            enum pipe_format format,
+                            enum pipe_video_profile profile)
+{
+   return format == PIPE_FORMAT_NV12;

Will this work when we have XVMC_VL set ?

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +1 -0
+}
src/gallium/drivers/nv50/nv84_video.h new file +0 -0
new file mode 100644
index 0000000..4ff8cf3

...

src/gallium/drivers/nv50/nv84_video.h line 45

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.h new file +2 -0
+struct nv84_video_buffer {
+   struct pipe_video_buffer base;

unsigned num_planes;

src/gallium/drivers/nv50/nv84_video.h line 55

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.h new file +10 -0
+   struct pipe_resource *resources[VL_NUM_COMPONENTS];
+   struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
+   struct pipe_sampler_view *sampler_view_components[VL_NUM_COMPONENTS];
+   struct pipe_surface *surfaces[VL_NUM_COMPONENTS * 2];
+
+   struct nouveau_bo *interlaced, *full;
+   int mvidx;
+   unsigned frame_num, frame_num_max;
+};
+

Looking at the params associated with each video engine, I was wondering about compacting it into a struct (names chosen are the first thing that came to mind)

struct nv84_decoder_eng {
struct nouveau_object *obj;
struct nouveau_object *channel;
struct nouveau_pushbuf *pushbuf;
struct nouveau_bufctx *bufctx;

struct nouveau_bo *fw;
struct nouveau_bo *data;
}

and then having an enum for the different engine types

enum nv84_decoder_eng_type
{
BSP = 0,
VP
};

#define NV84_DECODER_ENG_NUM VP + 1

src/gallium/drivers/nv50/nv84_video.h line 61

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.h new file +6 -0
+struct nv84_decoder {
+   struct pipe_video_decoder base;
+   struct nouveau_client *client;
+   struct nouveau_object *bsp_channel, *vp_channel, *bsp, *vp;
+   struct nouveau_pushbuf *bsp_pushbuf, *vp_pushbuf;
+   struct nouveau_bufctx *bsp_bufctx, *vp_bufctx;

Then the struct will look a bit cleaner struct nv84_decoder {
struct pipe_video_decoder base;
struct nouveau_client *client;
struct nv84_decoder_eng engs[NV84_DECODER_ENG_NUM];

Quoted 39 lines
src/gallium/drivers/nv50/nv84_video.h new file +39 -0
+
+   struct nouveau_bo *bsp_fw, *bsp_data;
+   struct nouveau_bo *vp_fw, *vp_data;
+   struct nouveau_bo *mbring, *vpring;
+
+   /*
+    * states:
+    *  0: init
+    *  1: vpring/mbring cleared, bsp is ready
+    *  2: bsp is done, vp is ready
+    * and then vp it back to 1
+    */
+   struct nouveau_bo *fence;
+
+   struct nouveau_bo *bitstream;
+   struct nouveau_bo *vp_params;
+
+   size_t vp_fw2_offset;
+
+   unsigned frame_mbs, frame_size;
+   /* VPRING layout:
+        RESIDUAL
+        CTRL
+        DEBLOCK
+        0x1000
+   */
+   unsigned vpring_deblock, vpring_residual, vpring_ctrl;
+
+
+   struct vl_mpg12_bs *mpeg12_bs;
+
+   struct nouveau_bo *mpeg12_bo;
+   void *mpeg12_mb_info;
+   uint16_t *mpeg12_data;
+   const int *zscan;
+   uint8_t mpeg12_intra_matrix[64];
+   uint8_t mpeg12_non_intra_matrix[64];
+};
+

...

src/gallium/drivers/nv50/nv84_video.h line 109

Quoted 9 lines
src/gallium/drivers/nv50/nv84_video.h new file +9 -0
+static INLINE uint32_t mb(uint32_t coord)
+{
+   return (coord + 0xf)>>4;
+}
+
+static INLINE uint32_t mb_half(uint32_t coord)
+{
+   return (coord + 0x1f)>>5;
+}

How about moving these in nouveau_video.h ? (and removing the duplicate copy from nvc0_video.h)

Might be better as a follow on patch

...

Quoted 19 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +13 -0
new file mode 100644
index 0000000..60c0848
@@ -0,0 +1,521 @@
+/*
+ * Copyright 2013 Ilia Mirkin
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *

src/gallium/drivers/nv50/nv84_video_vp.c line 23

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +10 -0
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <immintrin.h>

Wrap this include in
#ifdef __SSE4_2__
#endif

considering its definitions are used in a similar block
...

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_h264(struct nv84_decoder *dec,
+                     struct pipe_h264_picture_desc *desc,
+                     struct nv84_video_buffer *dest)
+{

...

Quoted 1 line

+ for (i = 0; i < 2; i++) {

for (i = 0; i < dest->num_planes; i++) {

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +7 -0
+      struct nv50_miptree *mt = nv50_miptree(dest->resources[i]);
+      mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+   }
+
+   PUSH_KICK (push);
+}
+

...

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_mpeg12_mb(struct nv84_decoder *dec,
+                          struct pipe_mpeg12_picture_desc *desc,
+                          const struct pipe_mpeg12_macroblock *macrob)
+{

...

Quoted 1 line

+#ifdef __SSE4_2__

IMHO this may produce non portable binaries in case of aggressive mtune/march flags. I'm not objecting against it just pointing out

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_mpeg12(struct nv84_decoder *dec,
+                       struct pipe_mpeg12_picture_desc *desc,
+                       struct nv84_video_buffer *dest)
+{

...

Quoted 1 line

+ for (i = 0; i < 2; i++) {

for (i = 0; i < dest->num_planes; i++) {

Cheers
Emil

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+      struct nv50_miptree *mt = nv50_miptree(dest->resources[i]);
+      mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+   }
+   PUSH_KICK (push);
+}
#2 lore raw
Quoted 13 lines · Emil Velikov

Hi Ilia,

Quoted 10 lines · Ilia Mirkin

Adds H.264 and MPEG2 codec support via VP2, using firmware from the blob. Acceleration is supported at the bitstream level for H.264 and IDCT level for MPEG2.

Known issues:
- H.264 interlaced doesn't render properly
- H.264 shows very occasional artifacts on a small fraction of videos
- MPEG2 + VDPAU shows frequent but small artifacts, which aren't there when using XvMC on the same videos

Quoted 7 lines
Quoted 1 line

Big thanks for working on this

I believe the hardware is capable of accelerating IDCT for VC1. Do you have any plans for it ?

Nope. With MPEG, there's XvMC, which sends the macroblocks directly. And there's an existing MPEG parser in gallium which can be used for VDPAU. With VC-1, no such thing as XvMC. Which means that I'd have to implement a bitstream parser in gallium. The spec is closed, and I'm not at all familiar with the concepts used in VC-1.

Quoted 3 lines

As far as I know mesa in general is keen on keeping trailing statements on the next line, as well as 78(80) characters line length.

I tried to do that... in the places where I violate that I felt that it enhanced readability. Feel free to point out places where you disagree. Also if it's a hard policy (e.g. as in the linux kernel), I can go through and reformat.

Quoted 9 lines
Quoted 9 lines

---

I did try to work out the known issues above, but so far to no avail.

The kernel support for these engines is not in mainline yet, but it's likely going to appear in 3.11. I figured that there would be a bunch of feedback so I might as well send it out early.

Quoted 6 lines
Quoted 4 lines

I played around a lot with XvMC performance, the mb function shows up as #1 in the profiles, with the SSE4.2 optimizations I put in, it drops to #2. Something clever can likely be done to improve VDPAU performance as well, e.g. using SSE to do the inverse quantization operations, but I've left that

Curious how many machines with vp2 card have a sse4.2 capable CPU? Mine is only sse4.1 ;(

At least one (mine). SSE4.2 appeared on Core i7 (Nehalem), AFAIK. Other optimizations may be possible with just SSE2, but I couldn't think of anything particularly clever. Those PCMPESTRM instructions are _really_ useful to skip over the useless 0's. I guess something could be done with PCMPEQW and PMOVMSKB (wish there were a PMOVMSKW...). I'll play with it. But the plain-C loop isn't so bad either.

Quoted 49 lines
Quoted 36 lines

out. (It gets tricky because a lot of the data is 0's, so it's unclear whether it's faster to use SSE to do operations on everything or one-at-a-time on the non-0's like I have it now.) Even with these, XvMC ends up ~20% faster than plain CPU decoding, and likely that percent improves on older CPUs that can't decode MPEG2 quite as quickly. VDPAU provides further improvements (likely because it is able to skip mb's while XvMC can't), but there are artifacts for reasons unknown.

Note that in order to get XvMC to work, you need my previous patch (or something similar) since otherwise libXvMCnouveau can't be dlopen'd:
http://lists.freedesktop.org/archives/mesa-dev/2013-June/040949.html

If you want to test it out, the kernel patch:
http://lists.freedesktop.org/archives/nouveau/2013-June/012821.html

Firmware:
https://github.com/imirkin/re-vp2/blob/master/extract_firmware.py

 src/gallium/drivers/nv50/Makefile.sources |   5 +-
 src/gallium/drivers/nv50/nv50_context.c   |  13 +-
 src/gallium/drivers/nv50/nv50_context.h   |  24 +
 src/gallium/drivers/nv50/nv50_miptree.c   |  27 ++
 src/gallium/drivers/nv50/nv50_resource.h  |   1 +
 src/gallium/drivers/nv50/nv50_screen.c    |  13 +-
 src/gallium/drivers/nv50/nv50_winsys.h    |   4 +
 src/gallium/drivers/nv50/nv84_video.c     | 778 ++++++++++++++++++++++++++++++
 src/gallium/drivers/nv50/nv84_video.h     | 134 +++++
 src/gallium/drivers/nv50/nv84_video_bsp.c | 251 ++++++++++
 src/gallium/drivers/nv50/nv84_video_vp.c  | 521 ++++++++++++++++++++
 11 files changed, 1768 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/drivers/nv50/nv84_video.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video.h
 create mode 100644 src/gallium/drivers/nv50/nv84_video_bsp.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video_vp.c

...

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +0 -0
new file mode 100644
index 0000000..064178c
@@ -0,0 +1,778 @@

...

src/gallium/drivers/nv50/nv84_video.c line 35

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+static int
+nv84_copy_firmware(const char *path, void *dest, size_t len)

ssize_t len
To prevent signed/unsigned issues in conditional below

Quoted 12 lines

src/gallium/drivers/nv50/nv84_video.c line 46

Quoted 11 lines
src/gallium/drivers/nv50/nv84_video.c new file +11 -0
+{
+   int fd = open(path, O_RDONLY | O_CLOEXEC);
+   ssize_t r;
+   if (fd < 0) {
+      fprintf(stderr, "opening firmware file %s failed: %m\n", path);
+      return 1;
+   }
+   r = read(fd, dest, len);
+   close(fd);
+
+   if (r != len) {

Here ^^

Yes, in case that len is 2^64 - 1... but I'll change it :)

Quoted 9 lines
Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+      fprintf(stderr, "reading firwmare file %s failed: %m\n", path);
+      return 1;
+   }
+
+   return 0;
+}
+

...

Quoted 16 lines
Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+static void
+nv84_decoder_decode_bitstream_mpeg12(struct pipe_video_decoder *decoder,
+                                     struct pipe_video_buffer *video_target,
+                                     struct pipe_picture_desc *picture,

src/gallium/drivers/nv50/nv84_video.c line 171

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+                                     unsigned num_buffers,
+                                     const void *const *data,
+                                     const unsigned *num_bytes)
+{
+   struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
+   struct nv84_video_buffer *target = (struct nv84_video_buffer *)video_target;
+
+   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
+
+   assert(target->base.buffer_format == PIPE_FORMAT_NV12);

This can be written as
assert(video_target->buffer_format == PIPE_FORMAT_NV12);

Good point. The code was a bit different at an earlier point.

Quoted 85 lines

src/gallium/drivers/nv50/nv84_video.c line 178

Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+
+   vl_mpg12_bs_decode(dec->mpeg12_bs,
+                      video_target,
+                      desc,
+                      num_buffers,
+                      data,
+                      num_bytes);

And then the temporary variables can be removed, as you've done in nv84_decoder_end_frame_mpeg12()

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+}
+

...

Quoted 71 lines
src/gallium/drivers/nv50/nv84_video.c new file +71 -0
+
+struct pipe_video_decoder *
+nv84_create_decoder(struct pipe_context *context,
+                    enum pipe_video_profile profile,
+                    enum pipe_video_entrypoint entrypoint,
+                    enum pipe_video_chroma_format chroma_format,
+                    unsigned width, unsigned height,
+                    unsigned max_references,
+                    bool chunked_decode)
+{
+   struct nv50_context *nv50 = (struct nv50_context *)context;
+   struct nouveau_screen *screen = &nv50->screen->base;
+   struct nv84_decoder *dec;
+   struct nouveau_pushbuf *bsp_push, *vp_push;
+   struct nv50_surface surf;
+   struct nv50_miptree mip;
+   union pipe_color_union color;
+   struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
+   int ret, i;
+   int is_h264 = u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG4_AVC;
+   int is_mpeg12 = u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12;
+   struct nouveau_pushbuf_refn fence_ref[] = {
+      { NULL, NOUVEAU_BO_RDWR | NOUVEAU_BO_VRAM },
+   };
+
+
+   if (getenv("XVMC_VL"))
+      return vl_create_decoder(context, profile, entrypoint,
+                               chroma_format, width, height,
+                               max_references, chunked_decode);
+
+   if ((is_h264 && entrypoint != PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
+       (is_mpeg12 && entrypoint > PIPE_VIDEO_ENTRYPOINT_IDCT)) {
+      debug_printf("%x\n", entrypoint);
+      return NULL;
+   }
+
+   if (!is_h264 && !is_mpeg12) {
+      debug_printf("invalid profile: %x\n", profile);
+      return NULL;
+   }
+
+   dec = CALLOC_STRUCT(nv84_decoder);
+   if (!dec) return NULL;
+
+   dec->base.context = context;
+   dec->base.profile = profile;
+   dec->base.entrypoint = entrypoint;
+   dec->base.chroma_format = chroma_format;
+   dec->base.width = width;
+   dec->base.height = height;
+   dec->base.max_references = max_references;
+   dec->base.destroy = nv84_decoder_destroy;
+   dec->base.flush = nv84_decoder_flush;
+   if (is_h264) {
+      dec->base.decode_bitstream = nv84_decoder_decode_bitstream_h264;
+      dec->base.begin_frame = nv84_decoder_begin_frame_h264;
+      dec->base.end_frame = nv84_decoder_end_frame_h264;
+
+      dec->frame_mbs = mb(dec->base.width) * mb_half(dec->base.height) * 2;
+      dec->frame_size = dec->frame_mbs << 8;
+      dec->vpring_deblock = align(0x30 * dec->frame_mbs, 0x100);
+      dec->vpring_residual = 0x2000 + MAX2(0x32000, 0x600 * dec->frame_mbs);
+      dec->vpring_ctrl = MAX2(0x10000, align(0x1080 + 0x144 * dec->frame_mbs, 0x100));
+   } else if (is_mpeg12) {
+      dec->base.decode_macroblock = nv84_decoder_decode_macroblock;
+      dec->base.begin_frame = nv84_decoder_begin_frame_mpeg12;
+      dec->base.end_frame = nv84_decoder_end_frame_mpeg12;
+
+      if (entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
+         dec->mpeg12_bs = CALLOC_STRUCT(vl_mpg12_bs);
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 340

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+         if (!dec->mpeg12_bs)
+            goto fail;
+         vl_mpg12_bs_init(dec->mpeg12_bs, &dec->base);
+         dec->base.decode_bitstream = nv84_decoder_decode_bitstream_mpeg12;
+      }
+   } else {
+      goto fail;

Seems to be handled already by - if (!is_h264 && !is_mpeg12)...

Yes. But I disliked relying on that, so I decided to leave it in.

Quoted 9 lines

src/gallium/drivers/nv50/nv84_video.c line 345

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +5 -0
+   }
+
+   ret = nouveau_client_new(screen->device, &dec->client);
+   if (ret)
+      goto fail;

Is there any particular reason for using a variable to store the return value through this functions? Me thinks it can be safely purged, making the code a bit cleaner

It was like that in nvc0_video and a lot of other code. I think the code looks a little odd when you have

if (super-long multi-line statement)
goto fail

Quoted 47 lines
Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +5 -0
+
+   if (is_h264) {
+      ret = nouveau_object_new(&screen->device->object, 0,
+                               NOUVEAU_FIFO_CHANNEL_CLASS,
+                               &nv04_data, sizeof(nv04_data), &dec->bsp_channel);

...

Quoted 39 lines
src/gallium/drivers/nv50/nv84_video.c new file +39 -0
+   if (is_h264) {
+      /* Zero out some parts of mbring/vpring. there's gotta be some cleaner way
+       * of doing this... perhaps makes sense to just copy the relevant logic
+       * here. */
+      color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
+      surf.offset = dec->frame_size;
+      surf.width = 64;
+      surf.height = (max_references + 1) * dec->frame_mbs / 4;
+      surf.depth = 1;
+      surf.base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      surf.base.u.tex.level = 0;
+      surf.base.texture = &mip.base.base;
+      mip.level[0].tile_mode = 0;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.domain = NOUVEAU_BO_VRAM;
+      mip.base.bo = dec->mbring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 64, 4760);
+      surf.offset = dec->vpring->size / 2 - 0x1000;
+      surf.width = 1024;
+      surf.height = 1;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.bo = dec->vpring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);
+      surf.offset = dec->vpring->size - 0x1000;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);
+
+      PUSH_SPACE(screen->pushbuf, 5);
+      fence_ref[0].bo = dec->fence;
+      nouveau_pushbuf_refn(screen->pushbuf, fence_ref, 1);
+      /* The clear_render_target is done via 3D engine, so use it to write to a
+       * sempahore to indicate that it's done.
+       */
+      BEGIN_NV04(screen->pushbuf, SUBC_3D(0x1b00), 4);
+      PUSH_DATAh(screen->pushbuf, dec->fence->offset);
+      PUSH_DATA (screen->pushbuf, dec->fence->offset);
+      PUSH_DATA (screen->pushbuf, 1);
+      PUSH_DATA (screen->pushbuf, 0xf010);
+      PUSH_KICK (screen->pushbuf);
+
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 521

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+      PUSH_SPACE(bsp_push, 2 + 12 + 2 + 4 + 3);
+
+      BEGIN_NV04(bsp_push, SUBC_BSP(NV01_SUBCHAN_OBJECT), 1);
+      PUSH_DATA (bsp_push, dec->bsp->handle);
+
+      BEGIN_NV04(bsp_push, SUBC_BSP(0x180), 11);
+      for (i = 0; i < 11; i++)

Any idea where 11 comes from ? Is it related to some other parameter ?

These are the dma indices (e.g. when i use 0x654321, each nibble is a dmaidx, which is an offset into these 0x180 values). It's the part of the whole thing that I understand the least... in theory I should be able to just initialize one of them and use it throughout, but I haven't been brave enough to put that theory to the test. This is how the blob does it.

Quoted 22 lines
Quoted 5 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+         PUSH_DATA(bsp_push, nv04_data.vram);
+      BEGIN_NV04(bsp_push, SUBC_BSP(0x1b8), 1);
+      PUSH_DATA (bsp_push, nv04_data.vram);
+

...

Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+struct pipe_video_buffer *
+nv84_video_buffer_create(struct pipe_context *pipe,
+                         const struct pipe_video_buffer *template)
+{

...

src/gallium/drivers/nv50/nv84_video.c line 653

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   buffer->base.buffer_format = template->buffer_format;
+   buffer->base.context = pipe;
+   buffer->base.destroy = nv84_video_buffer_destroy;
+   buffer->base.chroma_format = template->chroma_format;
+   buffer->base.width = template->width;
+   buffer->base.height = template->height;
+   buffer->base.get_sampler_view_planes = nv84_video_buffer_sampler_view_planes;
+   buffer->base.get_sampler_view_components = nv84_video_buffer_sampler_view_components;
+   buffer->base.get_surfaces = nv84_video_buffer_surfaces;
+   buffer->base.interlaced = true;
Quoted 3 lines

By storing the number of planes, will be able to demagic some constants later on
buffer->num_planes = 2;

nvc0_video does that. I find that incredibly confusing. It's always 2, and they are of diff size. I prefer my bike-shed color :)

Quoted 27 lines
Quoted 15 lines
src/gallium/drivers/nv50/nv84_video.c new file +14 -0
+
+   memset(&templ, 0, sizeof(templ));
+   templ.target = PIPE_TEXTURE_2D_ARRAY;
+   templ.depth0 = 1;
+   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
+   templ.format = PIPE_FORMAT_R8_UNORM;
+   templ.width0 = align(template->width, 2);
+   templ.height0 = align(template->height, 4) / 2;
+   templ.flags = NV50_RESOURCE_FLAG_VIDEO;
+   templ.array_size = 2;
+
+   cfg.nv50.tile_mode = 0x20;
+   cfg.nv50.memtype = 0x70;
+

src/gallium/drivers/nv50/nv84_video.c line 677

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
+   if (!buffer->resources[0])
+      goto error;
+
+   templ.format = PIPE_FORMAT_R8G8_UNORM;
+   templ.width0 /= 2;
+   templ.height0 /= 2;
+   buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
+   if (!buffer->resources[1])
+      goto error;

I believe that the nvc0 version of the code is easier to read (bikeshed)

Quoted 1 line

for (i = 1; i < buffer->num_planes; ++i) {

num_planes is always 2. for (i = 1; i < 2; i++) seems like a waste...

Quoted 18 lines
   buffer->resources[i] = pipe->screen->resource_create(pipe->screen,
&templ);
   if (!buffer->resources[i])
      goto error;
}
Quoted 12 lines
src/gallium/drivers/nv50/nv84_video.c new file +12 -0
+
+   mt0 = nv50_miptree(buffer->resources[0]);
+   mt1 = nv50_miptree(buffer->resources[1]);
+
+   bo_size = mt0->total_size + mt1->total_size;
+   if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
+                      bo_size, &cfg, &buffer->interlaced))
+      goto error;
+   /* XXX Change reference frame management so that this is only allocated in
+    * the decoder when necessary. */
+   if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
+                      bo_size, &cfg, &buffer->full))
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 695

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+      goto error;
+
+   mt0->base.bo = buffer->interlaced;
+   mt0->base.domain = NOUVEAU_BO_VRAM;
+   mt0->base.offset = 0;
+   mt0->base.address = buffer->interlaced->offset;

IMHO this looks a bit easier to grasp
mt0->base.address = buffer->interlaced->offset + mt0->base.offset;

Agreed.

Quoted 9 lines

src/gallium/drivers/nv50/nv84_video.c line 701

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+   nouveau_bo_ref(buffer->interlaced, &empty);
+
+   mt1->base.bo = buffer->interlaced;
+   mt1->base.domain = NOUVEAU_BO_VRAM;
+   mt1->base.offset = mt0->layer_stride * 2;
+   mt1->base.address = buffer->interlaced->offset + mt0->layer_stride * 2;

Similar
mt1->base.address = buffer->interlaced->offset + mt1->base.offset;

Yep.

Quoted 59 lines

src/gallium/drivers/nv50/nv84_video.c line 705

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+   nouveau_bo_ref(buffer->interlaced, &empty);
+
+   memset(&sv_templ, 0, sizeof(sv_templ));
+   for (component = 0, i = 0; i < 2; ++i ) {

for (component = 0, i = 0; i < buffer->num_planes; ++i ) {

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+      struct pipe_resource *res = buffer->resources[i];
+      unsigned nr_components = util_format_get_nr_components(res->format);
+
+      u_sampler_view_default_template(&sv_templ, res, res->format);
+      buffer->sampler_view_planes[i] = pipe->create_sampler_view(pipe, res, &sv_templ);
+      if (!buffer->sampler_view_planes[i])
+         goto error;
+
+      for (j = 0; j < nr_components; ++j, ++component) {
+         sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j;

src/gallium/drivers/nv50/nv84_video.c line 725

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+         sv_templ.swizzle_a = PIPE_SWIZZLE_ONE;
+
+         buffer->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
+         if (!buffer->sampler_view_components[component])
+            goto error;
+      }
+   }
+
+   memset(&surf_templ, 0, sizeof(surf_templ));
+   for (j = 0; j < 2; ++j) {

for (j = 0; j < buffer->num_planes; ++j) {

Quoted 18 lines
src/gallium/drivers/nv50/nv84_video.c new file +18 -0
+      surf_templ.format = buffer->resources[j]->format;
+      surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = 0;
+      buffer->surfaces[j * 2] = pipe->create_surface(pipe, buffer->resources[j], &surf_templ);
+      if (!buffer->surfaces[j * 2])
+         goto error;
+
+      surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = 1;
+      buffer->surfaces[j * 2 + 1] = pipe->create_surface(pipe, buffer->resources[j], &surf_templ);
+      if (!buffer->surfaces[j * 2 + 1])
+         goto error;
+   }
+
+   return &buffer->base;
+
+error:
+   nv84_video_buffer_destroy(&buffer->base);
+   return NULL;
+}

src/gallium/drivers/nv50/nv84_video.c line 753

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+
+int
+nv84_screen_get_video_param(struct pipe_screen *pscreen,
+                            enum pipe_video_profile profile,
+                            enum pipe_video_cap param)
+{
+   switch (param) {
+   case PIPE_VIDEO_CAP_SUPPORTED:
+      return u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG4_AVC ||
+         u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12;
      switch (u_reduce_video_profile(profile)) {
      case PIPE_VIDEO_CODEC_MPEG12:
Quoted 8 lines
      case PIPE_VIDEO_CODEC_MPEG4_AVC:
         return true;
      case PIPE_VIDEO_CODEC_VC1:
         /* TODO: Hardware is capable of IDCT acceleration for VC1*/
      case PIPE_VIDEO_CODEC_MPEG4:
      default:
         return false;
      }

Why? switch inside a switch is a bit ugly.

Quoted 18 lines
Quoted 18 lines
src/gallium/drivers/nv50/nv84_video.c new file +17 -0
+   case PIPE_VIDEO_CAP_NPOT_TEXTURES:
+      return 1;
+   case PIPE_VIDEO_CAP_MAX_WIDTH:
+   case PIPE_VIDEO_CAP_MAX_HEIGHT:
+      return 2048;
+   case PIPE_VIDEO_CAP_PREFERED_FORMAT:
+      return PIPE_FORMAT_NV12;
+   case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED:
+   case PIPE_VIDEO_CAP_PREFERS_INTERLACED:
+      return true;
+   case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
+      return false;
+   default:
+      debug_printf("unknown video param: %d\n", param);
+      return 0;
+   }
+}
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 777

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+
+boolean
+nv84_screen_video_supported(struct pipe_screen *screen,
+                            enum pipe_format format,
+                            enum pipe_video_profile profile)
+{
+   return format == PIPE_FORMAT_NV12;

Will this work when we have XVMC_VL set ?

Hm, well, XVMC_VL totally doesn't work for me... crashes in the shader somewhere. I just copied that logic from nvc0... If anything this would be too restrictive...

Quoted 39 lines
Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +1 -0
+}
src/gallium/drivers/nv50/nv84_video.h new file +0 -0
new file mode 100644
index 0000000..4ff8cf3

...

src/gallium/drivers/nv50/nv84_video.h line 45

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.h new file +2 -0
+struct nv84_video_buffer {
+   struct pipe_video_buffer base;

unsigned num_planes;

src/gallium/drivers/nv50/nv84_video.h line 55

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.h new file +10 -0
+   struct pipe_resource *resources[VL_NUM_COMPONENTS];
+   struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
+   struct pipe_sampler_view *sampler_view_components[VL_NUM_COMPONENTS];
+   struct pipe_surface *surfaces[VL_NUM_COMPONENTS * 2];
+
+   struct nouveau_bo *interlaced, *full;
+   int mvidx;
+   unsigned frame_num, frame_num_max;
+};
+

Looking at the params associated with each video engine, I was wondering about compacting it into a struct (names chosen are the first thing that came to mind)

struct nv84_decoder_eng {
   struct nouveau_object *obj;
   struct nouveau_object *channel;
   struct nouveau_pushbuf *pushbuf;
   struct nouveau_bufctx *bufctx;

   struct nouveau_bo *fw;
   struct nouveau_bo *data;
}

and then having an enum for the different engine types

Quoted 7 lines

enum nv84_decoder_eng_type

{
   BSP = 0,
   VP
};

#define NV84_DECODER_ENG_NUM VP + 1

Hmmm.... it's a point... the downside is that things become a little more confusing. I think there's some clarity to just having variable names like "bsp_foo" and "vp_foo" instead of "engines[VP].foo". And there are only 2 engines.

One advantage of your approach is that it'd save on some initialization code. I dunno. If someone else likes this approach too, I'll try it out.

Quoted 67 lines

src/gallium/drivers/nv50/nv84_video.h line 61

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.h new file +6 -0
+struct nv84_decoder {
+   struct pipe_video_decoder base;
+   struct nouveau_client *client;
+   struct nouveau_object *bsp_channel, *vp_channel, *bsp, *vp;
+   struct nouveau_pushbuf *bsp_pushbuf, *vp_pushbuf;
+   struct nouveau_bufctx *bsp_bufctx, *vp_bufctx;

Then the struct will look a bit cleaner

struct nv84_decoder {
   struct pipe_video_decoder base;
   struct nouveau_client *client;
   struct nv84_decoder_eng engs[NV84_DECODER_ENG_NUM];
Quoted 39 lines
src/gallium/drivers/nv50/nv84_video.h new file +39 -0
+
+   struct nouveau_bo *bsp_fw, *bsp_data;
+   struct nouveau_bo *vp_fw, *vp_data;
+   struct nouveau_bo *mbring, *vpring;
+
+   /*
+    * states:
+    *  0: init
+    *  1: vpring/mbring cleared, bsp is ready
+    *  2: bsp is done, vp is ready
+    * and then vp it back to 1
+    */
+   struct nouveau_bo *fence;
+
+   struct nouveau_bo *bitstream;
+   struct nouveau_bo *vp_params;
+
+   size_t vp_fw2_offset;
+
+   unsigned frame_mbs, frame_size;
+   /* VPRING layout:
+        RESIDUAL
+        CTRL
+        DEBLOCK
+        0x1000
+   */
+   unsigned vpring_deblock, vpring_residual, vpring_ctrl;
+
+
+   struct vl_mpg12_bs *mpeg12_bs;
+
+   struct nouveau_bo *mpeg12_bo;
+   void *mpeg12_mb_info;
+   uint16_t *mpeg12_data;
+   const int *zscan;
+   uint8_t mpeg12_intra_matrix[64];
+   uint8_t mpeg12_non_intra_matrix[64];
+};
+

...

src/gallium/drivers/nv50/nv84_video.h line 109

Quoted 9 lines
src/gallium/drivers/nv50/nv84_video.h new file +9 -0
+static INLINE uint32_t mb(uint32_t coord)
+{
+   return (coord + 0xf)>>4;
+}
+
+static INLINE uint32_t mb_half(uint32_t coord)
+{
+   return (coord + 0x1f)>>5;
+}

How about moving these in nouveau_video.h ? (and removing the duplicate copy from nvc0_video.h)

Quoted 1 line

Might be better as a follow on patch

Yeah. And as VP3/4 support comes out, I suspect a lot of nvc0_video will get refactored out since they're likely very similar.

Quoted 35 lines

...

Quoted 19 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +13 -0
new file mode 100644
index 0000000..60c0848
@@ -0,0 +1,521 @@
+/*
+ * Copyright 2013 Ilia Mirkin
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *

src/gallium/drivers/nv50/nv84_video_vp.c line 23

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +10 -0
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <immintrin.h>

Wrap this include in
#ifdef __SSE4_2__
#endif

Quoted 2 lines

considering its definitions are used in a similar block
...

I guess. There shouldn't be any harm to including it, but OK.

Quoted 27 lines
Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_h264(struct nv84_decoder *dec,
+                     struct pipe_h264_picture_desc *desc,
+                     struct nv84_video_buffer *dest)
+{

...

Quoted 1 line

+ for (i = 0; i < 2; i++) {

for (i = 0; i < dest->num_planes; i++) {

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +7 -0
+      struct nv50_miptree *mt = nv50_miptree(dest->resources[i]);
+      mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+   }
+
+   PUSH_KICK (push);
+}
+

...

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_mpeg12_mb(struct nv84_decoder *dec,
+                          struct pipe_mpeg12_picture_desc *desc,
+                          const struct pipe_mpeg12_macroblock *macrob)
+{

...

Quoted 3 lines
Quoted 1 line

+#ifdef __SSE4_2__

IMHO this may produce non portable binaries in case of aggressive mtune/march flags. I'm not objecting against it just pointing out

mtune shouldn't cause it to be defined. if you're specifying it on march, then that's the target arch, so... the compiler is free to use corei7 instructions anyways. Although distros will (likely) never use this, but people on, say, gentoo, will get the benefits. A much better approach would be to build the code anyways, and then select the best version at runtime.

$ gcc -dM -E - < /dev/null | grep SSE4
$ gcc -mtune=corei7 -dM -E - < /dev/null | grep SSE4
$ gcc -march=corei7 -dM -E - < /dev/null | grep SSE4
#define __SSE4_1__ 1
#define __SSE4_2__ 1
Quoted 22 lines
Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_mpeg12(struct nv84_decoder *dec,
+                       struct pipe_mpeg12_picture_desc *desc,
+                       struct nv84_video_buffer *dest)
+{

...

Quoted 1 line

+ for (i = 0; i < 2; i++) {

for (i = 0; i < dest->num_planes; i++) {

Cheers
Emil

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+      struct nv50_miptree *mt = nv50_miptree(dest->resources[i]);
+      mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+   }
+   PUSH_KICK (push);
+}
#3 lore raw
Quoted 19 lines · Ilia Mirkin
Quoted 18 lines · Emil Velikov

Hi Ilia,

Quoted 11 lines · Ilia Mirkin

Adds H.264 and MPEG2 codec support via VP2, using firmware from the blob. Acceleration is supported at the bitstream level for H.264 and IDCT level for MPEG2.

Known issues:
- H.264 interlaced doesn't render properly
- H.264 shows very occasional artifacts on a small fraction of videos
- MPEG2 + VDPAU shows frequent but small artifacts, which aren't there when using XvMC on the same videos

Big thanks for working on this

Quoted 8 lines
Quoted 2 lines

I believe the hardware is capable of accelerating IDCT for VC1. Do you have any plans for it ?

Nope. With MPEG, there's XvMC, which sends the macroblocks directly. And there's an existing MPEG parser in gallium which can be used for VDPAU. With VC-1, no such thing as XvMC. Which means that I'd have to implement a bitstream parser in gallium. The spec is closed, and I'm not at all familiar with the concepts used in VC-1.

I thought that gallium already has a vc1 bitstream parser. Anyway if anyone is interested the libavcodec project can be useful in that regard

Quoted 9 lines
Quoted 4 lines

As far as I know mesa in general is keen on keeping trailing statements on the next line, as well as 78(80) characters line length.

I tried to do that... in the places where I violate that I felt that it enhanced readability. Feel free to point out places where you disagree. Also if it's a hard policy (e.g. as in the linux kernel), I can go through and reformat.

There is a document describing the Mesa Coding Style[1], although enforcing it is another question

My main point was statements such as

if (ret) return NULL

which are rather hard to see when surrounded by compact code.

[1] http://www.mesa3d.org/devinfo.html

Quoted 223 lines
Quoted 10 lines
Quoted 10 lines

---

I did try to work out the known issues above, but so far to no avail.

The kernel support for these engines is not in mainline yet, but it's likely going to appear in 3.11. I figured that there would be a bunch of feedback so I might as well send it out early.

Quoted 6 lines
Quoted 4 lines

I played around a lot with XvMC performance, the mb function shows up as #1 in the profiles, with the SSE4.2 optimizations I put in, it drops to #2. Something clever can likely be done to improve VDPAU performance as well, e.g. using SSE to do the inverse quantization operations, but I've left that

Curious how many machines with vp2 card have a sse4.2 capable CPU? Mine is only sse4.1 ;(

At least one (mine). SSE4.2 appeared on Core i7 (Nehalem), AFAIK. Other optimizations may be possible with just SSE2, but I couldn't think of anything particularly clever. Those PCMPESTRM instructions are _really_ useful to skip over the useless 0's. I guess something could be done with PCMPEQW and PMOVMSKB (wish there were a PMOVMSKW...). I'll play with it. But the plain-C loop isn't so bad either.

Quoted 49 lines
Quoted 36 lines

out. (It gets tricky because a lot of the data is 0's, so it's unclear whether it's faster to use SSE to do operations on everything or one-at-a-time on the non-0's like I have it now.) Even with these, XvMC ends up ~20% faster than plain CPU decoding, and likely that percent improves on older CPUs that can't decode MPEG2 quite as quickly. VDPAU provides further improvements (likely because it is able to skip mb's while XvMC can't), but there are artifacts for reasons unknown.

Note that in order to get XvMC to work, you need my previous patch (or something similar) since otherwise libXvMCnouveau can't be dlopen'd:
http://lists.freedesktop.org/archives/mesa-dev/2013-June/040949.html

If you want to test it out, the kernel patch:
http://lists.freedesktop.org/archives/nouveau/2013-June/012821.html

Firmware:
https://github.com/imirkin/re-vp2/blob/master/extract_firmware.py

 src/gallium/drivers/nv50/Makefile.sources |   5 +-
 src/gallium/drivers/nv50/nv50_context.c   |  13 +-
 src/gallium/drivers/nv50/nv50_context.h   |  24 +
 src/gallium/drivers/nv50/nv50_miptree.c   |  27 ++
 src/gallium/drivers/nv50/nv50_resource.h  |   1 +
 src/gallium/drivers/nv50/nv50_screen.c    |  13 +-
 src/gallium/drivers/nv50/nv50_winsys.h    |   4 +
 src/gallium/drivers/nv50/nv84_video.c     | 778 ++++++++++++++++++++++++++++++
 src/gallium/drivers/nv50/nv84_video.h     | 134 +++++
 src/gallium/drivers/nv50/nv84_video_bsp.c | 251 ++++++++++
 src/gallium/drivers/nv50/nv84_video_vp.c  | 521 ++++++++++++++++++++
 11 files changed, 1768 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/drivers/nv50/nv84_video.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video.h
 create mode 100644 src/gallium/drivers/nv50/nv84_video_bsp.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video_vp.c

...

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +0 -0
new file mode 100644
index 0000000..064178c
@@ -0,0 +1,778 @@

...

src/gallium/drivers/nv50/nv84_video.c line 35

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+static int
+nv84_copy_firmware(const char *path, void *dest, size_t len)

ssize_t len
To prevent signed/unsigned issues in conditional below

Quoted 12 lines

src/gallium/drivers/nv50/nv84_video.c line 46

Quoted 11 lines
src/gallium/drivers/nv50/nv84_video.c new file +11 -0
+{
+   int fd = open(path, O_RDONLY | O_CLOEXEC);
+   ssize_t r;
+   if (fd < 0) {
+      fprintf(stderr, "opening firmware file %s failed: %m\n", path);
+      return 1;
+   }
+   r = read(fd, dest, len);
+   close(fd);
+
+   if (r != len) {

Here ^^

Yes, in case that len is 2^64 - 1... but I'll change it :)

Quoted 9 lines
Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+      fprintf(stderr, "reading firwmare file %s failed: %m\n", path);
+      return 1;
+   }
+
+   return 0;
+}
+

...

Quoted 16 lines
Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+static void
+nv84_decoder_decode_bitstream_mpeg12(struct pipe_video_decoder *decoder,
+                                     struct pipe_video_buffer *video_target,
+                                     struct pipe_picture_desc *picture,

src/gallium/drivers/nv50/nv84_video.c line 171

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+                                     unsigned num_buffers,
+                                     const void *const *data,
+                                     const unsigned *num_bytes)
+{
+   struct nv84_decoder *dec = (struct nv84_decoder *)decoder;
+   struct nv84_video_buffer *target = (struct nv84_video_buffer *)video_target;
+
+   struct pipe_mpeg12_picture_desc *desc = (struct pipe_mpeg12_picture_desc *)picture;
+
+   assert(target->base.buffer_format == PIPE_FORMAT_NV12);

This can be written as
assert(video_target->buffer_format == PIPE_FORMAT_NV12);

Good point. The code was a bit different at an earlier point.

Quoted 85 lines

src/gallium/drivers/nv50/nv84_video.c line 178

Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+
+   vl_mpg12_bs_decode(dec->mpeg12_bs,
+                      video_target,
+                      desc,
+                      num_buffers,
+                      data,
+                      num_bytes);

And then the temporary variables can be removed, as you've done in nv84_decoder_end_frame_mpeg12()

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+}
+

...

Quoted 71 lines
src/gallium/drivers/nv50/nv84_video.c new file +71 -0
+
+struct pipe_video_decoder *
+nv84_create_decoder(struct pipe_context *context,
+                    enum pipe_video_profile profile,
+                    enum pipe_video_entrypoint entrypoint,
+                    enum pipe_video_chroma_format chroma_format,
+                    unsigned width, unsigned height,
+                    unsigned max_references,
+                    bool chunked_decode)
+{
+   struct nv50_context *nv50 = (struct nv50_context *)context;
+   struct nouveau_screen *screen = &nv50->screen->base;
+   struct nv84_decoder *dec;
+   struct nouveau_pushbuf *bsp_push, *vp_push;
+   struct nv50_surface surf;
+   struct nv50_miptree mip;
+   union pipe_color_union color;
+   struct nv04_fifo nv04_data = { .vram = 0xbeef0201, .gart = 0xbeef0202 };
+   int ret, i;
+   int is_h264 = u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG4_AVC;
+   int is_mpeg12 = u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12;
+   struct nouveau_pushbuf_refn fence_ref[] = {
+      { NULL, NOUVEAU_BO_RDWR | NOUVEAU_BO_VRAM },
+   };
+
+
+   if (getenv("XVMC_VL"))
+      return vl_create_decoder(context, profile, entrypoint,
+                               chroma_format, width, height,
+                               max_references, chunked_decode);
+
+   if ((is_h264 && entrypoint != PIPE_VIDEO_ENTRYPOINT_BITSTREAM) ||
+       (is_mpeg12 && entrypoint > PIPE_VIDEO_ENTRYPOINT_IDCT)) {
+      debug_printf("%x\n", entrypoint);
+      return NULL;
+   }
+
+   if (!is_h264 && !is_mpeg12) {
+      debug_printf("invalid profile: %x\n", profile);
+      return NULL;
+   }
+
+   dec = CALLOC_STRUCT(nv84_decoder);
+   if (!dec) return NULL;
+
+   dec->base.context = context;
+   dec->base.profile = profile;
+   dec->base.entrypoint = entrypoint;
+   dec->base.chroma_format = chroma_format;
+   dec->base.width = width;
+   dec->base.height = height;
+   dec->base.max_references = max_references;
+   dec->base.destroy = nv84_decoder_destroy;
+   dec->base.flush = nv84_decoder_flush;
+   if (is_h264) {
+      dec->base.decode_bitstream = nv84_decoder_decode_bitstream_h264;
+      dec->base.begin_frame = nv84_decoder_begin_frame_h264;
+      dec->base.end_frame = nv84_decoder_end_frame_h264;
+
+      dec->frame_mbs = mb(dec->base.width) * mb_half(dec->base.height) * 2;
+      dec->frame_size = dec->frame_mbs << 8;
+      dec->vpring_deblock = align(0x30 * dec->frame_mbs, 0x100);
+      dec->vpring_residual = 0x2000 + MAX2(0x32000, 0x600 * dec->frame_mbs);
+      dec->vpring_ctrl = MAX2(0x10000, align(0x1080 + 0x144 * dec->frame_mbs, 0x100));
+   } else if (is_mpeg12) {
+      dec->base.decode_macroblock = nv84_decoder_decode_macroblock;
+      dec->base.begin_frame = nv84_decoder_begin_frame_mpeg12;
+      dec->base.end_frame = nv84_decoder_end_frame_mpeg12;
+
+      if (entrypoint == PIPE_VIDEO_ENTRYPOINT_BITSTREAM) {
+         dec->mpeg12_bs = CALLOC_STRUCT(vl_mpg12_bs);
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 340

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+         if (!dec->mpeg12_bs)
+            goto fail;
+         vl_mpg12_bs_init(dec->mpeg12_bs, &dec->base);
+         dec->base.decode_bitstream = nv84_decoder_decode_bitstream_mpeg12;
+      }
+   } else {
+      goto fail;

Seems to be handled already by - if (!is_h264 && !is_mpeg12)...

Yes. But I disliked relying on that, so I decided to leave it in.

Quoted 9 lines

src/gallium/drivers/nv50/nv84_video.c line 345

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +5 -0
+   }
+
+   ret = nouveau_client_new(screen->device, &dec->client);
+   if (ret)
+      goto fail;

Is there any particular reason for using a variable to store the return value through this functions? Me thinks it can be safely purged, making the code a bit cleaner

Quoted 5 lines

It was like that in nvc0_video and a lot of other code. I think the code looks a little odd when you have

if (super-long multi-line statement)
goto fail

There are cases where you've left the "super-long multi-line statement" in the conditional and others where you did not. I suggested changing the one that may produce smaller code and is more appealing imho

Quoted 18 lines
Quoted 18 lines
Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +5 -0
+
+   if (is_h264) {
+      ret = nouveau_object_new(&screen->device->object, 0,
+                               NOUVEAU_FIFO_CHANNEL_CLASS,
+                               &nv04_data, sizeof(nv04_data), &dec->bsp_channel);

...

Quoted 9 lines
src/gallium/drivers/nv50/nv84_video.c new file +9 -0
+   if (is_h264) {
+      /* Zero out some parts of mbring/vpring. there's gotta be some cleaner way
+       * of doing this... perhaps makes sense to just copy the relevant logic
+       * here. */
+      color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
+      surf.offset = dec->frame_size;
+      surf.width = 64;
+      surf.height = (max_references + 1) * dec->frame_mbs / 4;
+      surf.depth = 1;
Quoted 8 lines
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 492

Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +8 -0
+      surf.base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      surf.base.u.tex.level = 0;
+      surf.base.texture = &mip.base.base;
+      mip.level[0].tile_mode = 0;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.domain = NOUVEAU_BO_VRAM;
+      mip.base.bo = dec->mbring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 64, 4760);

You can drop the typecast here
s/(struct pipe_surface *)&surf/&surf.base/

Quoted 6 lines
Quoted 6 lines

src/gallium/drivers/nv50/nv84_video.c line 498

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+      surf.offset = dec->vpring->size / 2 - 0x1000;
+      surf.width = 1024;
+      surf.height = 1;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.bo = dec->vpring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);

Ditto

Quoted 2 lines
Quoted 2 lines

src/gallium/drivers/nv50/nv84_video.c line 500

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+      surf.offset = dec->vpring->size - 0x1000;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);

Ditto

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
src/gallium/drivers/nv50/nv84_video.c new file +14 -0
+
+      PUSH_SPACE(screen->pushbuf, 5);
+      fence_ref[0].bo = dec->fence;
+      nouveau_pushbuf_refn(screen->pushbuf, fence_ref, 1);
+      /* The clear_render_target is done via 3D engine, so use it to write to a
+       * sempahore to indicate that it's done.
+       */
+      BEGIN_NV04(screen->pushbuf, SUBC_3D(0x1b00), 4);
+      PUSH_DATAh(screen->pushbuf, dec->fence->offset);
+      PUSH_DATA (screen->pushbuf, dec->fence->offset);
+      PUSH_DATA (screen->pushbuf, 1);
+      PUSH_DATA (screen->pushbuf, 0xf010);
+      PUSH_KICK (screen->pushbuf);
+
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 521

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+      PUSH_SPACE(bsp_push, 2 + 12 + 2 + 4 + 3);
+
+      BEGIN_NV04(bsp_push, SUBC_BSP(NV01_SUBCHAN_OBJECT), 1);
+      PUSH_DATA (bsp_push, dec->bsp->handle);
+
+      BEGIN_NV04(bsp_push, SUBC_BSP(0x180), 11);
+      for (i = 0; i < 11; i++)

Any idea where 11 comes from ? Is it related to some other parameter ?

Quoted 6 lines

These are the dma indices (e.g. when i use 0x654321, each nibble is a dmaidx, which is an offset into these 0x180 values). It's the part of the whole thing that I understand the least... in theory I should be able to just initialize one of them and use it throughout, but I haven't been brave enough to put that theory to the test. This is how the blob does it.

If it works don't break it ^_^

Quoted 170 lines
Quoted 23 lines
Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+         PUSH_DATA(bsp_push, nv04_data.vram);
+      BEGIN_NV04(bsp_push, SUBC_BSP(0x1b8), 1);
+      PUSH_DATA (bsp_push, nv04_data.vram);
+

...

Quoted 4 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+struct pipe_video_buffer *
+nv84_video_buffer_create(struct pipe_context *pipe,
+                         const struct pipe_video_buffer *template)
+{

...

src/gallium/drivers/nv50/nv84_video.c line 653

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   buffer->base.buffer_format = template->buffer_format;
+   buffer->base.context = pipe;
+   buffer->base.destroy = nv84_video_buffer_destroy;
+   buffer->base.chroma_format = template->chroma_format;
+   buffer->base.width = template->width;
+   buffer->base.height = template->height;
+   buffer->base.get_sampler_view_planes = nv84_video_buffer_sampler_view_planes;
+   buffer->base.get_sampler_view_components = nv84_video_buffer_sampler_view_components;
+   buffer->base.get_surfaces = nv84_video_buffer_surfaces;
+   buffer->base.interlaced = true;
Quoted 3 lines

By storing the number of planes, will be able to demagic some constants later on
buffer->num_planes = 2;

nvc0_video does that. I find that incredibly confusing. It's always 2, and they are of diff size. I prefer my bike-shed color :)

Quoted 27 lines
Quoted 15 lines
src/gallium/drivers/nv50/nv84_video.c new file +14 -0
+
+   memset(&templ, 0, sizeof(templ));
+   templ.target = PIPE_TEXTURE_2D_ARRAY;
+   templ.depth0 = 1;
+   templ.bind = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
+   templ.format = PIPE_FORMAT_R8_UNORM;
+   templ.width0 = align(template->width, 2);
+   templ.height0 = align(template->height, 4) / 2;
+   templ.flags = NV50_RESOURCE_FLAG_VIDEO;
+   templ.array_size = 2;
+
+   cfg.nv50.tile_mode = 0x20;
+   cfg.nv50.memtype = 0x70;
+

src/gallium/drivers/nv50/nv84_video.c line 677

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+   buffer->resources[0] = pipe->screen->resource_create(pipe->screen, &templ);
+   if (!buffer->resources[0])
+      goto error;
+
+   templ.format = PIPE_FORMAT_R8G8_UNORM;
+   templ.width0 /= 2;
+   templ.height0 /= 2;
+   buffer->resources[1] = pipe->screen->resource_create(pipe->screen, &templ);
+   if (!buffer->resources[1])
+      goto error;

I believe that the nvc0 version of the code is easier to read (bikeshed)

Quoted 1 line

for (i = 1; i < buffer->num_planes; ++i) {

num_planes is always 2. for (i = 1; i < 2; i++) seems like a waste...

Quoted 18 lines
   buffer->resources[i] = pipe->screen->resource_create(pipe->screen,
&templ);
   if (!buffer->resources[i])
      goto error;
}
Quoted 12 lines
src/gallium/drivers/nv50/nv84_video.c new file +12 -0
+
+   mt0 = nv50_miptree(buffer->resources[0]);
+   mt1 = nv50_miptree(buffer->resources[1]);
+
+   bo_size = mt0->total_size + mt1->total_size;
+   if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
+                      bo_size, &cfg, &buffer->interlaced))
+      goto error;
+   /* XXX Change reference frame management so that this is only allocated in
+    * the decoder when necessary. */
+   if (nouveau_bo_new(screen->device, NOUVEAU_BO_VRAM | NOUVEAU_BO_NOSNOOP, 0,
+                      bo_size, &cfg, &buffer->full))
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 695

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+      goto error;
+
+   mt0->base.bo = buffer->interlaced;
+   mt0->base.domain = NOUVEAU_BO_VRAM;
+   mt0->base.offset = 0;
+   mt0->base.address = buffer->interlaced->offset;

IMHO this looks a bit easier to grasp
mt0->base.address = buffer->interlaced->offset + mt0->base.offset;

Agreed.

Quoted 9 lines

src/gallium/drivers/nv50/nv84_video.c line 701

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+   nouveau_bo_ref(buffer->interlaced, &empty);
+
+   mt1->base.bo = buffer->interlaced;
+   mt1->base.domain = NOUVEAU_BO_VRAM;
+   mt1->base.offset = mt0->layer_stride * 2;
+   mt1->base.address = buffer->interlaced->offset + mt0->layer_stride * 2;

Similar
mt1->base.address = buffer->interlaced->offset + mt1->base.offset;

Yep.

Quoted 59 lines

src/gallium/drivers/nv50/nv84_video.c line 705

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video.c new file +4 -0
+   nouveau_bo_ref(buffer->interlaced, &empty);
+
+   memset(&sv_templ, 0, sizeof(sv_templ));
+   for (component = 0, i = 0; i < 2; ++i ) {

for (component = 0, i = 0; i < buffer->num_planes; ++i ) {

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+      struct pipe_resource *res = buffer->resources[i];
+      unsigned nr_components = util_format_get_nr_components(res->format);
+
+      u_sampler_view_default_template(&sv_templ, res, res->format);
+      buffer->sampler_view_planes[i] = pipe->create_sampler_view(pipe, res, &sv_templ);
+      if (!buffer->sampler_view_planes[i])
+         goto error;
+
+      for (j = 0; j < nr_components; ++j, ++component) {
+         sv_templ.swizzle_r = sv_templ.swizzle_g = sv_templ.swizzle_b = PIPE_SWIZZLE_RED + j;

src/gallium/drivers/nv50/nv84_video.c line 725

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+         sv_templ.swizzle_a = PIPE_SWIZZLE_ONE;
+
+         buffer->sampler_view_components[component] = pipe->create_sampler_view(pipe, res, &sv_templ);
+         if (!buffer->sampler_view_components[component])
+            goto error;
+      }
+   }
+
+   memset(&surf_templ, 0, sizeof(surf_templ));
+   for (j = 0; j < 2; ++j) {

for (j = 0; j < buffer->num_planes; ++j) {

Quoted 18 lines
src/gallium/drivers/nv50/nv84_video.c new file +18 -0
+      surf_templ.format = buffer->resources[j]->format;
+      surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = 0;
+      buffer->surfaces[j * 2] = pipe->create_surface(pipe, buffer->resources[j], &surf_templ);
+      if (!buffer->surfaces[j * 2])
+         goto error;
+
+      surf_templ.u.tex.first_layer = surf_templ.u.tex.last_layer = 1;
+      buffer->surfaces[j * 2 + 1] = pipe->create_surface(pipe, buffer->resources[j], &surf_templ);
+      if (!buffer->surfaces[j * 2 + 1])
+         goto error;
+   }
+
+   return &buffer->base;
+
+error:
+   nv84_video_buffer_destroy(&buffer->base);
+   return NULL;
+}

src/gallium/drivers/nv50/nv84_video.c line 753

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.c new file +10 -0
+
+int
+nv84_screen_get_video_param(struct pipe_screen *pscreen,
+                            enum pipe_video_profile profile,
+                            enum pipe_video_cap param)
+{
+   switch (param) {
+   case PIPE_VIDEO_CAP_SUPPORTED:
+      return u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG4_AVC ||
+         u_reduce_video_profile(profile) == PIPE_VIDEO_CODEC_MPEG12;
      switch (u_reduce_video_profile(profile)) {
      case PIPE_VIDEO_CODEC_MPEG12:
Quoted 8 lines
      case PIPE_VIDEO_CODEC_MPEG4_AVC:
         return true;
      case PIPE_VIDEO_CODEC_VC1:
         /* TODO: Hardware is capable of IDCT acceleration for VC1*/
      case PIPE_VIDEO_CODEC_MPEG4:
      default:
         return false;
      }
Quoted 1 line

Why? switch inside a switch is a bit ugly.

Few silly reasons
* u_reduce_video_profile() is a nice switch statement
* ^^ is inlined and executed twice
* adds a note about VC1 :P

Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
src/gallium/drivers/nv50/nv84_video.c new file +17 -0
+   case PIPE_VIDEO_CAP_NPOT_TEXTURES:
+      return 1;
+   case PIPE_VIDEO_CAP_MAX_WIDTH:
+   case PIPE_VIDEO_CAP_MAX_HEIGHT:
+      return 2048;
+   case PIPE_VIDEO_CAP_PREFERED_FORMAT:
+      return PIPE_FORMAT_NV12;
+   case PIPE_VIDEO_CAP_SUPPORTS_INTERLACED:
+   case PIPE_VIDEO_CAP_PREFERS_INTERLACED:
+      return true;
+   case PIPE_VIDEO_CAP_SUPPORTS_PROGRESSIVE:
+      return false;
+   default:
+      debug_printf("unknown video param: %d\n", param);
+      return 0;
+   }
+}
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 777

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.c new file +7 -0
+
+boolean
+nv84_screen_video_supported(struct pipe_screen *screen,
+                            enum pipe_format format,
+                            enum pipe_video_profile profile)
+{
+   return format == PIPE_FORMAT_NV12;

Will this work when we have XVMC_VL set ?

Quoted 3 lines

Hm, well, XVMC_VL totally doesn't work for me... crashes in the shader somewhere. I just copied that logic from nvc0... If anything this would be too restrictive...

Hm... true, nouveau is quite restrictive. Radeons are handling it nicely, with a fallback to vl if uvd is not avaiable. Not that it matters here :)

Quoted 46 lines
Quoted 46 lines
Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +1 -0
+}
src/gallium/drivers/nv50/nv84_video.h new file +0 -0
new file mode 100644
index 0000000..4ff8cf3

...

src/gallium/drivers/nv50/nv84_video.h line 45

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.h new file +2 -0
+struct nv84_video_buffer {
+   struct pipe_video_buffer base;

unsigned num_planes;

src/gallium/drivers/nv50/nv84_video.h line 55

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video.h new file +10 -0
+   struct pipe_resource *resources[VL_NUM_COMPONENTS];
+   struct pipe_sampler_view *sampler_view_planes[VL_NUM_COMPONENTS];
+   struct pipe_sampler_view *sampler_view_components[VL_NUM_COMPONENTS];
+   struct pipe_surface *surfaces[VL_NUM_COMPONENTS * 2];
+
+   struct nouveau_bo *interlaced, *full;
+   int mvidx;
+   unsigned frame_num, frame_num_max;
+};
+

Looking at the params associated with each video engine, I was wondering about compacting it into a struct (names chosen are the first thing that came to mind)

struct nv84_decoder_eng {
   struct nouveau_object *obj;
   struct nouveau_object *channel;
   struct nouveau_pushbuf *pushbuf;
   struct nouveau_bufctx *bufctx;

   struct nouveau_bo *fw;
   struct nouveau_bo *data;
}

and then having an enum for the different engine types

enum nv84_decoder_eng_type

{
   BSP = 0,
   VP
};
Quoted 6 lines
Quoted 1 line

#define NV84_DECODER_ENG_NUM VP + 1

Hmmm.... it's a point... the downside is that things become a little more confusing. I think there's some clarity to just having variable names like "bsp_foo" and "vp_foo" instead of "engines[VP].foo". And there are only 2 engines.

When I came up with this I was looking at the nvc0 code, which was less intuitive

   struct nouveau_object *channel[3], *bsp, *vp, *ppp;
   struct nouveau_pushbuf *pushbuf[3];
Quoted 148 lines

One advantage of your approach is that it'd save on some initialization code. I dunno. If someone else likes this approach too, I'll try it out.

Quoted 67 lines

src/gallium/drivers/nv50/nv84_video.h line 61

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video.h new file +6 -0
+struct nv84_decoder {
+   struct pipe_video_decoder base;
+   struct nouveau_client *client;
+   struct nouveau_object *bsp_channel, *vp_channel, *bsp, *vp;
+   struct nouveau_pushbuf *bsp_pushbuf, *vp_pushbuf;
+   struct nouveau_bufctx *bsp_bufctx, *vp_bufctx;

Then the struct will look a bit cleaner

struct nv84_decoder {
   struct pipe_video_decoder base;
   struct nouveau_client *client;
   struct nv84_decoder_eng engs[NV84_DECODER_ENG_NUM];
Quoted 39 lines
src/gallium/drivers/nv50/nv84_video.h new file +39 -0
+
+   struct nouveau_bo *bsp_fw, *bsp_data;
+   struct nouveau_bo *vp_fw, *vp_data;
+   struct nouveau_bo *mbring, *vpring;
+
+   /*
+    * states:
+    *  0: init
+    *  1: vpring/mbring cleared, bsp is ready
+    *  2: bsp is done, vp is ready
+    * and then vp it back to 1
+    */
+   struct nouveau_bo *fence;
+
+   struct nouveau_bo *bitstream;
+   struct nouveau_bo *vp_params;
+
+   size_t vp_fw2_offset;
+
+   unsigned frame_mbs, frame_size;
+   /* VPRING layout:
+        RESIDUAL
+        CTRL
+        DEBLOCK
+        0x1000
+   */
+   unsigned vpring_deblock, vpring_residual, vpring_ctrl;
+
+
+   struct vl_mpg12_bs *mpeg12_bs;
+
+   struct nouveau_bo *mpeg12_bo;
+   void *mpeg12_mb_info;
+   uint16_t *mpeg12_data;
+   const int *zscan;
+   uint8_t mpeg12_intra_matrix[64];
+   uint8_t mpeg12_non_intra_matrix[64];
+};
+

...

src/gallium/drivers/nv50/nv84_video.h line 109

Quoted 9 lines
src/gallium/drivers/nv50/nv84_video.h new file +9 -0
+static INLINE uint32_t mb(uint32_t coord)
+{
+   return (coord + 0xf)>>4;
+}
+
+static INLINE uint32_t mb_half(uint32_t coord)
+{
+   return (coord + 0x1f)>>5;
+}

How about moving these in nouveau_video.h ? (and removing the duplicate copy from nvc0_video.h)

Quoted 1 line

Might be better as a follow on patch

Yeah. And as VP3/4 support comes out, I suspect a lot of nvc0_video will get refactored out since they're likely very similar.

Quoted 35 lines

...

Quoted 19 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +13 -0
new file mode 100644
index 0000000..60c0848
@@ -0,0 +1,521 @@
+/*
+ * Copyright 2013 Ilia Mirkin
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *

src/gallium/drivers/nv50/nv84_video_vp.c line 23

Quoted 10 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +10 -0
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <immintrin.h>

Wrap this include in
#ifdef __SSE4_2__
#endif

Quoted 2 lines

considering its definitions are used in a similar block
...

I guess. There shouldn't be any harm to including it, but OK.

Quoted 27 lines
Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_h264(struct nv84_decoder *dec,
+                     struct pipe_h264_picture_desc *desc,
+                     struct nv84_video_buffer *dest)
+{

...

Quoted 1 line

+ for (i = 0; i < 2; i++) {

for (i = 0; i < dest->num_planes; i++) {

Quoted 7 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +7 -0
+      struct nv50_miptree *mt = nv50_miptree(dest->resources[i]);
+      mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+   }
+
+   PUSH_KICK (push);
+}
+

...

Quoted 5 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_mpeg12_mb(struct nv84_decoder *dec,
+                          struct pipe_mpeg12_picture_desc *desc,
+                          const struct pipe_mpeg12_macroblock *macrob)
+{

...

Quoted 3 lines
Quoted 1 line

+#ifdef __SSE4_2__

IMHO this may produce non portable binaries in case of aggressive mtune/march flags. I'm not objecting against it just pointing out

Quoted 6 lines

mtune shouldn't cause it to be defined. if you're specifying it on march, then that's the target arch, so... the compiler is free to use corei7 instructions anyways. Although distros will (likely) never use this, but people on, say, gentoo, will get the benefits. A much better approach would be to build the code anyways, and then select the best version at runtime.

Ouch you got me there, I was not too sure which one was the correct gcc flag. AFAIK mesa does have code detecting runtime cpu capabilities, but that can follow on if anyone is interested

Cheers
Emil

Quoted 30 lines
$ gcc -dM -E - < /dev/null | grep SSE4
$ gcc -mtune=corei7 -dM -E - < /dev/null | grep SSE4
$ gcc -march=corei7 -dM -E - < /dev/null | grep SSE4
#define __SSE4_1__ 1
#define __SSE4_2__ 1
Quoted 22 lines
Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+void
+nv84_decoder_vp_mpeg12(struct nv84_decoder *dec,
+                       struct pipe_mpeg12_picture_desc *desc,
+                       struct nv84_video_buffer *dest)
+{

...

Quoted 1 line

+ for (i = 0; i < 2; i++) {

for (i = 0; i < dest->num_planes; i++) {

Cheers
Emil

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video_vp.c new file +5 -0
+      struct nv50_miptree *mt = nv50_miptree(dest->resources[i]);
+      mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
+   }
+   PUSH_KICK (push);
+}
#4 lore raw
Quoted 5 lines · Emil Velikov
Quoted 4 lines · Ilia Mirkin
Quoted 2 lines · Emil Velikov

I believe the hardware is capable of accelerating IDCT for VC1. Do you have any plans for it ?

Quoted 7 lines
Quoted 5 lines

Nope. With MPEG, there's XvMC, which sends the macroblocks directly. And there's an existing MPEG parser in gallium which can be used for VDPAU. With VC-1, no such thing as XvMC. Which means that I'd have to implement a bitstream parser in gallium. The spec is closed, and I'm not at all familiar with the concepts used in VC-1.

I thought that gallium already has a vc1 bitstream parser. Anyway if anyone is interested the libavcodec project can be useful in that regard

libavcodec is LGPL (incompatible with the mesa license for copying code) and doesn't have a nice API to extract the macroblock data.

Quoted 10 lines
Quoted 7 lines
Quoted 2 lines

As far as I know mesa in general is keen on keeping trailing statements on the next line, as well as 78(80) characters line length.

I tried to do that... in the places where I violate that I felt that it enhanced readability. Feel free to point out places where you disagree. Also if it's a hard policy (e.g. as in the linux kernel), I can go through and reformat.

There is a document describing the Mesa Coding Style[1], although enforcing it is another question

Quoted 7 lines

My main point was statements such as

if (ret) return NULL

which are rather hard to see when surrounded by compact code.

[1] http://www.mesa3d.org/devinfo.html

Oh, I see what you mean by "trailing statements" now. [I guess that's really the only interpretation, not sure why it didn't click before.] I'll go through and clean that up. I actually usually prefer that same policy too, except for super-short trailing statements.

Quoted 26 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
src/gallium/drivers/nv50/nv84_video.c new file +9 -0
+   if (is_h264) {
+      /* Zero out some parts of mbring/vpring. there's gotta be some cleaner way
+       * of doing this... perhaps makes sense to just copy the relevant logic
+       * here. */
+      color.f[0] = color.f[1] = color.f[2] = color.f[3] = 0;
+      surf.offset = dec->frame_size;
+      surf.width = 64;
+      surf.height = (max_references + 1) * dec->frame_mbs / 4;
+      surf.depth = 1;
Quoted 8 lines
Quoted 8 lines

src/gallium/drivers/nv50/nv84_video.c line 492

Quoted 8 lines
src/gallium/drivers/nv50/nv84_video.c new file +8 -0
+      surf.base.format = PIPE_FORMAT_B8G8R8A8_UNORM;
+      surf.base.u.tex.level = 0;
+      surf.base.texture = &mip.base.base;
+      mip.level[0].tile_mode = 0;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.domain = NOUVEAU_BO_VRAM;
+      mip.base.bo = dec->mbring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 64, 4760);

You can drop the typecast here
s/(struct pipe_surface *)&surf/&surf.base/

Quoted 6 lines
Quoted 6 lines

src/gallium/drivers/nv50/nv84_video.c line 498

Quoted 6 lines
src/gallium/drivers/nv50/nv84_video.c new file +6 -0
+      surf.offset = dec->vpring->size / 2 - 0x1000;
+      surf.width = 1024;
+      surf.height = 1;
+      mip.level[0].pitch = surf.width * 4;
+      mip.base.bo = dec->vpring;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);
Quoted 5 lines

Ditto

Quoted 2 lines
Quoted 2 lines

src/gallium/drivers/nv50/nv84_video.c line 500

Quoted 2 lines
src/gallium/drivers/nv50/nv84_video.c new file +2 -0
+      surf.offset = dec->vpring->size - 0x1000;
+      context->clear_render_target(context, (struct pipe_surface *)&surf, &color, 0, 0, 1024, 1);

Ditto

Nice catches. I hate this code... all this setup effort just to zero some stuff out. =/

Quoted 28 lines
Quoted 22 lines
Quoted 22 lines

Looking at the params associated with each video engine, I was wondering about compacting it into a struct (names chosen are the first thing that came to mind)

struct nv84_decoder_eng {
   struct nouveau_object *obj;
   struct nouveau_object *channel;
   struct nouveau_pushbuf *pushbuf;
   struct nouveau_bufctx *bufctx;

   struct nouveau_bo *fw;
   struct nouveau_bo *data;
}

and then having an enum for the different engine types

enum nv84_decoder_eng_type

{
   BSP = 0,
   VP
};
Quoted 6 lines
Quoted 1 line

#define NV84_DECODER_ENG_NUM VP + 1

Hmmm.... it's a point... the downside is that things become a little more confusing. I think there's some clarity to just having variable names like "bsp_foo" and "vp_foo" instead of "engines[VP].foo". And there are only 2 engines.

Quoted 6 lines

When I came up with this I was looking at the nvc0 code, which was less intuitive

   struct nouveau_object *channel[3], *bsp, *vp, *ppp;
   struct nouveau_pushbuf *pushbuf[3];

Next try to work out how those are populated :) For pre-kepler all the pushbufs are the same, for kepler they are different. It was suggested to me that it would be advantageous to keep BSP and VP on separate channels, and also the blob does it, so I did it that way. I don't fully understand how the hardware interprets these things, so... whatever.

Quoted 10 lines
Quoted 10 lines
Quoted 3 lines
Quoted 1 line

+#ifdef __SSE4_2__

IMHO this may produce non portable binaries in case of aggressive mtune/march flags. I'm not objecting against it just pointing out

mtune shouldn't cause it to be defined. if you're specifying it on march, then that's the target arch, so... the compiler is free to use corei7 instructions anyways. Although distros will (likely) never use this, but people on, say, gentoo, will get the benefits. A much better approach would be to build the code anyways, and then select the best version at runtime.

Quoted 3 lines

Ouch you got me there, I was not too sure which one was the correct gcc flag. AFAIK mesa does have code detecting runtime cpu capabilities, but that can follow on if anyone is interested

Well, as luck would have it, I've redone it as SSE2 and it's faster (more instructions, but fewer cycles). So I'm just going to replace it with SSE2, which is on by default for x86_64. I didn't see any runtime feature detection logic in gallium, only compile-time (e.g. in pipe/p_config.h and util/u_sse.h) but I'm very new to the mesa codebase, so I could have missed it. And sadly __builtin_cpu_supports is probably too new to use.

Thanks for all the feedback!

-ilia

A message that is not in the archive (sent off-list or not yet received): 1372332404-16547-1-git-send-email-imirkin-FrUbXkNCsVf2fBVCVOL8/[email protected]

#6 lore raw

PATCH v2 1/1 nv50: H.264/MPEG2 decoding support via VP2, available on NV84-NV96, NVA0

Adds H.264 and MPEG2 codec support via VP2, using firmware from the blob. Acceleration is supported at the bitstream level for H.264 and IDCT level for MPEG2.

Known issues:
- H.264 interlaced doesn't render properly
- H.264 shows very occasional artifacts on a small fraction of videos
- MPEG2 + VDPAU shows frequent but small artifacts, which aren't there when using XvMC on the same videos

---

v1 -> v2:

- Replaced SSE4.2 code with SSE2 code -- it's a little faster, and works on all machines likely to end up with one of these cards.

- Style cleanups suggested by Emil Velikov. Emil, let me know if there were things that I said I'd do but I forgot about (or if there are more things that you notice).

 src/gallium/drivers/nv50/Makefile.sources |   5 +-
 src/gallium/drivers/nv50/nv50_context.c   |  13 +-
 src/gallium/drivers/nv50/nv50_context.h   |  24 +
 src/gallium/drivers/nv50/nv50_miptree.c   |  27 +
 src/gallium/drivers/nv50/nv50_resource.h  |   1 +
 src/gallium/drivers/nv50/nv50_screen.c    |  13 +-
 src/gallium/drivers/nv50/nv50_winsys.h    |   4 +
 src/gallium/drivers/nv50/nv84_video.c     | 793 ++++++++++++++++++++++++++++++
 src/gallium/drivers/nv50/nv84_video.h     | 134 +++++
 src/gallium/drivers/nv50/nv84_video_bsp.c | 251 ++++++++++
 src/gallium/drivers/nv50/nv84_video_vp.c  | 547 +++++++++++++++++++++
 11 files changed, 1809 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/drivers/nv50/nv84_video.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video.h
 create mode 100644 src/gallium/drivers/nv50/nv84_video_bsp.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video_vp.c
src/gallium/drivers/nv50/Makefile.sources +4 -1
index 1092570..0fdac51 100644
@@ -13,7 +13,10 @@ C_SOURCES := \
 	nv50_program.c \
 	nv50_shader_state.c \
 	nv50_push.c \
-	nv50_query.c
+	nv50_query.c \
+	nv84_video.c \
+	nv84_video_bsp.c \
+	nv84_video_vp.c
 
 CODEGEN_NV50_SOURCES := \
 	codegen/nv50_ir.cpp \
src/gallium/drivers/nv50/nv50_context.c +12 -1
index 5781c4b..79a0473 100644
@@ -258,7 +258,18 @@ nv50_create(struct pipe_screen *pscreen, void *priv)
    draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
 #endif
 
-   nouveau_context_init_vdec(&nv50->base);
+   if (screen->base.device->chipset < 0x84) {
+      /* PMPEG */
+      nouveau_context_init_vdec(&nv50->base);
+   } else if (screen->base.device->chipset < 0x98 ||
+              screen->base.device->chipset == 0xa0) {
+      /* VP2 */
+      pipe->create_video_decoder = nv84_create_decoder;
+      pipe->create_video_buffer = nv84_video_buffer_create;
+   } else {
+      /* Unsupported, but need to init pointers. */
+      nouveau_context_init_vdec(&nv50->base);
+   }
 
    flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
 
src/gallium/drivers/nv50/nv50_context.h +24 -0
index 0a83131..b204cc8 100644
@@ -289,4 +289,28 @@ void nv50_vertex_arrays_validate(struct nv50_context *nv50);
 /* nv50_push.c */
 void nv50_push_vbo(struct nv50_context *, const struct pipe_draw_info *);
 
+/* nv84_video.c */
+struct pipe_video_decoder *
+nv84_create_decoder(struct pipe_context *context,
+                    enum pipe_video_profile profile,
+                    enum pipe_video_entrypoint entrypoint,
+                    enum pipe_video_chroma_format chroma_format,
+                    unsigned width, unsigned height,
+                    unsigned max_references,
+                    bool expect_chunked_decode);
+
+struct pipe_video_buffer *
+nv84_video_buffer_create(struct pipe_context *pipe,
+                         const struct pipe_video_buffer *template);
+
+int
+nv84_screen_get_video_param(struct pipe_screen *pscreen,
+                            enum pipe_video_profile profile,
+                            enum pipe_video_cap param);
+
+boolean
+nv84_screen_video_supported(struct pipe_screen *screen,
+                            enum pipe_format format,
+                            enum pipe_video_profile profile);
+
 #endif
src/gallium/drivers/nv50/nv50_miptree.c +27 -0
index 036f1c7..28be768 100644
@@ -239,6 +239,28 @@ nv50_miptree_init_layout_linear(struct nv50_miptree *mt, unsigned pitch_align)
 }
 
 static void
+nv50_miptree_init_layout_video(struct nv50_miptree *mt)
+{
+   const struct pipe_resource *pt = &mt->base.base;
+   const unsigned blocksize = util_format_get_blocksize(pt->format);
+
+   assert(pt->last_level == 0);
+   assert(mt->ms_x == 0 && mt->ms_y == 0);
+   assert(!util_format_is_compressed(pt->format));
+
+   mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
+
+   mt->level[0].tile_mode = 0x20;
+   mt->level[0].pitch = align(pt->width0 * blocksize, 64);
+   mt->total_size = align(pt->height0, 16) * mt->level[0].pitch * (mt->layout_3d ? pt->depth0 : 1);
+
+   if (pt->array_size > 1) {
+      mt->layer_stride = align(mt->total_size, NV50_TILE_SIZE(0x20));
+      mt->total_size = mt->layer_stride * pt->array_size;
+   }
+}
+
+static void
 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
 {
    struct pipe_resource *pt = &mt->base.base;
@@ -311,6 +333,11 @@ nv50_miptree_create(struct pipe_screen *pscreen,
       return NULL;
    }
 
+   if (unlikely(pt->flags & NV50_RESOURCE_FLAG_VIDEO)) {
+      nv50_miptree_init_layout_video(mt);
+      /* BO allocation done by client */
+      return pt;
+   } else
    if (bo_config.nv50.memtype != 0) {
       nv50_miptree_init_layout_tiled(mt);
    } else
src/gallium/drivers/nv50/nv50_resource.h +1 -0
index 6b92463..c520a72 100644
@@ -16,6 +16,7 @@ nv50_init_resource_functions(struct pipe_context *pcontext);
 void
 nv50_screen_init_resource_functions(struct pipe_screen *pscreen);
 
+#define NV50_RESOURCE_FLAG_VIDEO (NOUVEAU_RESOURCE_FLAG_DRV_PRIV << 0)
 
 #define NV50_TILE_SHIFT_X(m) 6
 #define NV50_TILE_SHIFT_Y(m) ((((m) >> 4) & 0xf) + 2)
src/gallium/drivers/nv50/nv50_screen.c +12 -1
index b6da303..86bad6b 100644
@@ -646,7 +646,18 @@ nv50_screen_create(struct nouveau_device *dev)
 
    nv50_screen_init_resource_functions(pscreen);
 
-   nouveau_screen_init_vdec(&screen->base);
+   if (screen->base.device->chipset < 0x84) {
+      /* PMPEG */
+      nouveau_screen_init_vdec(&screen->base);
+   } else if (screen->base.device->chipset < 0x98 ||
+              screen->base.device->chipset == 0xa0) {
+      /* VP2 */
+      screen->base.base.get_video_param = nv84_screen_get_video_param;
+      screen->base.base.is_video_format_supported = nv84_screen_video_supported;
+   } else {
+      /* Unsupported, but need to init pointers. */
+      nouveau_screen_init_vdec(&screen->base);
+   }
 
    ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0, 4096,
                         NULL, &screen->fence.bo);
src/gallium/drivers/nv50/nv50_winsys.h +4 -0
index 145ee70..e04247b 100644
@@ -60,6 +60,10 @@ PUSH_REFN(struct nouveau_pushbuf *push, struct nouveau_bo *bo, uint32_t flags)
 #define SUBC_COMPUTE(m) 6, (m)
 #define NV50_COMPUTE(n) SUBC_COMPUTE(NV50_COMPUTE_##n)
 
+/* These are expected to be on their own pushbufs */
+#define SUBC_BSP(m) 2, (m)
+#define SUBC_VP(m) 2, (m)
+
 
 static INLINE uint32_t
 NV50_FIFO_PKHDR(int subc, int mthd, unsigned size)
src/gallium/drivers/nv50/nv84_video.c new file +793 -0

793 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video.h new file +134 -0

134 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video_bsp.c new file +251 -0

251 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video_vp.c new file +547 -0

547 lines not on this page · raw message

Signature · 1 line
1.8.1.5

A message that is not in the archive (sent off-list or not yet received): 1372569420-18489-1-git-send-email-imirkin-FrUbXkNCsVf2fBVCVOL8/[email protected]

#8 lore raw

PATCH v3 1/1 nv50: H.264/MPEG2 decoding support via VP2, available on NV84-NV96, NVA0

Adds H.264 and MPEG2 codec support via VP2, using firmware from the blob. Acceleration is supported at the bitstream level for H.264 and IDCT level for MPEG2.

Known issues:
- H.264 interlaced doesn't render properly
- H.264 shows very occasional artifacts on a small fraction of videos
- MPEG2 + VDPAU shows frequent but small artifacts, which aren't there when using XvMC on the same videos

---

The kernel support for this is upstream now (in 3.11-rc1), might be nice to get this into Mesa 9.2.

v2 -> v3:

- Restricted MPEG2 optimizations to x86-64 (all of which include sse2)

- Fixed logic to be able to allow non-NV12 video surfaces when no video profile is specified.

v1 -> v2:

- Replaced SSE4.2 code with SSE2 code -- it's a little faster, and works on all machines likely to end up with one of these cards.

- Style cleanups suggested by Emil Velikov. Emil, let me know if there were things that I said I'd do but I forgot about (or if there are more things that you notice).

 src/gallium/drivers/nv50/Makefile.sources |   5 +-
 src/gallium/drivers/nv50/nv50_context.c   |  13 +-
 src/gallium/drivers/nv50/nv50_context.h   |  24 +
 src/gallium/drivers/nv50/nv50_miptree.c   |  27 +
 src/gallium/drivers/nv50/nv50_resource.h  |   1 +
 src/gallium/drivers/nv50/nv50_screen.c    |  13 +-
 src/gallium/drivers/nv50/nv50_winsys.h    |   4 +
 src/gallium/drivers/nv50/nv84_video.c     | 796 ++++++++++++++++++++++++++++++
 src/gallium/drivers/nv50/nv84_video.h     | 134 +++++
 src/gallium/drivers/nv50/nv84_video_bsp.c | 251 ++++++++++
 src/gallium/drivers/nv50/nv84_video_vp.c  | 550 +++++++++++++++++++++
 11 files changed, 1815 insertions(+), 3 deletions(-)
 create mode 100644 src/gallium/drivers/nv50/nv84_video.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video.h
 create mode 100644 src/gallium/drivers/nv50/nv84_video_bsp.c
 create mode 100644 src/gallium/drivers/nv50/nv84_video_vp.c
src/gallium/drivers/nv50/Makefile.sources +4 -1
index 1092570..0fdac51 100644
@@ -13,7 +13,10 @@ C_SOURCES := \
 	nv50_program.c \
 	nv50_shader_state.c \
 	nv50_push.c \
-	nv50_query.c
+	nv50_query.c \
+	nv84_video.c \
+	nv84_video_bsp.c \
+	nv84_video_vp.c
 
 CODEGEN_NV50_SOURCES := \
 	codegen/nv50_ir.cpp \
src/gallium/drivers/nv50/nv50_context.c +12 -1
index 5781c4b..79a0473 100644
@@ -258,7 +258,18 @@ nv50_create(struct pipe_screen *pscreen, void *priv)
    draw_set_rasterize_stage(nv50->draw, nv50_draw_render_stage(nv50));
 #endif
 
-   nouveau_context_init_vdec(&nv50->base);
+   if (screen->base.device->chipset < 0x84) {
+      /* PMPEG */
+      nouveau_context_init_vdec(&nv50->base);
+   } else if (screen->base.device->chipset < 0x98 ||
+              screen->base.device->chipset == 0xa0) {
+      /* VP2 */
+      pipe->create_video_decoder = nv84_create_decoder;
+      pipe->create_video_buffer = nv84_video_buffer_create;
+   } else {
+      /* Unsupported, but need to init pointers. */
+      nouveau_context_init_vdec(&nv50->base);
+   }
 
    flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_RD;
 
src/gallium/drivers/nv50/nv50_context.h +24 -0
index 0a83131..b204cc8 100644
@@ -289,4 +289,28 @@ void nv50_vertex_arrays_validate(struct nv50_context *nv50);
 /* nv50_push.c */
 void nv50_push_vbo(struct nv50_context *, const struct pipe_draw_info *);
 
+/* nv84_video.c */
+struct pipe_video_decoder *
+nv84_create_decoder(struct pipe_context *context,
+                    enum pipe_video_profile profile,
+                    enum pipe_video_entrypoint entrypoint,
+                    enum pipe_video_chroma_format chroma_format,
+                    unsigned width, unsigned height,
+                    unsigned max_references,
+                    bool expect_chunked_decode);
+
+struct pipe_video_buffer *
+nv84_video_buffer_create(struct pipe_context *pipe,
+                         const struct pipe_video_buffer *template);
+
+int
+nv84_screen_get_video_param(struct pipe_screen *pscreen,
+                            enum pipe_video_profile profile,
+                            enum pipe_video_cap param);
+
+boolean
+nv84_screen_video_supported(struct pipe_screen *screen,
+                            enum pipe_format format,
+                            enum pipe_video_profile profile);
+
 #endif
src/gallium/drivers/nv50/nv50_miptree.c +27 -0
index 036f1c7..28be768 100644
@@ -239,6 +239,28 @@ nv50_miptree_init_layout_linear(struct nv50_miptree *mt, unsigned pitch_align)
 }
 
 static void
+nv50_miptree_init_layout_video(struct nv50_miptree *mt)
+{
+   const struct pipe_resource *pt = &mt->base.base;
+   const unsigned blocksize = util_format_get_blocksize(pt->format);
+
+   assert(pt->last_level == 0);
+   assert(mt->ms_x == 0 && mt->ms_y == 0);
+   assert(!util_format_is_compressed(pt->format));
+
+   mt->layout_3d = pt->target == PIPE_TEXTURE_3D;
+
+   mt->level[0].tile_mode = 0x20;
+   mt->level[0].pitch = align(pt->width0 * blocksize, 64);
+   mt->total_size = align(pt->height0, 16) * mt->level[0].pitch * (mt->layout_3d ? pt->depth0 : 1);
+
+   if (pt->array_size > 1) {
+      mt->layer_stride = align(mt->total_size, NV50_TILE_SIZE(0x20));
+      mt->total_size = mt->layer_stride * pt->array_size;
+   }
+}
+
+static void
 nv50_miptree_init_layout_tiled(struct nv50_miptree *mt)
 {
    struct pipe_resource *pt = &mt->base.base;
@@ -311,6 +333,11 @@ nv50_miptree_create(struct pipe_screen *pscreen,
       return NULL;
    }
 
+   if (unlikely(pt->flags & NV50_RESOURCE_FLAG_VIDEO)) {
+      nv50_miptree_init_layout_video(mt);
+      /* BO allocation done by client */
+      return pt;
+   } else
    if (bo_config.nv50.memtype != 0) {
       nv50_miptree_init_layout_tiled(mt);
    } else
src/gallium/drivers/nv50/nv50_resource.h +1 -0
index 6b92463..c520a72 100644
@@ -16,6 +16,7 @@ nv50_init_resource_functions(struct pipe_context *pcontext);
 void
 nv50_screen_init_resource_functions(struct pipe_screen *pscreen);
 
+#define NV50_RESOURCE_FLAG_VIDEO (NOUVEAU_RESOURCE_FLAG_DRV_PRIV << 0)
 
 #define NV50_TILE_SHIFT_X(m) 6
 #define NV50_TILE_SHIFT_Y(m) ((((m) >> 4) & 0xf) + 2)
src/gallium/drivers/nv50/nv50_screen.c +12 -1
index 5c57aa2..2ed85d6 100644
@@ -645,7 +645,18 @@ nv50_screen_create(struct nouveau_device *dev)
 
    nv50_screen_init_resource_functions(pscreen);
 
-   nouveau_screen_init_vdec(&screen->base);
+   if (screen->base.device->chipset < 0x84) {
+      /* PMPEG */
+      nouveau_screen_init_vdec(&screen->base);
+   } else if (screen->base.device->chipset < 0x98 ||
+              screen->base.device->chipset == 0xa0) {
+      /* VP2 */
+      screen->base.base.get_video_param = nv84_screen_get_video_param;
+      screen->base.base.is_video_format_supported = nv84_screen_video_supported;
+   } else {
+      /* Unsupported, but need to init pointers. */
+      nouveau_screen_init_vdec(&screen->base);
+   }
 
    ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0, 4096,
                         NULL, &screen->fence.bo);
src/gallium/drivers/nv50/nv50_winsys.h +4 -0
index 145ee70..e04247b 100644
@@ -60,6 +60,10 @@ PUSH_REFN(struct nouveau_pushbuf *push, struct nouveau_bo *bo, uint32_t flags)
 #define SUBC_COMPUTE(m) 6, (m)
 #define NV50_COMPUTE(n) SUBC_COMPUTE(NV50_COMPUTE_##n)
 
+/* These are expected to be on their own pushbufs */
+#define SUBC_BSP(m) 2, (m)
+#define SUBC_VP(m) 2, (m)
+
 
 static INLINE uint32_t
 NV50_FIFO_PKHDR(int subc, int mthd, unsigned size)
src/gallium/drivers/nv50/nv84_video.c new file +796 -0

796 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video.h new file +134 -0

134 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video_bsp.c new file +251 -0

251 lines not on this page · raw message

src/gallium/drivers/nv50/nv84_video_vp.c new file +550 -0

550 lines not on this page · raw message

Signature · 1 line
1.8.1.5