对rgb图像转yuv,可以直接对像素进行处理实现,但可能写出来的代码执行效率不高,发现ffmpeg的执行速度很快,后续再研究其转换的函数sws_scale的源码,下面是转换实现的代码,
SwsContext* swsCtx = NULL;
swsCtx = sws_getContext(WIDTH, HEIGHT, AV_PIX_FMT_RGB24, WIDTH, HEIGHT, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);
uint8_t* rgbBuf = (uint8_t*)av_malloc(frameSize * sizeof(uint8_t));
AVFrame* rgbFrame = av_frame_alloc();
rgbFrame->format = AV_PIX_FMT_BGR24;
rgbFrame->width = WIDTH;
rgbFrame->height = HEIGHT;
rgbFrame->linesize[0] = WIDTH * 3;
av_image_fill_arrays(rgbFrame->data, rgbFrame->linesize, rgbBuf, AV_PIX_FMT_RGB24, WIDTH, HEIGHT, 1);
int outSliceH = sws_scale(swsCtx, rgbFrame->data, rgbFrame->linesize, 0, HEIGHT, frame->data, frame->linesize);