3

I'm working on a C++ project using Visual Studio 2010 on Windows. I'm linking dynamically against x264 which I built myself as a shared library using MinGW following the guide at

http://www.ayobamiadewole.com/Blog/Others/x264compilation.aspx

The strange thing is that my x264 code is working perfectly sometimes. Then when I change some line of code (or even change the comments in the file!) and recompile everything crashes on the line

encoder_ = x264_encoder_open(&param);

With the message

Access violation reading location 0x00000000

I'm not doing anything funky at all so it's probably not my code that is wrong but I guess there is something going wrong with the linking or maybe something is wrong with how I compiled x264.

The full initialization code:

x264_param_t param = { 0 };
if (x264_param_default_preset(&param, "ultrafast", "zerolatency") < 0) {
  throw KStreamerException("x264_param_default_preset failed");
}

param.i_threads = 1;
param.i_width = 640;
param.i_height = 480;
param.i_fps_num = 10;
param.i_fps_den = 1;

encoder_ = x264_encoder_open(&param); // <-----
if (encoder_ == 0) {
  throw KStreamerException("x264_encoder_open failed");
}

x264_picture_alloc(&pic_, X264_CSP_I420, 640, 480);

Edit: It turns out that it always works in Release mode and when using superfast instead of ultrafast it also works in Debug mode 100%. Could it be that the ultrafast mode is doing some crazy optimizations that the debugger doesn't like?

Daniel
  • 1,417
  • 2
  • 12
  • 20

2 Answers2

1

I've met this problem too with libx264-120. libx264-120 was built on MinGW and configuration option like below.

$ ./configure --disable-cli --enable-shared --extra-ldflags=-Wl,--output-def=libx264-120.def --enable-debug --enable-win32thread

platform:      X86
system:        WINDOWS
cli:           no
libx264:       internal
shared:        yes
static:        no
asm:           yes
interlaced:    yes
avs:           yes
lavf:          no
ffms:          no
gpac:          no
gpl:           yes
thread:        win32
filters:       crop select_every
debug:         yes
gprof:         no
strip:         no
PIC:           no
visualize:     no
bit depth:     8
chroma format: all

$ make -j8

lib /def:libx264-120.def /machine:x86

#include "stdafx.h"
#include <iostream>
#include <cassert>

using namespace std;

#include <stdint.h>
extern "C"{
#include <x264.h>
}

int _tmain(int argc, _TCHAR* argv[])
{
int width(640);
int height(480);

int err(-1);

x264_param_t x264_param = {0};
//x264_param_default(&x264_param);

err = 
    x264_param_default_preset(&x264_param, "veryfast", "zerolatency");
assert(0==err);

x264_param.i_threads = 8;
x264_param.i_width = width;
x264_param.i_height = height;
x264_param.i_fps_num = 60;//fps;
x264_param.i_fps_den = 1;
// Intra refres:
x264_param.i_keyint_max = 60;//fps;
x264_param.b_intra_refresh = 1;
//Rate control:
x264_param.rc.i_rc_method = X264_RC_CRF;
x264_param.rc.f_rf_constant = 25;
x264_param.rc.f_rf_constant_max = 35;
//For streaming:
x264_param.b_repeat_headers = 1;
x264_param.b_annexb = 1;

err = x264_param_apply_profile(&x264_param, "baseline");
assert(0==err);

x264_t *x264_encoder = x264_encoder_open(&x264_param);
x264_encoder = x264_encoder;


x264_encoder_close( x264_encoder );

getchar();
return 0;
}

This program succeeds sometime. But will fail often on x264_encoder_open with the access violation. The information for this is not existing on Google. And how to initialize x264_param_t and how to use x264_encoder_open are unclear.

It seems that behavior caused from x264's setting values, but I can't know these without reading some open source programs that using libx264.

And, this access violation seems doesn't occurs on FIRST TIME EXECUTION and on compilation with MinGW's gcc (e.g gcc -o test test.c -lx264;./test)

Since this behavior, I think that libx264 doing some strange processes of resources in DLL version of ilbx264 that was built on MinGW's gcc.

0

I had the same problem. The only way I was able to fix it was to build the x264 dll without the asm option (ie. specify --disable-asm)

fbf
  • 1