你如何调整AVFrame的大小?

你如何调整AVFrame大小? 一世

这是我目前正在做的事情:

 AVFrame* frame = /*...*/; int width = 600, height = 400; AVFrame* resizedFrame = av_frame_alloc(); auto format = AVPixelFormat(frame->format); auto buffer = av_malloc(avpicture_get_size(format, width, height) * sizeof(uint8_t)); avpicture_fill((AVPicture *)resizedFrame, (uint8_t*)buffer, format, width, height); struct SwsContext* swsContext = sws_getContext(frame->width, frame->height, format, width, height, format, SWS_BILINEAR, nullptr, nullptr, nullptr); sws_scale(swsContext, frame->data, frame->linesize, 0, frame->height, resizedFrame->data, resizedFrame->linesize); 

但是在此resizedFrames->widthheight仍为0之后,AVFrame的内容看起来像垃圾,当我调用sws_scale时,我收到一条警告,表示数据未对齐。 注意:我不想更改像素格式,我不想硬编码它是什么。

所以,有一些事情正在发生。

  • avpicture_fill()不设置frame- > width / height / format。 您必须自己设置这些值。
  • avpicture_get_size()和avpicture_fill()不保证对齐。 在这些包装器中调用的底层函数(例如av_image_get_buffer_size()或av_image_fill_arrays() )使用align = 1调用,因此行之间没有缓冲区对齐。 如果你想要对齐(你这样做),你必须使用不同的对齐设置直接调用底层函数,或者在宽度/高度上调用avcodec_align_dimensions2()并为avpicture _ *()函数提供对齐的宽度/高度。 如果你这样做,你也可以考虑使用avpicture_alloc()而不是avpicture_get_size() + av_malloc() + avpicture_fill() 。

我想如果您遵循这两个建议,您会发现重新缩放按预期工作,不会发出警告并且输出正确。 质量可能不是很好,因为你正在尝试进行双线性缩放。 大多数人使用双三次缩放( SWS_BICUBIC )。