简体   繁体   English

如何为FFmpeg lib创建函数以将帧编码为任何(可能的)格式?

[英]How to create function for FFmpeg lib to encode frame into any (possible) format?

So how to create something which would have something like this in its api: 因此,如何创建在其api中具有类似内容的内容:

Encoder = EncoderFormat(format name); // prepare encoder
//...code for frames generation... now we want to encode frame!//
EncodeFrame(const CImage* src, void* dest, int* is_keyframe, more args if needed);

Is it possible? 可能吗? Please could any one provide simple example of how to create such thing? 请提供一个简单示例说明如何创建这种东西吗?

It is not possible without resorting to extremely bad programming practices, like global variables. 如果不诉诸极差的编程习惯(如全局变量),这是不可能的。 Encoding of frames is not independent; 帧的编码不是独立的。 an encoder must keep a state (context) for which you must keep a pointer to pass to the encoder function each time. 编码器必须保持一个状态(上下文),您必须为此状态保持一个指针以每次都传递给编码器功能。 The idea of passing a choice of format to EncodeFrame is also rather silly since you can't pick a format per-frame without closing the existing encoder context and switching to a new one. 将格式选择传递给EncodeFrame的想法也很愚蠢,因为如果不关闭现有的编码器上下文并切换到新的编码器上下文,就无法选择每帧的格式。

Unless the source image is already in the format the encoder wants (probably YUV 4:2:0) your wrapper will need to convert it. 除非源图像已经是编码器想要的格式(可能是YUV 4:2:0),否则包装程序将需要对其进行转换。 This can be done on your own or using libswscale from ffmpeg. 这可以自己完成,也可以使用ffmpeg的libswscale来完成。 You need to provide a timestamp for each frame too. 您还需要为每个帧提供一个时间戳。 If you want a simple API where you don't have to worry about that stuff, you probably want to wrap the av codec context pointer libavcodec gives you in another structure where you keep your running timestamp value, swscale context pointer, etc. 如果您想要一个不需要担心这些东西的简单API,则可能要包装av编解码器上下文指针libavcodec为您提供另一种结构,其中您可以保留运行时戳值,swscale上下文指针等。

Aside from that, your API has no way to specify the size of the destination buffer, so it's completely unsafe. 除此之外,您的API无法指定目标缓冲区的大小,因此这是完全不安全的。 It may be better to return a pointer to the internal buffer (via either a return value or pointer-to-pointer argument) along with the encoded frame size, instead of writing to the caller's buffer. 最好将指向内部缓冲区的指针(通过返回值或指向指针的参数)连同编码的帧大小一起返回,而不是写入调用方的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM