@donte.zoom Hi, finally I resolved my issue myself. Sharing solution that may be save others time.
Ffmpeg Helper Header file:
extern âCâ
{
#include <libswscale/swscale.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libavutil/opt.h>
#include <libavutil/frame.h>
#include <libavutil/mem.h>
#include <libavcodec/avcodec.h>
#pragma comment (lib,âavcodec.libâ)
#pragma comment (lib,âavutil.libâ)
#pragma comment (lib,âswscale.libâ)
}
class FFmpegHelper
{
public:
AVFrame* allocPicture(enum AVPixelFormat pix_fmt, int width, int height);
AVFrame* frameConversion(char* y, char* u, char* v, int srcWidth, int srcHeight);
};
Ffmpeg Source File:
#include âffmpeg-helper.hâ
AVFrame* FFmpegHelper::allocPicture(enum AVPixelFormat pix_fmt, int width, int height)
{
// Allocate a frame
AVFrame* frame = av_frame_alloc();
if (frame == NULL)
{
fprintf(stderr, "avcodec_alloc_frame failed");
}
if (av_image_alloc(frame->data, frame->linesize, width, height, pix_fmt, 1) < 0)
{
fprintf(stderr, "av_image_alloc failed");
}
frame->width = width;
frame->height = height;
frame->format = pix_fmt;
return frame;
}
AVFrame* FFmpegHelper::frameConversion(char* y, char* u, char* v, int srcWidth, int srcHeight)
{
SwsContext* resize;
int width = 640;
int height = 360;
AVFrame* src = allocPicture(AVPixelFormat::AV_PIX_FMT_YUV420P, srcWidth, srcHeight);
AVFrame* dst = allocPicture(AVPixelFormat::AV_PIX_FMT_YUV420P, width, height);
src->data[0] =(uint8_t*) y;
src->data[1] =(uint8_t*) u;
src->data[2] =(uint8_t*) v;
resize = sws_getContext(
srcWidth,
srcHeight,
AVPixelFormat::AV_PIX_FMT_YUV420P,
width,
height,
AVPixelFormat::AV_PIX_FMT_YUV420P,
SWS_LANCZOS | SWS_ACCURATE_RND,
NULL,
NULL,
NULL);
int response = sws_scale(
resize,
src->data,
src->linesize,
0,
height,
dst->data,
dst->linesize);
return dst;
}
Here is recording saving
videoFile = fopen(videoFilePath.c_str(), âabâ);
auto height = data->GetStreamHeight();
auto width = data->GetStreamWidth();
AVFrame* yuvScaled = ffmpegHelper.frameConversion(data->GetYBuffer(), data->GetUBuffer(), data->GetVBuffer(), width, height);
auto yPlanar = 640 * 360;
auto uPlanar = (yPlanar / 4);
auto vPlanar = (yPlanar / 4);
fwrite(yuvScaled->data[0], yPlanar, 1, videoFile);
fwrite(yuvScaled->data[1], uPlanar, 1, videoFile);
fwrite(yuvScaled->data[2], vPlanar, 1, videoFile);
fclose(videoFile);