[Libav-user] Setting bitrate for x264 encoder?
Philip Schneider
pjschneider at earthlink.net
Thu Apr 9 20:55:02 CEST 2015
Hi Max -
Good suggestion...I’ll try to create a minimal implementation to post.
But now my questions may be a bit more educated. I’m posting what I’ve learned so far, so that perhaps this will help others, or allow others to provide some more useful feedback...
There’s a *lot* of info on command-line ffmpeg usage, with details on controlling quality for x264 encoding. So I was convinced my goal is achievable. The problem was that I was not aware of the mapping between ffmpeg command-line parameters and the API. Eventually, I looked at the source for FFmpeg’s libx264.c (https://www.ffmpeg.org/doxygen/2.4/libx264_8c_source.html <https://www.ffmpeg.org/doxygen/2.4/libx264_8c_source.html>), and found that there’s a rather large set of parameters/controls/options for x264 that are not part of the general encoder API:
754 <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#a384ff94b5139f441c282c9b4c7b985e6> static const AVOption <https://www.ffmpeg.org/doxygen/2.4/structAVOption.html> options <https://www.ffmpeg.org/doxygen/2.4/ffmpeg_8h.html#abdbaa7127ef32aa4c435f3c609b4c2cd>[] = {
<> 755 { "preset", "Set the encoding preset (cf. x264 --fullhelp)", OFFSET <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#ad12dce0a7bf9d908b172a28155b3d261>(preset <https://www.ffmpeg.org/doxygen/2.4/vf__curves_8c.html#a7f1bb62735b2e1288baece1f7e14514f>), AV_OPT_TYPE_STRING <https://www.ffmpeg.org/doxygen/2.4/group__avoptions.html#ggabd75aa30eb8ad6387672df9a1fa79444afadddce95ad3b690dd38644b458b96c4>, { .str = "medium" }, 0, 0, VE <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#a3dcf4acb9f7d9c57f43802a9a1ac4ee6>},
<> 756 { "tune", "Tune the encoding params (cf. x264 --fullhelp)", OFFSET <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#ad12dce0a7bf9d908b172a28155b3d261>(tune), AV_OPT_TYPE_STRING <https://www.ffmpeg.org/doxygen/2.4/group__avoptions.html#ggabd75aa30eb8ad6387672df9a1fa79444afadddce95ad3b690dd38644b458b96c4>, { 0 }, 0, 0, VE <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#a3dcf4acb9f7d9c57f43802a9a1ac4ee6>},
<> 757 { "profile", "Set profile restrictions (cf. x264 --fullhelp) ", OFFSET <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#ad12dce0a7bf9d908b172a28155b3d261>(profile), AV_OPT_TYPE_STRING <https://www.ffmpeg.org/doxygen/2.4/group__avoptions.html#ggabd75aa30eb8ad6387672df9a1fa79444afadddce95ad3b690dd38644b458b96c4>, { 0 }, 0, 0, VE <https://www.ffmpeg.org/doxygen/2.4/libx264_8c.html#a3dcf4acb9f7d9c57f43802a9a1ac4ee6>},
and many more...
Usage is like this:
av_opt_set(c->priv_data, "crf", "1", AV_OPT_SEARCH_CHILDREN);
Of particular importance for controlling quality are the “crf” (Constant Rate Factor) or “qp” (Quantization Parameter) options, which are named as they are in the command-line ffmpeg (and thus referenced in numerous posts/forums). Notably, “crf” causes variable bitrate, and “qp” causes a constant bitrate.
To avoid repeating what’s in a lot of forum posts, I’ll just note that “crf” defaults to 23, which seems not to be adequate for my purposes (very large images with high-frequency content, used as still images to be encoded). Using smaller values increases quality via increasing the bitrate (and other things?). A “0” value is lossless, but not all profiles support this. A “1” value gives me more sampling than I need; I’ve not played with the value to determine what would be optimal.
I’ve also tried “qp”. Again with smaller values, the quality can be drastically improved. Some forum posts suggest “crf” is a better approach, as it’s adaptive and generally results in smaller files for the same quality results; I found this to be accurate.
So, for both “crf” and “qp”, I can tune the quality by using different values, but the resulting bitrate is determined algorithmically by the encoder.
According to ffmpeg command-line documentation, I should be able to explicitly set the bitrate with the equivalent to the “-b” option or similar, but I’m not able to find this in the AVOptions structure in libx264.c. And, as I’ve stated before, setting the bitrate with the “obvious” encoder API has no effect. It certainly seems like one ought to be able to just force a particular bitrate, but I still don’t know how to do this.
As a practical matter, forcing a particular bitrate may be a non-issue, as dialing in the right “crf” or “qp” value can yield a range of quality levels. But still, it would be nice to know how to do this…
— Philip
> On Apr 9, 2015, at 1:10 AM, Max Vlasov <max.vlasov at gmail.com> wrote:
>
> Then I'd recommend creating a simple "all in one procedure" example containing all affected codes from your sources to reproduce the problem. Seeing all of them in one place sometimes help determining what the problem is. If the problem still exists and you don't see what is wrong then posting this fragment here might help
>
> On Wed, Apr 8, 2015 at 10:30 PM, Philip Schneider <pjschneider at earthlink.net <mailto:pjschneider at earthlink.net>> wrote:
> Hi -
>
> Indeed, that’s what I’m doing. This seems to have solved the same issue for some other folks, but my guess is that some other parameter/setting is necessary, which I don’t have :-(
>
> — Philip
>
>> On Apr 8, 2015, at 6:03 AM, Max Vlasov <max.vlasov at gmail.com <mailto:max.vlasov at gmail.com>> wrote:
>>
>> I think you should set frame pts before avcodec_encode_video2. After avcodec_encode_video2 this value is transferred to the packet by the encoder where it should be converted into the stream time base.
>>
>> On Mon, Apr 6, 2015 at 10:10 PM, Philip Schneider <pjschneider at earthlink.net <mailto:pjschneider at earthlink.net>> wrote:
>> Greetings -
>>
>> I’m attempting to encode a sequence of frames with libx264. For testing, I’m using the sample code from http://www.imc-store.com.au/Articles.asp?ID=276 <http://www.imc-store.com.au/Articles.asp?ID=276>. This is all pretty vanilla FFmpeg API usage, and is similar to what one might write oneself. It produces correct output with libx264, however the bitrate I specify is not honored at all…
>>
>> I can set the bitrate (m_AVIMOV_BPS) to whatever number I choose, and the actual bitrate used is some other value:
>>
>> AVCodec *m_video_codec = avcodec_find_encoder(m_fmt->video_codec);
>> if (!(m_video_codec)) {
>> return;
>> }
>> AVStream *st = avformat_new_stream(m_oc, m_video_codec);
>> AVCodecContext *m_c = st->codec;
>>
>> m_c->codec_id = m_fmt->video_codec;
>> m_c->bit_rate = m_AVIMOV_BPS;
>>
>>
>> Googling this issue, I find that it’s the subject of a lot of discussion. Evidently, simply setting the bit rate in the context is not sufficient — I can set m_AVIMOV_BPS to 400 million, but the bitrate used in encoding ends up being something like 176 Kbps (using the info inspector in one of any number of players/utilities). As well, visually I see significant undersampling artifacts. I assume the effective bitrate used is computed by ffmpeg or the encoder itself, based on some other (default) parameters. But in any case, my value is being ignored, making the output useless to me.
>>
>> One forum I post I found ( http://libav-users.943685.n4.nabble.com/Setting-libx264-bitrate-via-API-td4655453.html <http://libav-users.943685.n4.nabble.com/Setting-libx264-bitrate-via-API-td4655453.html> ) suggests that the reason the encoder (or ffmpeg itself?) is ignoring the bit rate is because of the use of “pts” and “dts” in the encoder and output stream writer. Specifically, the encoder’s input should use, say, integer frame numbers, while the stream writer should use values in its own time base:
>>
>> My code was sending pictures into the encoder using a pts in the stream's
>> time_base of 1/90000 (e.g. 3003, 6006, 9009). The solution was to first
>> rescale the AVFrame's pts from the stream's time_base to the codec time_base
>> to get a simple frame number (e.g. 1, 2, 3).
>>
>> pic->pts = av_rescale_q(pic->pts, ost->time_base, enc->time_base);
>> avcodec_encode_video2(enc, &newpkt, pic, &got_packet_ptr);
>>
>> Then when a packet is received from the encoder, you need to rescale pts and
>> dts back to the stream time_base.
>>
>> newpkt.pts = av_rescale_q(newpkt.pts, enc->time_base, ost->time_base);
>> newpkt.dts = av_rescale_q(newpkt.dts, enc->time_base, ost->time_base);
>> av_interleaved_write_frame(out, &newpkt);
>>
>> However, this is not working for me. I suspect it may be due to other differences between my code and theirs.. :-(
>>
>> In any case, surely someone out there has the understanding (and code snippets?) of how to get the libx264 encoder to honor the specified bitrate? Any help/pointers/advice/code would be greatly appreciated!
>>
>> Thanks!
>>
>>
>>
>> _______________________________________________
>> Libav-user mailing list
>> Libav-user at ffmpeg.org <mailto:Libav-user at ffmpeg.org>
>> http://ffmpeg.org/mailman/listinfo/libav-user <http://ffmpeg.org/mailman/listinfo/libav-user>
>>
>>
>> _______________________________________________
>> Libav-user mailing list
>> Libav-user at ffmpeg.org <mailto:Libav-user at ffmpeg.org>
>> http://ffmpeg.org/mailman/listinfo/libav-user <http://ffmpeg.org/mailman/listinfo/libav-user>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org <mailto:Libav-user at ffmpeg.org>
> http://ffmpeg.org/mailman/listinfo/libav-user <http://ffmpeg.org/mailman/listinfo/libav-user>
>
>
> _______________________________________________
> Libav-user mailing list
> Libav-user at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/libav-user
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20150409/b1f3dc45/attachment.html>
More information about the Libav-user
mailing list