首页 文章

使用libvlc smem从视频中获取帧并将其转换为opencv Mat . (C )

提问于
浏览
7

[UPDATED WITH PARTIAL ANSWER]
这是我的代码:

void cbVideoPrerender(void *p_video_data, uint8_t **pp_pixel_buffer, int size) {
    // Locking
    imageMutex.lock();
    videoBuffer = (uint8_t *)malloc(size);
    *pp_pixel_buffer = videoBuffer;
}  
void cbVideoPostrender(void *p_video_data, uint8_t *p_pixel_buffer
      , int width, int height, int pixel_pitch, int size, int64_t pts) {
   // Unlocking
   imageMutex.unlock();
   Mat img = Mat(Size(width,height), CV_8UC3, p_pixel_buffer);
   //cvtColor(img,img,CV_RGB2BGR);
}
int main(int argc, char ** argv)
{
   libvlc_instance_t * inst;
   char smem_options[1000];
   sprintf(smem_options
      , "#transcode{vcodec=RV24}:smem{"
         "video-prerender-callback=%lld,"
         "video-postrender-callback=%lld,"
         "video-data=%lld,"
         "no-time-sync},"
      , (long long int)(intptr_t)(void*)&cbVideoPrerender
      , (long long int)(intptr_t)(void*)&cbVideoPostrender //This would normally be useful data, 100 is just test data
      , (long long int)200 //Test data
      );
    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=1", // Be verbose
              "--sout", smem_options // Stream to memory
               };

    // We launch VLC
    inst = libvlc_new(sizeof(vlc_args) / sizeof(vlc_args[0]), vlc_args);
...
return 0;
}

QUESTION UPDATED
我检查了我的两个回调函数似乎正确执行 .
_RV32准确输出什么样的数据?它是否适合CV_8U3C(此处需要无符号8位int 3通道?_我需要向Mat类添加一个步骤吗?(步骤 - 每个矩阵行占用的字节数)
UPDATED2
我将RV32改为RV24,这更有意义 . 我添加cvtColor导致Mat矩阵似乎需要BGR像素而不是RGB但仍然无法正确显示图像 .
_有一个vcodec会给我一个YUV格式作为输出,所以我可以在尝试输出opencv :: Mat img之前测试像素数据吗?
[EDIT OUTPUT IMG] (通过将vlc类型更改为CV_8UC4四个通道(不知道为什么)我们几乎可以看到框架,但质量非常差,为什么会这样?
img updated 2

[SOLUTION]
我发现我视频开头的图像质量很差,这就是为什么我的Mat imshow()向我展示了如此丑陋的东西,上面的代码应该可以正常工作(显然不需要cvtColor)

1 回答

  • 6

    首先,快速警告:从VLC2.2(当前git版本,即将发布)开始,size参数是size_t . smem没有API(但希望这会改变),这很糟糕,所以这会默默地破坏你的应用程序 .

    然后,快速评论“数据”参数:它应该包含你需要做的处理 . 这是一个指向结构的指针,一个类的实例,你可以命名它 . 我强烈怀疑在32位机器上通过很长时间才能工作,因为你要强制64位只能包含32位的东西 . 你应该做的是声明一个结构,并将你需要的内容存储到它中 . 在这里,一个很好的例子可能是:

    struct MyParamStruct
    {
        YourMutexType imageMutex; // Here mutex is not a global variable anymore
        int otherParam; // You can use this to store the value 200 that you were passing before
    };
    //...
    
    // Init the struct somewhere
    MyParamStruct* param = new MyStructParam;
    param->otherParam = 200;
    //...
    
    sprintf(smem_options
          , "#transcode{vcodec=h264}:smem{"
             "video-prerender-callback=%lld,"
             "video-postrender-callback=%lld,"
             "video-data=%lld,"
             "no-time-sync},"
          , (long long int)(intptr_t)(void*)&cbVideoPrerender
          , (long long int)(intptr_t)(void*)&cbVideoPostrender //This would normally be useful data, 100 is just test data
          , (long long int)(intptr_t)(void*)param
          );
    

    关于互斥锁的使用,它对我来说很好看 . 实际上,您似乎没有任何并发问题,因为您为每个帧同步分配一个新的缓冲区 . 如果每次使用预分配缓冲区,则在退出postrender函数时需要考虑锁定 .

    事实上,我甚至不确定什么是无效指针p_video_data .

    这取决于您的图像格式 . 对于H264,它将取决于解码器将输出的像素格式 . 由于您要求H264输出,很可能您会获得平面像素格式,但具体类型取决于您的H264配置文件 .

    如果您建议您切换到RV32: #transcode{vcodec=RV32}

    您需要传递给转码模块的是 output fourcc,VLC会为您处理输入:)

    更新

    我不知道Mat类是否取得指针的所有权,但您可能也想检查它 .

    更新2

    要回答关于什么是RV32的进一步问题:

    /* 24 bits RGB */
    #define VLC_CODEC_RGB24           VLC_FOURCC('R','V','2','4')
    /* 24 bits RGB padded to 32 bits */
    #define VLC_CODEC_RGB32           VLC_FOURCC('R','V','3','2')
    /* 32 bits RGBA */
    #define VLC_CODEC_RGBA            VLC_FOURCC('R','G','B','A')
    

    如果您只期望3个字节,那么您可能应该试试RV24!我应该从一开始就建议,因为8CU3肯定只建议3个字节......

相关问题