I’m trying to explore libav
and raylib
just to understand how audio and video work, and also to learn how to build nice interfaces using the raylib
project. I’ve implemented a simple struct capable of decoding audio and video frames. When a video frame appears, I convert it to the RGBA
format, which packs the values into 32bpp. This is the setup:
if (av_image_alloc((uint8_t **)media->dst_frame->data,
media->dst_frame->linesize, media->ctxs[0]->width,
media->ctxs[0]->height, AV_PIX_FMT_RGBA, 1) < 0) {
fprintf(stderr, "Failed to setup dest imagen");
return -1;
}
media->sws_ctx = sws_getContext(
media->ctxs[0]->width, media->ctxs[0]->height, media->ctxs[0]->pix_fmt,
media->ctxs[0]->width, media->ctxs[0]->height, AV_PIX_FMT_RGBA,
SWS_BILINEAR, NULL, NULL, 0);
// Later on, in the decode function:
int ret = sws_scale(media->sws_ctx, media->frame->data,
media->frame->linesize, 0, media->frame->height,
media->dst_frame->data, media->dst_frame->linesize);
In the main file, I init raylib, and setup the necessary steps to load the texture (here I’m trying to fetch the first video frame in order to show the user a preview of the video, later on I plan to reset the stream to allow a correct playback routine). I think the format of the image is right.
Image previewImage =
GenImageColor(videoArea.width, videoArea.height, BLACK);
// I assume this makes the formats compatible
ImageFormat(&previewImage, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8);
Texture2D videoTexture = LoadTextureFromImage(previewImage);
UnloadImage(previewImage);
if (!state->has_media) {
DrawText("Drop a video file here!", videoArea.x + 10,
videoArea.y + 10, 20, GRAY);
} else {
if (state->first_frame) {
do {
decode_packet(state->media);
} while (!is_frame_video(state->media));
UpdateTexture(videoTexture, state->media->dst_frame->data[0]);
state->first_frame = 0;
}
}
DrawTexture(videoTexture, videoArea.x, videoArea.y, WHITE);
Anyway, this is what I get when a mp4 file is dropped:
It seems like an alignment issue maybe? Can someone point me in the right direction in order to correctly solve this problem?