如何调整 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->width
和height
仍然是0之后,AVFrame的内容看起来像垃圾,当我调用 sws_scale 。注意:我不想更改像素格式,也不想对其进行硬编码。
最佳答案
所以,有一些事情正在发生。
- avpicture_fill()不设置框架->宽度/高度/格式。您必须自己设置这些值。
- 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() .
我认为,如果您遵循这两个建议,您会发现重新缩放按预期工作,不会发出警告并且具有正确的输出。质量可能不是很好,因为您正在尝试执行 bilinear缩放。大多数人使用bicubic缩放(SWS_BICUBIC)。
关于c++ - 如何调整 AVFrame 的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31253485/