[Libav-user] MPEG-TS muxrate and bufsize setting in lav*

Jason Livingston jettoblack at gmail.com
Tue Jan 1 05:07:29 CET 2013


Happy new year everyone!

I'm trying to encode a CBR transport stream.  It works fine using
ffmpeg CLI, but I think I am not setting some parameters correctly
when I try to do it using lav*.

e.g. to encode a 2.2Mbit CBR TS with 2.0Mbit CBR video and 64k audio:

ffmpeg -i source -codec:v libx264 -b:v 2M -minrate 2M -maxrate 2M
-bufsize 1M -muxrate 2.2M -codec:a aac -b:a 64k -ac 2 -ar 48000 out.ts

This works ok.  When I try to replicate the same settings in code:

vCC->bit_rate = 2000000;   // 2M
vCC->rc_min_rate = vCC->bit_rate;
vCC->rc_max_rate = vCC->bit_rate;
vCC->rc_buffer_size = 1000000;  // 1M
...
AVDictionary *dict = NULL;
av_dict_set(&dict, "muxrate", "2200000", 0);  // 2.2M
avformat_write_header(out, &dict);
...

I think I'm setting the codec bitrate options correctly since I get
this debug output from av_dump_format():
Output #0, mpegts, to 'Stream 0':
    Stream #0:0: Video: h264, yuv420p, 1280x720 [SAR 1:1 DAR 16:9],
q=-1--1, 2000 kb/s, 90k tbn, 29.97 tbc
    Stream #0:1: Audio: aac, 48000 Hz, stereo, fltp, 64 kb/s

Also I think muxrate is set as intended because I get the message:
[mpegts @ 0x103021800] muxrate 2200000, pcr every 58 pkts, sdt every
1462, pat/pmt every 1462 pkts


However, even if I set the muxrate to an absurdly large value like 10x
the video+audio bitrate, I get these errors on almost every packet:
[mpegts @ 0x103021800] dts < pcr, TS is invalid

This error usually indicates that the encoded bitrate spikes higher
than will fit into the specified muxrate.

I get the same error if I omit the -bufsize parameter to ffmpeg, and
in my code I get the errors regardless of what rc_buffer_size is set
to (or not setting it at all), so I suspect I am not setting the same
variable that bufsize sets, or I'm not using the right value for it.
I looked through ffmpeg.c and couldn't easily find how the -bufsize
parameter gets applied as a format or codec option, or whether ffmpeg
is tweaking my values. However I'm not positive that bufisize is even
related to the problem.

Do I need to set any other rate control or buffer related options for
this to work, such as rc_buffer_aggressivity,
rc_initial_buffer_occupancy, etc.?  And if so, what are the
recommended values?

I'm happy to post a self contained example that compiles, if it would help.

Any ideas?  Thanks!


More information about the Libav-user mailing list