ffmpeg解码到opencv Mat中

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)
		{
			//av_frame_free(&frame);
			break;
		}
		else if (ret < 0) {
			//fprintf(stderr, "Error during decoding\n");
			//av_frame_free(&frame);
			break;
		}
		int w = 640; //v_frame->width;
		int h = 360; //v_frame->height;

		if (_img_convert_ctx == NULL)
		{
			_img_convert_ctx = sws_getContext(w, h,
				v_codecctx->pix_fmt,//PIX_FMT_YUV420P, 
				w,
				h,
				AV_PIX_FMT_BGR24,
				//SWS_POINT,
				SWS_BICUBIC,
				NULL,
				NULL,
				NULL);
		}
		AVFrame *dframe = av_frame_alloc();
		cv::Mat nmat;
		nmat.create(cv::Size(w, h), CV_8UC3);
		//printf("frame %3d\n", v_codecctx->frame_number);
		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);
	}
	//av_packet_unref(&pkt);

}

很简单,这样就把解码后的rgb直接放入到了cv::Mat 中。

你可能感兴趣的:(c++高级技巧,opencv,ffmepg,Mat)