Ticket #147: xpra-vpx-gl.patch
File xpra-vpx-gl.patch, 8.5 KB (added by , 9 years ago) |
---|
-
xpra/vpx/codec.pyx
8 8 cdef extern from "Python.h": 9 9 ctypedef int Py_ssize_t 10 10 ctypedef object PyObject 11 ctypedef void** const_void_pp "const void**" 11 12 int PyObject_AsReadBuffer(object obj, void ** buffer, Py_ssize_t * buffer_len) except -1 12 13 13 14 ctypedef unsigned char uint8_t … … 16 17 cdef extern from "vpxlib.h": 17 18 vpx_codec_ctx_t* init_encoder(int width, int height) 18 19 void clean_encoder(vpx_codec_ctx_t *context) 19 vpx_image_t* csc_image (vpx_codec_ctx_t *ctx, uint8_t *input, int stride)20 vpx_image_t* csc_image_rgb2yuv(vpx_codec_ctx_t *ctx, uint8_t *input, int stride) 20 21 int compress_image(vpx_codec_ctx_t *ctx, vpx_image_t *image, uint8_t **out, int *outsz) nogil 21 22 22 23 vpx_codec_ctx_t* init_decoder(int width, int height) 23 24 void clean_decoder(vpx_codec_ctx_t *context) 24 int decompress_image(vpx_codec_ctx_t *context, uint8_t *input, int size, uint8_t **out, int *outsize, int *outstride) 25 int decompress_image(vpx_codec_ctx_t *ctx, uint8_t *input, int size, uint8_t *(*out)[4], int *outsize, int (*outstride)[4]) 26 int csc_image_yuv2rgb(vpx_codec_ctx_t *ctx, uint8_t *input[3], int stride[3], uint8_t **out, int *outsz, int *outstride) nogil 25 27 26 28 27 29 ENCODERS = {} … … 64 66 clean_decoder(self.context) 65 67 self.context = NULL 66 68 69 def decompress_image_to_yuv(self, input): 70 cdef uint8_t *dout[4] 71 cdef int outsize 72 cdef int outstrides[4] 73 cdef unsigned char * buf = <uint8_t *> 0 74 cdef Py_ssize_t buf_len = 0 75 assert self.context!=NULL 76 PyObject_AsReadBuffer(input, <const_void_pp> &buf, &buf_len) 77 i = decompress_image(self.context, buf, buf_len, &dout, &outsize, &outstrides) 78 if i!=0: 79 return i, [0, 0, 0], ["", "", ""] 80 doutvY = (<char *>dout[0])[:self.height * outstrides[0]] 81 doutvU = (<char *>dout[1])[:self.height * outstrides[1]] 82 doutvV = (<char *>dout[2])[:self.height * outstrides[2]] 83 out = [doutvY, doutvU, doutvV] 84 strides = [outstrides[0], outstrides[1], outstrides[2]] 85 return i, strides, out 86 67 87 def decompress_image_to_rgb(self, input): 88 cdef uint8_t *yuvplanes[4] 68 89 cdef uint8_t *dout 69 90 cdef int outsize 91 cdef int yuvstrides[4] 70 92 cdef int outstride 71 93 cdef unsigned char * buf = <uint8_t *> 0 72 94 cdef Py_ssize_t buf_len = 0 95 cdef int i 73 96 assert self.context!=NULL 74 97 assert self.last_image==NULL 75 PyObject_AsReadBuffer(input, < void **>&buf, &buf_len)76 i = decompress_image(self.context, buf, buf_len, &dout, &outsize, &outstride)77 self.last_image = dout98 PyObject_AsReadBuffer(input, <const_void_pp> &buf, &buf_len) 99 print("vpx: before decompress_image") 100 i = decompress_image(self.context, buf, buf_len, &yuvplanes, &outsize, &yuvstrides) 78 101 if i!=0: 79 102 return i, 0, "" 103 for x in xrange(0,4): 104 print("vpx stride[%s]=%s" % (x, yuvstrides[x])) 105 print("vpx out[%s]=%s" % (x, yuvplanes[x][0])) 106 with nogil: 107 i = csc_image_yuv2rgb(self.context, yuvplanes, yuvstrides, &dout, &outsize, &outstride) 108 print("vpx: csc returned %s" % i) 109 if i!=0: 110 return i, 0, "" 111 self.last_image = dout 80 112 doutv = (<char *>dout)[:outsize] 81 113 return i, outstride, doutv 82 114 … … 104 136 cdef Py_ssize_t buf_len = 0 105 137 assert self.context!=NULL 106 138 #colourspace conversion with gil held: 107 PyObject_AsReadBuffer(input, < void **>&buf, &buf_len)108 pic_in = csc_image (self.context, buf, rowstride)139 PyObject_AsReadBuffer(input, <const_void_pp> &buf, &buf_len) 140 pic_in = csc_image_rgb2yuv(self.context, buf, rowstride) 109 141 assert pic_in!=NULL, "colourspace conversion failed" 110 142 #actual compression (no gil): 111 143 with nogil: -
xpra/vpx/vpxlib.c
30 30 #define fourcc 0x30385056 31 31 #define IVF_FILE_HDR_SZ (32) 32 32 #include <libswscale/swscale.h> 33 #include <libavcodec/avcodec.h> 33 34 34 35 struct vpx_context { 35 36 vpx_codec_ctx_t codec; … … 99 100 free(ctx); 100 101 } 101 102 102 vpx_image_t* csc_image (struct vpx_context *ctx, const uint8_t *in, int stride)103 vpx_image_t* csc_image_rgb2yuv(struct vpx_context *ctx, const uint8_t *in, int stride) 103 104 { 104 105 vpx_image_t *image = malloc(sizeof(vpx_image_t)); 105 106 if (!vpx_img_alloc(image, VPX_IMG_FMT_I420, ctx->width, ctx->height, 1)) { … … 144 145 return 0; 145 146 } 146 147 147 int decompress_image(struct vpx_context *ctx, uint8_t *in, int size, uint8_t **out, int *outsize, int *outstride)148 int csc_image_yuv2rgb(struct vpx_context *ctx, uint8_t *in[4], const int stride[4], uint8_t **out, int *outsz, int *outstride) 148 149 { 150 AVPicture pic; 151 152 if (!ctx->yuv2rgb) 153 return 1; 154 155 avpicture_fill(&pic, malloc(ctx->height * ctx->width * 3), PIX_FMT_RGB24, ctx->width, ctx->height); 156 157 sws_scale(ctx->yuv2rgb, (const uint8_t * const*) in, stride, 0, ctx->height, pic.data, pic.linesize); 158 159 /* Output (must be freed!) */ 160 *out = pic.data[0]; 161 *outsz = pic.linesize[0] * ctx->height; 162 *outstride = pic.linesize[0]; 163 164 return 0; 165 } 166 167 int decompress_image(struct vpx_context *ctx, uint8_t *input, int size, uint8_t *(*out)[4], int *outsize, int (*outstride)[4]) 168 { 149 169 vpx_image_t *img; 150 170 int frame_sz = size; 151 171 vpx_codec_iter_t iter = NULL; 152 uint8_t* frame = in;153 int outstrides[4];154 uint8_t* outs[4];155 172 int stride = 0; 156 173 int i = 0; 157 174 158 if (vpx_codec_decode(&ctx->codec, frame, frame_sz, NULL, 0)) {175 if (vpx_codec_decode(&ctx->codec, (uint8_t *) input, frame_sz, NULL, 0)) { 159 176 codec_error(&ctx->codec, "vpx_codec_decode"); 160 177 return -1; 161 178 } … … 164 181 codec_error(&ctx->codec, "vpx_codec_get_frame"); 165 182 return -1; 166 183 } 167 for (i=0; i<4; i++) 184 for (i=0; i<4; i++) { 185 *outstride[i] = img->stride[i]; 186 *out[i] = img->planes[i]; 168 187 stride += img->stride[i]; 188 } 169 189 *outsize = stride * img->h; 170 171 *out = malloc(*outsize);172 for (i=0; i<4; i++) {173 outstrides[i] = img->w*3;174 outs[i] = *out;175 }176 sws_scale(ctx->yuv2rgb, img->planes, img->stride, 0, img->h, outs, outstrides);177 stride = 0;178 for (i=0; i<4; i++)179 stride += img->stride[i];180 *outstride = stride;181 190 return 0; 182 191 } -
xpra/vpx/vpxlib.h
21 21 /** Cleanup decoding context. Must be freed after calling this function. */ 22 22 void clean_decoder(struct vpx_context *ctx); 23 23 24 /** Colo urspace conversion.24 /** Colorspace conversion. 25 25 * Note: you must call compress_image to free the image buffer. 26 26 @param in: Input buffer, format is packed RGB24. 27 27 @param stride: Input stride (size is taken from context). 28 28 @return: the converted picture. 29 29 */ 30 vpx_image_t * csc_image(struct vpx_context *ctx, const uint8_t *in, int stride);30 vpx_image_t *csc_image_rgb2yuv(struct vpx_context *ctx, const uint8_t *in, int stride); 31 31 32 /** Colorspace conversion. 33 * Note: you must call compress_image to free the image buffer. 34 @param in: Input picture (3 planes). 35 @param stride: Input strides (3 planes). 36 @param out: Will be set to point to the output data in packed RGB24 format. Must be freed after use by calling free(). 37 @param outsz: Will be set to the size of the output buffer. 38 @param outstride: Output stride. 39 @return non zero on error. 40 */ 41 int csc_image_yuv2rgb(struct vpx_context *ctx, uint8_t *in[3], const int stride[3], uint8_t **out, int *outsz, int *outstride); 42 32 43 /** Compress an image using the given context. 33 44 @param pic_in: the input image, as returned by csc_image 34 45 @param out: Will be set to point to the output data. This output buffer MUST NOT BE FREED and will be erased on the … … 40 51 /** Decompress an image using the given context. 41 52 @param in: Input buffer, format is H264. 42 53 @param size: Input size. 43 @param out: Will be set to point to the output data in RGB24 format.44 @param outstride: Output stride .54 @param out: Will be filled to point to the output data in planar YUV420 format (4 planes). 55 @param outstride: Output strides. (4 planes) 45 56 */ 46 int decompress_image(struct vpx_context *ctx, uint8_t *in, int size, uint8_t * *out, int *outsize, int *outstride);57 int decompress_image(struct vpx_context *ctx, uint8_t *in, int size, uint8_t *(*out)[4], int *outsize, int (*outstride)[4]);