[英]ffmpeg sws_scale YUV420p to RGBA not giving correct scaling result (c++)
我正在嘗試通過sws_scale將已解碼的YUV420p幀(1018x700)縮放為RGBA,我正在將數據保存到原始視頻文件中,然后使用ffplay播放原始視頻以查看結果。
這是我的代碼:
sws_ctx = sws_getContext(video_dec_ctx->width, video_dec_ctx->height,AV_PIX_FMT_YUV420P, video_dec_ctx->width, video_dec_ctx->height, AV_PIX_FMT_BGR32, SWS_LANCZOS | SWS_ACCURATE_RND, 0, 0, 0);
ret = avcodec_decode_video2(video_dec_ctx, yuvframe, got_frame, &pkt);
if (ret < 0) {
std::cout<<"Error in decoding"<<std::endl;
return ret;
}else{
//the source and destination heights and widths are the same
int sourceX = video_dec_ctx->width;
int sourceY = video_dec_ctx->height;
int destX = video_dec_ctx->width;
int destY = video_dec_ctx->height;
//declare destination frame
AVFrame avFrameRGB;
avFrameRGB.linesize[0] = destX * 4;
avFrameRGB.data[0] = (uint8_t*)malloc(avFrameRGB.linesize[0] * destY);
//scale the frame to avFrameRGB
sws_scale(sws_ctx, yuvframe->data, yuvframe->linesize, 0, yuvframe->height, avFrameRGB.data, avFrameRGB.linesize);
//write to file
fwrite(avFrameRGB.data[0], 1, video_dst_bufsize, video_dst_file);
}
我使用以下命令運行ffplay(“視頻”是原始視頻文件)
ffplay -f rawvideo -pix_fmt bgr32 -video_size 1018x700 video
我該如何解決才能使RGB32正確縮放?
我找到了解決方案,這里的問題是我沒有使用正確的緩沖區大小來寫入文件。
fwrite(avFrameRGB.data[0], 1, video_dst_bufsize, video_dst_file);
變量video_dst_file是從的返回值中獲取的
video_dst_bufsize = av_image_alloc(yuvframe.data, yuvframe.linesize, destX, destY, AV_PIX_FMT_YUV420P, 1);
解決方案是從fget語句中獲取返回值並在其中使用它:
video_dst_bufsize_RGB = av_image_alloc(avFrameRGB.data, avFrameRGB.linesize, destX, destY, AV_PIX_FMT_BGR32, 1);
fwrite(avFrameRGB.data[0], 1, video_dst_bufsize_RGB, video_dst_file);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.