Reputation: 87
My goal is to write the frame I decode into a file. I know I capture it well because it shows in my SDL playback and I encode it afterwards without any problems. Yet it seems I can't write the frame into a file properly. Here is the code :
#define PIXFMT AV_PIX_FMT_YUV420P
#define WIDTH 1280
#define HEIGHT 720
// initialize SWS context for software scaling
sws_ctx = sws_getContext(pCodecCtx->width,
pCodecCtx->height,
pCodecCtx->pix_fmt,
WIDTH,
HEIGHT,
PIXFMT,
SWS_LANCZOS,
NULL,
NULL,
NULL
);
FfmpegEncoder enc("rtsp://127.0.0.1:1935/live/myStream", pParser);
//SetPixelArray();
i = 0;
enc.FillYuvImage(pFrameRGB, 0, this->pCodecCtx->width, this->pCodecCtx->height);
FILE *pFile;
while (av_read_frame(pFormatCtx, &packet) >= 0 && !exit) {
if (packet.stream_index == videoindex) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished) {
i++;
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
if (i < 500)
{
pFile = fopen(std::string("./screenshots/screen.yuv").c_str(), "a+");
for (int y = 0; y < pCodecCtx->height; y++)
{
fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, pCodecCtx->width * pCodecCtx->height, pFile);
fwrite(pFrame->data[1] + y * pFrame->linesize[1], 1, pCodecCtx->width * pCodecCtx->height / 4, pFile);
fwrite(pFrame->data[2] + y * pFrame->linesize[2], 1, pCodecCtx->width * pCodecCtx->height / 4, pFile);
}
fclose(pFile);
}
enc.encodeFrame(pFrameRGB);
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
The program crashes when it tries to write the frame.
Upvotes: 0
Views: 1084
Reputation: 87
First, thanks for all your comments and answers ! Here is the fixed code :
while (av_read_frame(pFormatCtx, &packet) >= 0 && !exit && i < 1000) {
if (packet.stream_index == videoindex) {
// Decode video frame
avcodec_decode_video2(pCodecCtx, pFrame, &frameFinished, &packet);
if (frameFinished) {
i++;
sws_scale(sws_ctx, (uint8_t const * const *)pFrame->data, pFrame->linesize, 0, pCodecCtx->height, pFrameRGB->data, pFrameRGB->linesize);
if ((pFile = fopen(std::string("./screenshots/screen.yuv").c_str(), "ab")) == NULL)
continue;
fwrite(pFrameRGB->data[0], 1, pCodecCtx->width * pCodecCtx->height, pFile);
fwrite(pFrameRGB->data[1], 1, pCodecCtx->width * pCodecCtx->height / 4, pFile);
fwrite(pFrameRGB->data[2], 1, pCodecCtx->width * pCodecCtx->height / 4, pFile);
fclose(pFile);
enc.encodeFrame(pFrameRGB);
}
}
// Free the packet that was allocated by av_read_frame
av_free_packet(&packet);
}
I was using the wrong frame before, aswell as wrong data and size.
Upvotes: 0
Reputation: 1957
As I mentioned in comment, you need to check if fopen is successful.
But I think issue is you are writing more than what is present in buffer. I know limitted knowledge about yuv. I think you should do the following. This is based on my understanding from the Single Frame YUV420 picture in this page.
abcfor (int y = 0; y < pCodecCtx->height; y++) //plane 0: same width and height
{
fwrite(pFrame->data[0] + y * pFrame->linesize[0], 1, pCodecCtx->width, pFile);
}
for (int y = 0; y < pCodecCtx->height/4; y++) //plane1: Same width and quarter height.
{
fwrite(pFrame->data[1] + y * pFrame->linesize[1], 1, pCodecCtx->width, pFile);
}
for (int y = 0; y < pCodecCtx->height/4; y++) //plane2: Same width and quarter height.
{
fwrite(pFrame->data[2] + y * pFrame->linesize[2], 1, pCodecCtx->width, pFile);
}
Please note, the order of writing depends on the colour format. I want you to focus on the number of loop iterations and number of bytes in fwrite()
in each iteration.
Upvotes: 0