yuvToRgb(char * outSpace, int outWidth, int outHeight)
{
mutex.lock();
if (!iCtx || !yuv)
{
strcpy(errorbuf, "stream not open or yuv not decoded!");
mutex.unlock();
return false;
}
AVCodecContext * videoCtx = iCtx->streams[this->videoStream]->codec;
vCtx = sws_getCachedContext(vCtx,
videoCtx->width, videoCtx->height, videoCtx->pix_fmt,
outWidth, outHeight, AV_PIX_FMT_BGRA,
SWS_BICUBIC, NULL, NULL, NULL
);
if (!vCtx)
{
strcpy(errorbuf, "sws getCachedContext failed!");
mutex.unlock();
return false;
}
uint8_t *data[AV_NUM_DATA_POINTERS] = {
0 };
data[0] = (uint8_t *)outSpace;
int linesize[AV_NUM_DATA_POINTERS] = {
0 };
linesize[0] = outWidth * 4;
int h = sws_scale(vCtx, yuv->data, yuv->linesize, 0, videoCtx->height,
data, linesize);
if (h <= 0)
{
strcpy(errorbuf, "sws scale failed!");
mutex.unlock();
return false;
}
mutex.unlock();
return true;
}
Mat avframe_to_cvmat(AVFrame *frame)
{
AVFrame dst;
cv::Mat cvmat;
memset(&dst, 0, sizeof(dst));
int w = frame->width, h = frame->height;
cvmat = cv::Mat(h, w, CV_8UC3);
dst.data[0] = (uint8_t *)cvmat.data;
avpicture_fill((AVPicture *)&dst, dst.data[0], AV_PIX_FMT_BGR24, w, h);
struct SwsContext *img_convert_ctx = NULL;
AVPixelFormat src_pixfmt = (AVPixelFormat)frame->format;
AVPixelFormat dst_pixfmt = AV_PIX_FMT_BGR24;
img_convert_ctx = sws_getContext(w, h, src_pixfmt, w, h, dst_pixfmt, SWS_FAST_BILINEAR, NULL, NULL, NULL);
sws_scale(img_convert_ctx, frame->data, frame->linesize, 0, h, dst.data, dst.linesize);
sws_freeContext(img_convert_ctx);
return cvmat;
}