你怎么调整的AVFrame? [英] How do you resize an AVFrame?

查看:299
本文介绍了你怎么调整的AVFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么调整的 AVFrame ?我

下面是目前我在做什么:

Here's what I'm currently doing:

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->宽度高度仍然为0,AVFrame的内容看起来像垃圾,我得到的数据是不对齐的,当我打电话 sws_scale 一个警告。注:我不想改变像素格式,我不想硬code是什么

But after this resizedFrames->widthand height are still 0, the contents of the AVFrame look like garbage, and I get an warning that data is unaligned when I call sws_scale. Note: I don't want to change the pixel format, and I don't want to hard code what it is.

推荐答案

所以,有几件事情怎么回事。

So, there's a few things going on.


  • avpicture_fill()不设置框架>宽/高/格式。你必须自己设置这些值。

  • avpicture_get_size()并的 avpicture_fill()不保证对齐。所谓在这些包装的基本功能(例如<一href=\"https://www.ffmpeg.org/doxygen/trunk/group__lavu__picture.html#ga24a67963c3ae0054a2a4bab35930e694\"相对=nofollow> av_image_get_buffer_size()或<一个href=\"https://www.ffmpeg.org/doxygen/trunk/group__lavu__picture.html#ga5b6ead346a70342ae8a303c16d2b3629\"相对=nofollow> av_image_fill_arrays())被称为与ALIGN = 1,所以有行之间没有缓冲区对齐。如果你想比对(你这样做),你要么直接与不同的对齐设置调用底层的功能,或致电的 AV codec_align_dimensions2()并提供一致的宽度/高度的avpicture _ *()函数。如果你这样做,你也可以考虑使用 avpicture_alloc()而不是<一HREF =htt​​p://ffmpeg.org/doxygen/trunk/group__lavc__picture.html#ga18a08bcb237767ef442fd5d3d1dd2084相对=nofollow> avpicture_get_size() +的 av_malloc() +的 avpicture_fill()

  • avpicture_fill() does not set frame->width/height/format. You have to set these values yourself.
  • avpicture_get_size() and avpicture_fill() do not guarantee alignment. The underlying functions called in these wrappers (e.g. av_image_get_buffer_size() or av_image_fill_arrays()) are called with align=1, so there's no buffer alignment between lines. If you want alignment (you do), you either have to call the underlying functions directly with a different align setting, or call avcodec_align_dimensions2() on the width/height and provide aligned width/height to the avpicture_*() functions. If you do that, you can also consider using avpicture_alloc() instead of avpicture_get_size() + av_malloc() + avpicture_fill().

我想,如果你遵循这些两个建议,你会发现,重新调节正常工作,没有给出警告,并拥有正确的输出。因为你试图做双线性缩放的质量可能不会很大。大多数人使用双三次缩放(的 SWS_BICUBIC )。

I think if you follow these two suggestions, you'll find that the rescaling works as expected, gives no warnings and has correct output. The quality may not be great because you're trying to do bilinear scaling. Most people use bicubic scaling (SWS_BICUBIC).

这篇关于你怎么调整的AVFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆