2016-10-25 39 views
0

我使用ffmpeg在C++中提取视频帧。我想在C++中获得框架的array<unsigned char>,但是我从此行代码中获得AVFrame将AVPicture转换为数组<unsigned char>

avcodec_decode_video2(codecContext, DecodedFrame, &gotPicture, Packet); 

所以我用sws_scale转换AVFrameAVPicture而且我不能接到框架array<unsigned char>

sws_scale(convertContext, DecodedFrame->data, DecodedFrame->linesize, 0, (codecContext)->height, convertedFrame->data, convertedFrame->linesize); 

因此,谁能帮我AVFrameAVPicturearray<unsigned char>转换?

+0

解码的帧通常是YUV,并且图像以平面形式存储在AVFrame.data [0-2]中。对于使用swscale后的RGB转换,这将会有所不同,但仍然是AVFrame.data []中的像素数据。 AVFrame.linesize []通常与宽度不同,所以请记住。 – WLGfx

回答

1

AVPicture已弃用。转换成它是没有意义的,因为AVFrame是它的替代品。

如果我正确地理解了这个问题,您试图将原始图片像素值设置为std::array。如果是这样,只需将data字段的AVFrame转储到其中即可。

avcodec_decode_video2(codecContext, DecodedFrame, &gotPicture, Packet); 

// If you need rgb, create a swscontext to convert from video pixel format 
sws_ctx = sws_getContext(DecodedFrame->width, DecodedFrame->height, codecContext->pix_fmt, DecodedFrame->width, DecodedFrame->height, AV_PIX_FMT_RGB24, 0, 0, 0, 0); 

uint8_t* rgb_data[4]; int rgb_linesize[4]; 
av_image_alloc(rgb_data, rgb_linesize, DecodedFrame->width, DecodedFrame->height, AV_PIX_FMT_RGB24, 32); 
sws_scale(sws_ctx, DecodedFrame->data, DecodedFrame->linesize, 0, DecodedFrame->height, rgb_data, rgb_linesize); 

// RGB24 is a packed format. It means there is only one plane and all data in it. 
size_t rgb_size = DecodedFrame->width * DecodedFrame->height * 3; 
std::array<uint8_t, rgb_size> rgb_arr; 
std::copy_n(rgb_data[0], rgb_size, rgb_arr); 
+0

非类型模板参数必须是constexpr。检查问题标签,它不是std数组。 –

相关问题