xpra icon
Bug tracker and wiki

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


Ticket #110: xpra-x264-no-gil.patch

File xpra-x264-no-gil.patch, 5.1 KB (added by Antoine Martin, 9 years ago)

allows us to drop the gil when calling the compress part of x264 encoding

  • xpra/x264/x264lib.c

     
    111111}
    112112
    113113#ifndef _WIN32
    114 int compress_image(struct x264lib_ctx *ctx, const uint8_t *in, int stride, uint8_t **out, int *outsz)
     114x264_picture_t* csc_image(struct x264lib_ctx *ctx, const uint8_t *in, int stride)
    115115{
    116116        if (!ctx->encoder || !ctx->rgb2yuv)
    117                 return 1;
     117                return NULL;
    118118
    119         x264_picture_t pic_in, pic_out;
    120         x264_picture_alloc(&pic_in, X264_CSP_I420, ctx->width, ctx->height);
     119        x264_picture_t *pic_in = malloc(sizeof(x264_picture_t));
     120        x264_picture_alloc(pic_in, X264_CSP_I420, ctx->width, ctx->height);
    121121
    122122        /* Colorspace conversion (RGB -> I420) */
    123         sws_scale(ctx->rgb2yuv, &in, &stride, 0, ctx->height, pic_in.img.plane, pic_in.img.i_stride);
     123        sws_scale(ctx->rgb2yuv, &in, &stride, 0, ctx->height, pic_in->img.plane, pic_in->img.i_stride);
     124        return pic_in;
     125}
    124126
     127void free_csc_image(x264_picture_t *image)
     128{
     129        x264_picture_clean(image);
     130        free(image);
     131}
     132
     133int compress_image(struct x264lib_ctx *ctx, x264_picture_t *pic_in, uint8_t **out, int *outsz)
     134{
     135        if (!ctx->encoder || !ctx->rgb2yuv) {
     136                free_csc_image(pic_in);
     137                return 1;
     138        }
     139
     140        x264_picture_t pic_out;
     141
    125142        /* Encoding */
    126         pic_in.i_pts = 1;
     143        pic_in->i_pts = 1;
    127144
    128145        x264_nal_t* nals;
    129146        int i_nals;
    130         int frame_size = x264_encoder_encode(ctx->encoder, &nals, &i_nals, &pic_in, &pic_out);
    131         if (frame_size >= 0) {
    132                 /* Do not free that! */
    133                 *out = nals[0].p_payload;
    134                 *outsz = frame_size;
    135         } else {
    136                 fprintf(stderr, "Problem\n");
    137                 x264_picture_clean(&pic_in);
     147        int frame_size = x264_encoder_encode(ctx->encoder, &nals, &i_nals, pic_in, &pic_out);
     148        if (frame_size < 0) {
     149                fprintf(stderr, "Problem during x264_encoder_encode: frame_size is invalid!\n");
     150                free_csc_image(pic_in);
    138151                return 2;
    139152        }
    140  
    141         x264_picture_clean(&pic_in);
     153        /* Do not clean that! */
     154        *out = nals[0].p_payload;
     155        *outsz = frame_size;
     156        x264_picture_clean(pic_in);
    142157        return 0;
    143158}
    144159#else
    145 int compress_image(struct x264lib_ctx *ctx, const uint8_t *in, int stride, uint8_t **out, int *outsz)
     160x264_picture_t* csc_image(struct x264lib_ctx *ctx, const uint8_t *in, int stride) {
     161        return  NULL;
     162}
     163int compress_image(struct x264lib_ctx *ctx, x264_picture_t *pic_in, uint8_t **out, int *outsz)
    146164{
    147165        return 1;
    148166}
  • xpra/x264/codec.pyx

     
    1616
    1717ctypedef unsigned char uint8_t
    1818ctypedef void x264lib_ctx
     19ctypedef void x264_picture_t
    1920cdef extern from "x264lib.h":
    2021    x264lib_ctx* init_encoder(int width, int height)
    2122    void clean_encoder(x264lib_ctx *context)
    22     int compress_image(x264lib_ctx *context, uint8_t *input, int stride, uint8_t **out, int *outsz)
     23    x264_picture_t* csc_image(x264lib_ctx *ctx, uint8_t *input, int stride)
     24    void free_csc_image(x264_picture_t *image)
     25    int compress_image(x264lib_ctx *ctx, x264_picture_t *pic_in, uint8_t **out, int *outsz) nogil
     26
    2327    x264lib_ctx* init_decoder(int width, int height)
    2428    void clean_decoder(x264lib_ctx *context)
    2529    int decompress_image(x264lib_ctx *context, uint8_t *input, int size, uint8_t **out, int *outsize, int *outstride)
     
    99103            self.context = NULL
    100104
    101105    def compress_image(self, input, rowstride):
     106        cdef x264_picture_t *pic_in = NULL
    102107        cdef uint8_t *cout
    103108        cdef int coutsz
    104109        cdef uint8_t *buf = <uint8_t *> 0
    105110        cdef Py_ssize_t buf_len = 0
    106111        assert self.context!=NULL
    107112        PyObject_AsReadBuffer(input, <void **>&buf, &buf_len)
    108         i = compress_image(self.context, buf, rowstride, &cout, &coutsz)
     113
     114        pic_in = csc_image(self.context, buf, rowstride)
     115        assert pic_in!=NULL
     116        with nogil:
     117            i = compress_image(self.context, pic_in, &cout, &coutsz)
    109118        if i!=0:
    110119            return i, 0, ""
    111120        coutv = (<char *>cout)[:coutsz]
  • xpra/x264/x264lib.h

     
    55#include "stdint.h"
    66#endif
    77
     8#ifndef _WIN32
     9#include <x264.h>
     10#else
     11typedef void x264_t;
     12#define inline __inline
     13#endif
     14
    815/** Opaque structure - "context". You must have a context to encode images of a given size */
    916struct x264lib_ctx;
    1017
     
    2027/** Cleanup decoding context. Must be freed after calling this function. */
    2128void clean_decoder(struct x264lib_ctx *);
    2229
     30
     31x264_picture_t* csc_image(struct x264lib_ctx *ctx, const uint8_t *in, int stride);
     32
    2333/** Compress an image using the given context.
    2434 @param in: Input buffer, format is packed RGB24.
    2535 @param stride: Input stride (size is taken from context).
     
    2737 next call to compress_image.
    2838 @param outsz: Output size
    2939*/
    30 int compress_image(struct x264lib_ctx *, const uint8_t *in, int stride, uint8_t **out, int *outsz);
     40int compress_image(struct x264lib_ctx *ctx, x264_picture_t *pic_in, uint8_t **out, int *outsz);
    3141
    3242/** Decompress an image using the given context.
    3343 @param in: Input buffer, format is H264.