Trying to play a video using GLFW and FFmpeg on visual studio 2019 on windows 10.
Exception is thrown at sws_scale(sws_scaler_ctx, av_frame->data, av_frame->linesize, 0, av_frame->height, dest, dest_linesize); in the code. Don't know why this is occurred. Please Help!!
#include "video_reader.h"
#include <stdio.h>
bool video_reader_read_frame(VideoReaderState* state, uint8_t* frame_buffer, int64_t* pts) {
// Unpack members of state
auto& width = state->width;
auto& height = state->height;
auto& av_format_ctx = state->av_format_ctx;
auto& av_codec_ctx = state->av_codec_ctx;
auto& video_stream_index = state->video_stream_index;
auto& av_frame = state->av_frame;
auto& av_packet = state->av_packet;
auto& sws_scaler_ctx = state->sws_scaler_ctx;
// Decode one frame
int response;
while (av_read_frame(av_format_ctx, av_packet) >= 0) {
if (av_packet->stream_index != video_stream_index) {
av_packet_unref(av_packet);
continue;
}
response = avcodec_send_packet(av_codec_ctx, av_packet);
if (response < 0) {
printf("Failed to decode packet: %s\n", response);
return false;
}
response = avcodec_receive_frame(av_codec_ctx, av_frame);
if (response == AVERROR(EAGAIN) || response == AVERROR_EOF) {
av_packet_unref(av_packet);
continue;
}
else if (response < 0) {
printf("Failed to decode packet: %s\n", response);
return false;
}
av_packet_unref(av_packet);
break;
}
*pts = av_frame->pts;
// Set up sws scaler
if (!sws_scaler_ctx) {
sws_scaler_ctx = sws_getContext(width, height, av_codec_ctx->pix_fmt,
width, height, AV_PIX_FMT_RGB0,
SWS_BILINEAR, NULL, NULL, NULL);
}
if (!sws_scaler_ctx) {
printf("Couldn't initialize sw scaler\n");
return false;
}
uint8_t* dest[4] = { frame_buffer, NULL, NULL, NULL };
int dest_linesize[4] = { width * 4, 0, 0, 0 };
sws_scale(sws_scaler_ctx, av_frame->data, av_frame->linesize, 0, av_frame->height, dest, dest_linesize);
return true;
}
User contributions licensed under CC BY-SA 3.0