Last active
February 20, 2025 08:15
-
-
Save nakaly/11eb992ebd134ee08b75e4c67afb5703 to your computer and use it in GitHub Desktop.
YUV <-> RGB by ffmpeg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Convert from YUV to RGB | |
struct SwsContext *sws_ctx = NULL; | |
sws_ctx = | |
sws_getContext | |
( | |
inlink->w, | |
inlink->h, | |
inlink->format, | |
outlink->w, | |
outlink->h, | |
AV_PIX_FMT_RGB24, | |
SWS_BILINEAR,NULL,NULL,NULL | |
); | |
uint8_t *prgb24 = calloc(3 * inlink->w * inlink->h, sizeof(uint8_t)); | |
uint8_t *rgb24[1] = { prgb24 }; | |
int rgb24_stride[1] = { 3 * inlink->w }; | |
sws_scale(sws_ctx, in->data, in->linesize, 0, inlink->h, rgb24, rgb24_stride); | |
if (!out) { | |
av_frame_free(&in); | |
return AVERROR(ENOMEM); | |
} | |
av_frame_copy_props(out, in); | |
// Put 0 to all red byte | |
for (i = 0; i < inlink->w * inlink->h; i++ ) | |
{ | |
rgb24[0][i * 3] = 0; | |
} | |
// Convert from RGB to YUV | |
sws_ctx = sws_getContext( | |
inlink->w, | |
inlink->h, | |
AV_PIX_FMT_RGB24, | |
outlink->w, | |
outlink->h, | |
AV_PIX_FMT_YUV420P, | |
SWS_BILINEAR, NULL, NULL, NULL | |
); | |
sws_scale(sws_ctx, rgb24, rgb24_stride, 0, inlink->h, out->data, out->linesize); | |
free(prgb24); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment