你如何调整 AVFrame 的大小?
How do you resize an 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->width
和height
之后仍然是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,所以行与行之间没有缓冲区对齐。如果你想要对齐(你这样做),你要么必须使用不同的对齐设置直接调用底层函数,要么在 width/height 上调用 avcodec_align_dimensions2()并为 avpicture_*() 函数提供对齐的 width/height。如果这样做,您还可以考虑使用 avpicture_alloc() 而不是 avpicture_get_size() + av_malloc() + avpicture_fill().
我认为如果您遵循这两个建议,您会发现重新缩放按预期工作,没有发出警告并且输出正确。质量可能不是很好,因为您正在尝试 bilinear scaling. Most people use bicubic scaling (SWS_BICUBIC)。
如何调整 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()不设置frame->width/height/format。您必须自己设置这些值。
- avpicture_get_size() 和 avpicture_fill() 不保证对齐。在这些包装器中调用的底层函数(例如 av_image_get_buffer_size() 或 av_image_fill_arrays())使用 align= 调用1,所以行与行之间没有缓冲区对齐。如果你想要对齐(你这样做),你要么必须使用不同的对齐设置直接调用底层函数,要么在 width/height 上调用 avcodec_align_dimensions2()并为 avpicture_*() 函数提供对齐的 width/height。如果这样做,您还可以考虑使用 avpicture_alloc() 而不是 avpicture_get_size() + av_malloc() + avpicture_fill().
我认为如果您遵循这两个建议,您会发现重新缩放按预期工作,没有发出警告并且输出正确。质量可能不是很好,因为您正在尝试 bilinear scaling. Most people use bicubic scaling (SWS_BICUBIC)。