rtsp 接收解码与opencv结合并不是使用opencv直接打开rtsp 链接的意思,使用live555等工具或者自行编写后解码时与opencv直接融合
cv::Mat
解码时如何把解码数据直接放到cv::Mat 中?
void c_rtspthread::Decode2RGB(uint8_t* src, int & srcLen)
{
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = src;
pkt.size = srcLen;
int ret = avcodec_send_packet(v_codecctx, &pkt) == 0;
av_packet_unref(&pkt);
if (ret < 0)
{
fprintf(stderr, "Error sending a packet for decoding\n");
return;
}
while (ret >= 0)
{
ret = avcodec_receive_frame(v_codecctx, v_frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
{
break;
}
else if (ret < 0) {
break;
}
int w = 640;
int h = 360;
if (_img_convert_ctx == NULL)
{
_img_convert_ctx = sws_getContext(w, h,
v_codecctx->pix_fmt,
w,
h,
AV_PIX_FMT_BGR24,
SWS_BICUBIC,
NULL,
NULL,
NULL);
}
AVFrame *dframe = av_frame_alloc();
cv::Mat nmat;
nmat.create(cv::Size(w, h), CV_8UC3);
av_image_fill_arrays(dframe->data, dframe->linesize, nmat.data, AV_PIX_FMT_BGR24,
w, h, 16);
sws_scale(_img_convert_ctx, v_frame->data, v_frame->linesize, 0, h,
dframe->data, dframe->linesize);
if (v_analyse != NULL)
{
v_analyse->pushdata(nmat);
}
av_frame_free(&dframe);
}
}
很简单,这样就把解码后的rgb直接放入到了cv::Mat 中。
|