Search code examples
c++opencvlibvlc

SMEM: Bad dst image pointers when using format_callback


I'm trying to get frames from video stream and use them as OpenCV Mat, but I have encountered a problem. I don't know the resolution my video stream will have so I use libvlc_video_set_format_callbacks but then, after video starts playing, I hear audio, but I'm not getting an image, instead I get errors: Bad dst image pointers. Here's my code:

struct ctx
{
    unsigned char *pixeldata;
    std::mutex imagemutex;
};

static void *lock(void *data, void **p_pixels)
{
    struct ctx *ctx = reinterpret_cast<struct ctx *>(data);
    ctx->imagemutex.lock();
    *p_pixels = ctx->pixeldata;
    return NULL;
}


static void unlock(void *data, void *id, void *const *p_pixels)
{
    struct ctx *ctx = reinterpret_cast<struct ctx *>(data);
    ctx->imagemutex.unlock();
    assert(id == NULL);
}

unsigned setup(void **opaque, char *chroma, unsigned *width, unsigned *height, unsigned *pitches, unsigned *lines)
{
   struct ctx *callback = reinterpret_cast<struct ctx *>(*opaque);

   unsigned nWidth = (*width);
   unsigned nHeight = (*height);
   (*pitches) = nWidth * 3;
   (*lines) = nHeight;
   chroma = (char *)"RV24";

   callback->pixeldata = new unsigned char[nWidth*nHeight*3];
   return 1;
}

int main(int argc, char *argv[])
{
    libvlc_instance_t *vlcInstance;
    libvlc_media_player_t *_player;
    libvlc_media_t *_media;

    const char * const vlc_args[] = {
       "-I", "dummy", // Don't use any interface
       "--ignore-config", // Don't use VLC's config
       "--extraintf=logger", // Log anything
       "--verbose=2", // Be much more verbose then normal for debugging purpose
    };

    vlcInstance = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);

   _media = libvlc_media_new_location(vlcInstance , "rtsp://address");
   _player = libvlc_media_player_new_from_media(_media);

   struct ctx *callback = new struct ctx;
   libvlc_video_set_callbacks(_player, lock, unlock, 0, callback);
   libvlc_video_set_format_callbacks(_player, setup, 0);
   libvlc_media_player_play(_player);    
}

If I won't use libvlc_video_set_format_callbacks(_player, setup, 0); but instead set fixed parameters, eg. libvlc_video_set_format(_player, "RV24", 720, 404, 720 * 3); it works fine. Any idea what might be a problem?


Solution

  • Actually it was simple and stupid mistake. Line:

    chroma = (char *)"RV24";
    

    needs to be replaced by:

    memcpy(chroma, "RV24", 4);