SMEM:使用format_callback时的坏dst图像指针

时间:2016-03-14 01:05:02

标签: c++ opencv libvlc

我试图从视频流中获取帧并将其用作OpenCV Mat,但我遇到了一个问题。 我不知道我的视频流所具有的分辨率,所以我使用了 libvlc_video_set_format_callbacks 但是,在视频开始播放后,我听到了音频,但我没有收到图片,而是出现了错误: Bad dst image pointers。  这是我的代码:

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);    
}

如果我不使用libvlc_video_set_format_callbacks(_player, setup, 0);,而是设置固定参数,例如。 libvlc_video_set_format(_player, "RV24", 720, 404, 720 * 3);它运作正常。知道可能有什么问题吗?

1 个答案:

答案 0 :(得分:0)

实际上这是一个简单而愚蠢的错误。 行:

chroma = (char *)"RV24";

需要替换为:

memcpy(chroma, "RV24", 4);