xpra icon
Bug tracker and wiki

This bug tracker and wiki are being discontinued
please use https://github.com/Xpra-org/xpra instead.


Ticket #370: add-libva-stub.patch

File add-libva-stub.patch, 10.9 KB (added by Antoine Martin, 8 years ago)

stub libva files to get going

  • xpra/codecs/enc_libva

  • xpra/codecs/enc_libva/__init__.py

    Property changes on: xpra/codecs/enc_libva
    ___________________________________________________________________
    Added: svn:ignore
    ## -0,0 +1 ##
    +encoder.c
     
     1# This file is part of Xpra.
     2# Copyright (C) 2012, 2013 Antoine Martin <antoine@devloop.org.uk>
     3# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
     4# later version. See the file COPYING for details.
  • xpra/codecs/enc_libva/encoder.pyx

     
     1# This file is part of Xpra.
     2# Copyright (C) 2012, 2013 Antoine Martin <antoine@devloop.org.uk>
     3# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
     4# later version. See the file COPYING for details.
     5
     6import os
     7from libc.stdlib cimport free
     8
     9from xpra.codecs.codec_constants import get_subsampling_divs
     10
     11cdef extern from "string.h":
     12    void * memcpy ( void * destination, void * source, size_t num )
     13    void * memset ( void * ptr, int value, size_t num )
     14
     15cdef extern from *:
     16    ctypedef unsigned long size_t
     17
     18cdef extern from "Python.h":
     19    ctypedef int Py_ssize_t
     20    ctypedef object PyObject
     21    ctypedef void** const_void_pp "const void**"
     22    int PyObject_AsReadBuffer(object obj, void ** buffer, Py_ssize_t * buffer_len) except -1
     23
     24ctypedef unsigned char uint8_t
     25ctypedef void valib_ctx
     26cdef extern from "libvalib.h":
     27    valib_ctx* init_encoder(int width, int height)
     28    void clean_encoder(valib_ctx *context)
     29    int compress_image(valib_ctx *ctx, uint8_t *pic_in, uint8_t **out, int *outsz) nogil
     30
     31cdef extern from "X11/Xlib.h":
     32    ctypedef struct Display:
     33        pass
     34
     35
     36cdef class Encoder:
     37    cdef int frames
     38    cdef int supports_options
     39    cdef valib_ctx *context
     40    cdef int width
     41    cdef int height
     42
     43    def init_context(self, int width, int height, options):    #@DuplicatedSignature
     44        self.width = width
     45        self.height = height
     46        self.frames = 0
     47        self.supports_options = int(options.get("client_options", False))
     48        self.context = init_encoder(width, height)
     49
     50    def is_closed(self):
     51        return self.context==NULL
     52
     53    def __dealloc__(self):
     54        self.clean()
     55
     56    def get_width(self):
     57        return self.width
     58
     59    def get_height(self):
     60        return self.height
     61
     62    def get_type(self):
     63        return  "va/x264"
     64
     65    def clean(self):                        #@DuplicatedSignature
     66        if self.context!=NULL:
     67            clean_encoder(self.context)
     68            self.context = NULL
     69
     70    def get_client_options(self, options):
     71        client_options = {
     72                "frame" : self.frames
     73                }
     74        return  client_options
     75
     76    def compress_image(self, input, rowstride, options):
     77        cdef uint8_t *pic_in = NULL
     78        cdef const uint8_t *pic_buf = NULL
     79        cdef Py_ssize_t pic_buf_len = 0
     80        assert self.context!=NULL
     81        #colourspace conversion with gil held:
     82        PyObject_AsReadBuffer(input, <const_void_pp> &pic_buf, &pic_buf_len)
     83        pic_in = NULL
     84        assert pic_in!=NULL, "colourspace conversion failed"
     85        return self.do_compress_image(pic_in), self.get_client_options(options)
     86
     87    cdef do_compress_image(self, uint8_t *pic_in):
     88        #actual compression (no gil):
     89        cdef int i
     90        cdef uint8_t *cout
     91        cdef int coutsz
     92        with nogil:
     93            i = compress_image(self.context, pic_in, &cout, &coutsz)
     94        if i!=0:
     95            return None
     96        coutv = (<char *>cout)[:coutsz]
     97        self.frames += 1
     98        return  coutv
  • xpra/codecs/enc_libva/libvalib.c

     
     1/* This file is part of Xpra.
     2 * Copyright (C) 2012 Serviware (Arthur Huillet, <ahuillet@serviware.com>)
     3 * Copyright (C) 2012, 2013 Antoine Martin <antoine@devloop.org.uk>
     4 * Xpra is released under the terms of the GNU GPL v2, or, at your option, any
     5 * later version. See the file COPYING for details.
     6 */
     7
     8#include <stdio.h>
     9#include <stdlib.h>
     10#include <string.h>
     11#include <sys/types.h>
     12#include <sys/stat.h>
     13#include <fcntl.h>
     14#include <stdint.h>
     15#include <inttypes.h>
     16
     17#include "libvalib.h"
     18#include <va/va.h>
     19#include <va/va_x11.h>
     20#include <X11/Xlib.h>
     21
     22struct valib_ctx {
     23        Display *x11_dpy;
     24        VADisplay va_dpy;
     25
     26        VAConfigID config;
     27        VAContextID context;
     28        VASurfaceID surface_id;
     29        VABufferID seq_param;      /* Sequence parameters */
     30        VABufferID pic_param;      /* Picture parameters */
     31        VABufferID slice_param;  /* Slice parameters */
     32        VABufferID coded_buf;      /* Output buffer, encoded data */
     33        int width;
     34        int height;
     35
     36};
     37
     38//      if (va_status != VA_STATUS_SUCCESS) {
     39
     40/**
     41 * Configure values that will not change during the lifetime of the encoder.
     42 */
     43void configure_encoder(struct valib_ctx *ctx, int width, int height)
     44{
     45        ctx->width = width;
     46        ctx->height = height;
     47}
     48
     49/**
     50 * Actually initialize the encoder.
     51 * This may be called more than once if required, ie:
     52 * - if the dimensions change,
     53 * - if the csc mode changes.
     54 */
     55int do_init_encoder(struct valib_ctx *ctx)
     56{
     57        VAStatus va_status;
     58        int va_major_ver, va_minor_ver;
     59        VAEntrypoint entrypoints[5];
     60        int num_entrypoints, entrypoint;
     61        VAConfigAttrib attrib[2];
     62
     63        /* Open local X11 display and VA display */
     64        ctx->x11_dpy = XOpenDisplay(":0.0");
     65        if (!ctx->x11_dpy) {
     66                fprintf(stderr, "Could not open display :0.0 for VA encoding\n");
     67                return -1;
     68        }
     69        ctx->va_dpy = vaGetDisplay(ctx->x11_dpy);
     70        va_status = vaInitialize(ctx->va_dpy, &va_major_ver, &va_minor_ver);
     71        if (va_status!=VA_STATUS_SUCCESS) {
     72                fprintf(stderr, "Could not initialize va display: %i\n", va_status);
     73                return -1;
     74        }
     75
     76        /* Check for Slice entrypoint */
     77        vaQueryConfigEntrypoints(ctx->va_dpy, VAProfileH264Baseline, entrypoints,
     78                                                         &num_entrypoints);
     79
     80        for (entrypoint = 0; entrypoint < num_entrypoints; entrypoint++) {
     81                if (entrypoints[entrypoint] == VAEntrypointEncSlice) {
     82                        break;
     83                }
     84        }
     85
     86        if (entrypoint == num_entrypoints) {
     87                fprintf(stderr, "Could not find Encoder Slice entrypoint\n");
     88                return -1;
     89        }
     90
     91        /* Set encode pipeline configuration */
     92        attrib[0].type = VAConfigAttribRTFormat;
     93        attrib[1].type = VAConfigAttribRateControl;
     94        vaGetConfigAttributes(ctx->va_dpy, VAProfileH264Baseline,
     95                                                  VAEntrypointEncSlice, &attrib[0], 2);
     96
     97        //VA_RT_FORMAT_YUV422 or VA_RT_FORMAT_YUV444
     98        if ((attrib[0].value & VA_RT_FORMAT_YUV420) == 0) {
     99                fprintf(stderr, "YUV420 format is not supported by VA encoder\n");
     100                return -1;
     101        }
     102        if ((attrib[1].value & VA_RC_VBR) == 0) {
     103                fprintf(stderr, "VBR mode is not supported by VA encoder\n");
     104                return -1;
     105        }
     106
     107        attrib[0].value = VA_RT_FORMAT_YUV420;
     108        attrib[1].value = VA_RC_VBR;
     109        //VAProfileH264Main, VAProfileH264High
     110        va_status = vaCreateConfig(ctx->va_dpy, VAProfileH264Baseline,
     111                                                           VAEntrypointEncSlice, &attrib[0], 2,
     112                                                           &ctx->config);
     113        if (va_status!=VA_STATUS_SUCCESS) {
     114                fprintf(stderr, "Could not create va config: %i\n", va_status);
     115                return -1;
     116        }
     117        return 0;
     118}
     119
     120struct valib_ctx *init_encoder(int width, int height)
     121{
     122        struct valib_ctx *ctx = malloc(sizeof(struct valib_ctx));
     123        if (ctx==NULL)
     124                return NULL;
     125        memset(ctx, 0, sizeof(struct valib_ctx));
     126        configure_encoder(ctx, width, height);
     127        if (do_init_encoder(ctx)) {
     128                free(ctx);
     129                return NULL;
     130        }
     131        return ctx;
     132}
     133
     134
     135void clean_encoder(struct valib_ctx *ctx)
     136{
     137        do_clean_encoder(ctx);
     138        free(ctx);
     139}
     140void do_clean_encoder(struct valib_ctx *ctx)
     141{
     142        if (ctx->config != VA_INVALID_ID) {
     143                vaDestroyConfig(ctx->va_dpy, ctx->config);
     144            ctx->config = VA_INVALID_ID;
     145        }
     146        if (ctx->va_dpy) {
     147                vaTerminate(ctx->va_dpy);
     148                ctx->va_dpy = NULL;
     149        }
     150        if (ctx->x11_dpy) {
     151                XCloseDisplay(ctx->x11_dpy);
     152                ctx->x11_dpy = NULL;
     153        }
     154}
     155
     156int compress_image(struct valib_ctx *ctx, uint8_t *pic_in, uint8_t **out, int *outsz)
     157{
     158        return 0;
     159}
  • xpra/codecs/enc_libva/libvalib.h

     
     1/* This file is part of Xpra.
     2 * Copyright (C) 2012 Serviware (Arthur Huillet, <ahuillet@serviware.com>)
     3 * Copyright (C) 2012, 2013 Antoine Martin <antoine@devloop.org.uk>
     4 * Parti is released under the terms of the GNU GPL v2, or, at your option, any
     5 * later version. See the file COPYING for details.
     6 */
     7
     8#include <stdint.h>
     9#include <inttypes.h>
     10
     11/** Opaque structure - "context". You must have a context to encode images of a given size */
     12struct valib_ctx;
     13
     14/** Create an encoding context for images of a given size.  */
     15struct valib_ctx *init_encoder(int width, int height);
     16
     17void configure_encoder(struct valib_ctx *ctx, int width, int height);
     18
     19int do_init_encoder(struct valib_ctx *ctx);
     20
     21void clean_encoder(struct valib_ctx *ctx);
     22
     23void do_clean_encoder(struct valib_ctx *ctx);
     24
     25
     26/** Compress an image using the given context.
     27 @param in: the input image
     28 @param out: the compressed output
     29 @param outsz: Output size
     30 @param quality_override: Desired quality setting (0 to 100), -1 to use current settings.
     31*/
     32int compress_image(struct valib_ctx *ctx, uint8_t *pic_in, uint8_t **out, int *outsz);
     33
  • setup.py

     
    133133
    134134
    135135
     136enc_libva_ENABLED = True
     137
     138
     139
    136140csc_nvcuda_ENABLED = False
    137141
    138142
    139143
    140144#allow some of these flags to be modified on the command line:
    141 SWITCHES = ("enc_x264", "nvenc", "dec_avcodec", "csc_swscale", "csc_nvcuda", "vpx", "webp", "rencode", "clipboard",
     145SWITCHES = ("enc_x264", "nvenc", "dec_avcodec", "enc_libva", "csc_swscale", "csc_nvcuda",
     146            "vpx", "webp", "rencode", "clipboard",
    142147            "server", "client", "x11",
    143148            "gtk2", "gtk3", "qt4",
    144149            "sound", "cyxor", "cymaths", "opengl", "argb",
     
    942947
    943948
    944949
     950toggle_packages(enc_x264_ENABLED, "xpra.codecs.enc_libva")
     951if enc_libva_ENABLED:
     952    libva_pkgconfig = pkgconfig("libva", "x11")
     953    cython_add(Extension("xpra.codecs.enc_libva.encoder",
     954                ["xpra/codecs/enc_libva/encoder.pyx", "xpra/codecs/enc_libva/libvalib.c"],
     955                **libva_pkgconfig), min_version=(0, 16))
     956
    945957toggle_packages(enc_x264_ENABLED, "xpra.codecs.enc_x264")
    946958if enc_x264_ENABLED:
    947959    x264_pkgconfig = pkgconfig("x264")