how to set crf for vp8 encoder in ffmpeg programmatically?

996 views
Skip to first unread message

YIRAN LI

unread,
May 20, 2013, 4:22:23 AM5/20/13
to webm-d...@webmproject.org
Hi guys,

I've downloaded latest libvpx code and built it into ffmpeg, so that my application can use ffmpeg dlls.

Vp8 encoding works correctly and my application can generate webm videos.

But what I want to know is, how can I set crf when encoding?

What I do in my application is just calling ffmpeg interface
1. AVStream* pVideoStream = av_new_stream(pFormatCtx, 0);
2. AVCodec* pVideoCodec = avcodec_find_encoder(AVCODE_ID_VP8)
3. AVCodecContext* pVideoCodecCtx = pVideoStream->codec;

   pVideoCodexCtx->bit_rate =
                           ->rc_min_rate= 
                           ->rc_max_rate=   (set all these bit rate control param)

  I checked libvpxenc.c under libavcodec and seems when initing the encoder, it doesn't directly refer to any member 
 of AVCodecContext to set crf.

 So could anyone tell me, how to set crf when open vp8 encoder programmatically?

 Great thanks!

I

YIRAN LI

unread,
May 20, 2013, 5:30:15 AM5/20/13
to webm-d...@webmproject.org
Hi Guys,

I checked the patch for ffmpeg-0.6.3 (seems part of changes have been merged into latest ffmpeg code).

I can see this change

else if (avctx->crf)
+        enccfg.rc_end_usage = VPX_CQ;

and what's in ffmpeg now is
else if (ctx->crf)
        enccfg.rc_end_usage = VPX_CQ;

But I didn't see in the patch that crf was added as a member of AVCodecContext.

Here I'd like to know, if I can add crf into AVCodecContext definition and change that line
so that VP8 encode can directly be initialized by AVCodecContext.crf?

Great thanks!

John Koleszar

unread,
May 20, 2013, 11:46:13 AM5/20/13
to WebM Discussion
It sounds like this change is more appropriate for the ffmpeg/libav
lists? We don't have any control over that structure.

One thing libvpx related that I'll note: crf doesn't work like other
codecs. It's how libavcodec maps to what libvpx calls "Constrained
Quality (CQ) mode." It's not a single parameter you can tune to change
quality. Bitrate is still required. See
http://blog.webmproject.org/2011/03/vp8-constrained-quality-cq-encoding.html
for some discussion.
> --
> You received this message because you are subscribed to the Google Groups
> "WebM Discussion" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to webm-discuss...@webmproject.org.
> To post to this group, send email to webm-d...@webmproject.org.
> Visit this group at
> http://groups.google.com/a/webmproject.org/group/webm-discuss/?hl=en.
> For more options, visit
> https://groups.google.com/a/webmproject.org/groups/opt_out.
>
>

YIRAN LI

unread,
May 21, 2013, 10:38:52 PM5/21/13
to webm-d...@webmproject.org
Hi John,

Thanks for your reply, and now I turn to use VBR with a target bitrate.

While ffmpeg command line works good with -b:v

when I set AVCodecContext->bit_rate with desired bitrate and do conversion programmatically,
generated videos are pretty much of the same quality, and bitrate is far from what I want (for example,
I generated a 810k video while what I want is 32k)

Could you please let me know if you can, what else arguments do I need to set in AVCodecContext
to achieve desired bitrate?

Thanks!

James Zern

unread,
May 22, 2013, 2:20:57 PM5/22/13
to WebM Discussion
On Tue, May 21, 2013 at 7:38 PM, YIRAN LI <mrfun...@gmail.com> wrote:
> Hi John,
>
> Thanks for your reply, and now I turn to use VBR with a target bitrate.
>
> While ffmpeg command line works good with -b:v
>
> when I set AVCodecContext->bit_rate with desired bitrate and do conversion
> programmatically,
> generated videos are pretty much of the same quality, and bitrate is far
> from what I want (for example,
> I generated a 810k video while what I want is 32k)
>
> Could you please let me know if you can, what else arguments do I need to
> set in AVCodecContext
> to achieve desired bitrate?
>

The mappings from ffmpeg to libvpx are documented here [1] with some
encoding guidance here [2]. Also note you can dump the libvpx settings
with -loglevel debug.
It's not clear, to me, from the above if you're seeing different
behavior through the API than the command line, but I'd suggest
refining your settings with ffmpeg first before moving to the API.

[1] http://www.ffmpeg.org/ffmpeg-all.html#libvpx
[2] http://www.webmproject.org/docs/encoder-parameters/
Reply all
Reply to author
Forward
0 new messages