xpra icon
Bug tracker and wiki

Ticket #281: x264-ifdef-encoder-win32-v4.patch

File x264-ifdef-encoder-win32-v4.patch, 8.0 KB (added by Antoine Martin, 7 years ago)

updated patch that builds ok but does not link on win32

  • xpra/x264/codec.pyx

     
    3333    ctypedef void** const_void_pp "const void**"
    3434    int PyObject_AsReadBuffer(object obj, void ** buffer, Py_ssize_t * buffer_len) except -1
    3535
     36#needed for win32?
     37cdef extern from "stdint.h":
     38    pass
     39cdef extern from "inttypes.h":
     40    pass
     41
    3642ctypedef unsigned char uint8_t
    3743ctypedef void x264lib_ctx
    3844ctypedef void x264_picture_t
  • xpra/x264/x264lib.c

     
    1212#include <sys/stat.h>
    1313#include <fcntl.h>
    1414#include <math.h>
     15#include <malloc.h>
     16#include <stdint.h>
     17#include <inttypes.h>
    1518
     19#ifdef _WIN32
     20#define _STDINT_H
     21#endif
     22
     23#include "x264lib.h"
     24#include <x264.h>
     25#include <libswscale/swscale.h>
     26#include <libavcodec/avcodec.h>
     27#include <libavutil/mem.h>
     28
     29
    1630//not honoured on MS Windows:
    1731#define MEMALIGN 1
    1832//not honoured on OSX:
     
    2135//(ie: when not supported by the library we build against)
    2236#define SUPPORT_CSC_MODES 1
    2337
    24 #ifdef _WIN32
    25 #include <malloc.h>
    26 #include "stdint.h"
    27 #include "inttypes.h"
    28 #else
    29 #include <stdint.h>
    30 #include <unistd.h>
    31 #endif
    3238
    33 #ifdef _WIN32
    34 typedef void x264_t;
    35 #define inline __inline
    36 #else
    37 #include <x264.h>
    38 #endif
    39 
    40 #include <libswscale/swscale.h>
    41 #include <libavcodec/avcodec.h>
    42 #include <libavutil/mem.h>
    43 #include "x264lib.h"
    44 
    4539//beware that these macros may evaluate a or b twice!
    4640//ie: do not use them for something like: MAX(i++, N)
    4741#define MAX(a,b) ((a) > (b) ? a : b)
     
    108102        return ctx->speed;
    109103}
    110104
    111 #ifndef _WIN32
    112105//Given a quality percentage (0 to 100),
    113106//return the x264 quality constant to use
    114107float get_x264_quality(int pct) {
    115         return  roundf(50.0 - (MIN(100, MAX(0, pct)) * 49.0 / 100.0));
     108        return  50.0f - (MIN(100, MAX(0, pct)) * 49.0f / 100.0f);
    116109}
    117110
    118111//Given a quality percentage (0 to 100),
     
    169162                return -1;
    170163        }
    171164}
    172 #endif
    173165
    174166//Given a csc colour sampling constant,
    175167//return our own generic csc constant (see codec_constants.py)
     
    211203const int DEFAULT_I422_MIN_QUALITY = 80;
    212204const int DEFAULT_I444_MIN_QUALITY = 90;
    213205
    214 #ifndef _WIN32
    215206//Given a quality percentage (0 to 100)
    216207//return the profile to use
    217208//IMPORTANT: changes here must be reflected in get_x264_colour_sampling
     
    232223const char *get_valid_profile(const char* csc_mode, const char *profile, const char *profiles[], const char *default_profile)
    233224{
    234225        //printf("get_valid_profile(%s, %s, %p, %s)\n", csc_mode, profile, profiles, default_profile);
     226        int i = 0;
    235227        if (profile==NULL)
    236228                return  default_profile;
    237         int i = 0;
    238229        while (profiles[i]!=NULL)
    239230        {
    240231                if (strcmp(profiles[i], profile)==0) {
     
    312303 */
    313304void do_init_encoder(struct x264lib_ctx *ctx)
    314305{
     306        x264_param_t param;
    315307        ctx->colour_sampling = get_x264_colour_sampling(ctx, ctx->quality);
    316308        ctx->x264_quality = get_x264_quality(ctx->quality);
    317309        ctx->csc_format = get_csc_format_for_x264_format(ctx->colour_sampling);
     
    321313        ctx->csc_algo = get_csc_algo_for_quality(ctx->quality);
    322314        //printf("do_init_encoder(%p, %i, %i, %i, %i) colour_sampling=%i, initial x264_quality=%f, initial profile=%s\n", ctx, ctx->width, ctx->height, ctx->quality, ctx->supports_csc_option, ctx->colour_sampling, ctx->x264_quality, ctx->profile);
    323315
    324         x264_param_t param;
    325316        x264_param_default_preset(&param, ctx->preset, "zerolatency");
    326317        param.i_threads = 1;
    327318        param.i_width = ctx->width;
     
    375366        }
    376367}
    377368
    378 #else
    379 struct x264lib_ctx *init_encoder(int width, int height,
    380                 int initial_quality, int initial_speed,
    381                 int supports_csc_option,
    382                 int I422_quality, int I444_quality,
    383                 int I422_min, int I444_min,
    384         char *i420_profile, char *i422_profile, char *i444_profile)
    385 {
    386         return NULL;
    387 }
    388369
    389 void clean_encoder(struct x264lib_ctx *ctx)
    390 {
    391         return;
    392 }
    393 #endif
    394 
    395370int init_decoder_context(struct x264lib_ctx *ctx, int width, int height, int csc_fmt)
    396371{
    397372        if (csc_fmt<0)
     
    459434        free(ctx);
    460435}
    461436
    462 #ifndef _WIN32
     437
    463438x264_picture_t *csc_image_rgb2yuv(struct x264lib_ctx *ctx, const uint8_t *in, int stride)
    464439{
     440        x264_picture_t *pic_in = NULL;
    465441        if (!ctx->encoder || !ctx->rgb2yuv)
    466442                return NULL;
    467443
    468         x264_picture_t *pic_in = malloc(sizeof(x264_picture_t));
     444        pic_in = malloc(sizeof(x264_picture_t));
    469445        x264_picture_alloc(pic_in, ctx->colour_sampling, ctx->width, ctx->height);
    470446
    471447        /* Colorspace conversion (RGB -> I4??) */
     
    481457
    482458int compress_image(struct x264lib_ctx *ctx, x264_picture_t *pic_in, uint8_t **out, int *outsz)
    483459{
     460        x264_nal_t* nals = NULL;
     461        int i_nals = 0;
     462        x264_picture_t pic_out;
     463        int frame_size = 0;
     464
    484465        if (!ctx->encoder || !ctx->rgb2yuv) {
    485466                free_csc_image(pic_in);
    486467                *out = NULL;
    487468                *outsz = 0;
    488469                return 1;
    489470        }
    490         x264_picture_t pic_out;
    491471
    492472        /* Encoding */
    493473        pic_in->i_pts = 1;
    494         x264_nal_t* nals;
    495         int i_nals;
    496         int frame_size = x264_encoder_encode(ctx->encoder, &nals, &i_nals, pic_in, &pic_out);
     474        frame_size = x264_encoder_encode(ctx->encoder, &nals, &i_nals, pic_in, &pic_out);
    497475        // Unconditional cleanup:
    498476        free_csc_image(pic_in);
    499477        if (frame_size < 0) {
     
    507485        *outsz = frame_size;
    508486        return 0;
    509487}
    510 #else
    511 x264_picture_t* csc_image_rgb2yuv(struct x264lib_ctx *ctx, const uint8_t *in, int stride)
    512 {
    513         return  NULL;
    514 }
    515 int compress_image(struct x264lib_ctx *ctx, x264_picture_t *pic_in, uint8_t **out, int *outsz)
    516 {
    517         return 1;
    518 }
    519 #endif
    520488
     489
    521490int csc_image_yuv2rgb(struct x264lib_ctx *ctx, uint8_t *in[3], const int stride[3], uint8_t **out, int *outsz, int *outstride)
    522491{
    523492        AVPicture pic;
     
    594563 * Change the speed of encoding (x264 preset).
    595564 * @param percent: 100 for maximum ("ultrafast") with lowest compression, 0 for highest compression (slower)
    596565 */
    597 #ifndef _WIN32
    598566void set_encoding_speed(struct x264lib_ctx *ctx, int pct)
    599567{
    600568        x264_param_t param;
     569        int new_preset = 7-MAX(0, MIN(6, round(pct/16.7)));
    601570        x264_encoder_parameters(ctx->encoder, &param);
    602571        ctx->speed = pct;
    603         int new_preset = 7-MAX(0, MIN(6, pct/16.7));
    604572        if (new_preset==ctx->encoding_preset)
    605573                return;
    606574        //printf("set_encoding_speed(%i) old preset: %i=%s, new preset: %i=%s\n", pct, ctx->encoding_preset, x264_preset_names[ctx->encoding_preset], new_preset, x264_preset_names[new_preset]);
     
    614582        x264_param_apply_profile(&param, ctx->profile);
    615583        x264_encoder_reconfig(ctx->encoder, &param);
    616584}
    617 #else
    618 void set_encoding_speed(struct x264lib_ctx *ctx, int pct)
    619 {
    620         ;
    621 }
    622 #endif
    623585
    624586/**
    625587 * Change the quality of encoding (x264 f_rf_constant).
    626588 * @param percent: 100 for best quality, 0 for lowest quality.
    627589 */
    628 #ifndef _WIN32
    629590void set_encoding_quality(struct x264lib_ctx *ctx, int pct)
    630591{
     592        int old_csc_algo = ctx->csc_algo;
    631593        float new_quality = get_x264_quality(pct);
    632594        //printf("set_encoding_quality(%i) new_quality=%f, can csc=%i\n", pct, new_quality, ctx->supports_csc_option);
    633595        if (ctx->supports_csc_option) {
     
    658620                param.rc.f_rf_constant = new_quality;
    659621                x264_encoder_reconfig(ctx->encoder, &param);
    660622        }
    661         int old_csc_algo = ctx->csc_algo;
    662623        ctx->csc_algo = get_csc_algo_for_quality(pct);
    663624        if (old_csc_algo!=ctx->csc_algo) {
    664625                ctx->rgb2yuv = init_encoder_csc(ctx);
    665626        }
    666627}
    667 #else
    668 void set_encoding_quality(struct x264lib_ctx *ctx, int pct)
    669 {
    670         ;
    671 }
    672 #endif
    673628
    674629
    675630void* xmemalign(size_t size)
  • xpra/x264/x264lib.h

     
    55 * later version. See the file COPYING for details.
    66 */
    77
     8#include <stdint.h>
     9#include <inttypes.h>
     10
    811#ifdef _WIN32
    9 #include "stdint.h"
    10 #include "inttypes.h"
    11 #else
    12 #include "stdint.h"
    13 #include "stdlib.h"
     12#define _STDINT_H
    1413#endif
    1514
    16 #ifndef _WIN32
     15#define inline __inline
    1716#include <x264.h>
    18 #else
    19 #define inline __inline
    20 typedef void * x264_picture_t;
    21 #endif
    2217
    2318/** Opaque structure - "context". You must have a context to encode images of a given size */
    2419struct x264lib_ctx;