Controlling bitrate

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Controlling bitrate

musikit
Hello,

im trying to create a software collaboration program to allow  
streaming audio/video using FFMPEG.

My test app has a drop down box for which the user will eventually  
select the bitrate the want to broadcast at. my problem is no matter  
what i do to these variables they just dont seem to control the  
coder's bitrate

AVCodecContext.bit_rate = (preferredKbps * 1000);
AVCodecContext.bit_rate_tolerance = (preferredKbps * 1000);
AVCodecContext.rc_max_rate = (preferredKbps * 1000);

again no matter what i do to these variables they dont seem to  
control the bitrate. i have tried h264, ffv1, and flv1.

i would really appreciate someone helping understand how to properly  
set up my encoder settings to get a decent picture quality and  
directly control the bitrate. i have search for many hours for  
tutorials and samples that would show me how to directly control the  
bitrate but have been very unsuccessful.

thank you to anyone that can assist.

Pete
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Controlling bitrate

Tom Bilan
 

> Hello,
>
> im trying to create a software collaboration program to allow
> streaming audio/video using FFMPEG.
>
> My test app has a drop down box for which the user will eventually
> select the bitrate the want to broadcast at. my problem is no matter
> what i do to these variables they just dont seem to control the
> coder's bitrate
>
> AVCodecContext.bit_rate = (preferredKbps * 1000);
> AVCodecContext.bit_rate_tolerance = (preferredKbps * 1000);
> AVCodecContext.rc_max_rate = (preferredKbps * 1000);
>
> again no matter what i do to these variables they dont seem to
> control the bitrate. i have tried h264, ffv1, and flv1.
>
> i would really appreciate someone helping understand how to properly
> set up my encoder settings to get a decent picture quality and
> directly control the bitrate. i have search for many hours for
> tutorials and samples that would show me how to directly control the
> bitrate but have been very unsuccessful.
>
> thank you to anyone that can assist.
>
> Pete

If your -qmax is too low then that seems to put a lower bound on
bitrate.  Try using -qmax 51 and see if your bitrate changes.

Also, when posting please show the ffmpeg.exe command and full output.

Tom
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Controlling bitrate

musikit
Tom,

i appreciate the help. that unfortunately didnt do anything but make  
the video quality go down.

i am not using the command line version of ffmpeg. i am trying to use  
ffmpeg in my C++ project. i tried the dev email list and they shipped  
me over here.

my encoder settings are as follows (this is for FLV but would very  
much prefer to use h.264)

AVCodecContext.width = frameWidth;
AVCodecContext.height = frameHeight;
AVCodecContext.gop_size = 1;
AVCodecContext.pix_fmt = PIX_FMT_YUV420P;
AVCodecContext.bit_rate = (preferredKbps * 1000);
AVCodecContext.bit_rate_tolerance = (encoderContext->bit_rate);
AVCodecContext.rc_max_rate = (preferredKbps * 1000);
AVCodecContext.rc_min_rate = AVCodecContext. rc_max_rate;
AVCodecContext.rc_buffer_size = 1000000;
AVCodecContext.time_base.num = 1;
AVCodecContext.time_base.den = framesPerSecond;
AVCodecContext.crf = 0.0f;
AVCodecContext.cqp = -1;
AVCodecContext.i_quant_factor = 10.0f;
AVCodecContext.b_quant_factor = 0.0f;
AVCodecContext.rc_initial_buffer_occupancy = 0;
AVCodecContext.rc_buffer_size = 1000000;
AVCodecContext.me_method = ME_UMH;
AVCodecContext.qmin = 1;
AVCodecContext.qmax = 5;
AVCodecContext.max_qdiff = 4;
AVCodecContext.max_b_frames = 0;


Pete

On Dec 26, 2007, at 11:25 AM, Tom Bilan wrote:

>
>> Hello,
>>
>> im trying to create a software collaboration program to allow
>> streaming audio/video using FFMPEG.
>>
>> My test app has a drop down box for which the user will eventually
>> select the bitrate the want to broadcast at. my problem is no matter
>> what i do to these variables they just dont seem to control the
>> coder's bitrate
>>
>> AVCodecContext.bit_rate = (preferredKbps * 1000);
>> AVCodecContext.bit_rate_tolerance = (preferredKbps * 1000);
>> AVCodecContext.rc_max_rate = (preferredKbps * 1000);
>>
>> again no matter what i do to these variables they dont seem to
>> control the bitrate. i have tried h264, ffv1, and flv1.
>>
>> i would really appreciate someone helping understand how to properly
>> set up my encoder settings to get a decent picture quality and
>> directly control the bitrate. i have search for many hours for
>> tutorials and samples that would show me how to directly control the
>> bitrate but have been very unsuccessful.
>>
>> thank you to anyone that can assist.
>>
>> Pete
>
> If your -qmax is too low then that seems to put a lower bound on
> bitrate.  Try using -qmax 51 and see if your bitrate changes.
>
> Also, when posting please show the ffmpeg.exe command and full output.
>
> Tom
> _______________________________________________
> ffmpeg-user mailing list
> [hidden email]
> http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user

Peter Wilkes
973-849-0500 x525
[hidden email]



_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Controlling bitrate

Stefano Sabatini
On date Wednesday 2007-12-26 11:44:09 -0500, Peter Wilkes encoded:

> Tom,
>
> i appreciate the help. that unfortunately didnt do anything but make  
> the video quality go down.
>
> i am not using the command line version of ffmpeg. i am trying to use  
> ffmpeg in my C++ project. i tried the dev email list and they shipped  
> me over here.
>
> my encoder settings are as follows (this is for FLV but would very  
> much prefer to use h.264)
>
> AVCodecContext.width = frameWidth;
> AVCodecContext.height = frameHeight;
> AVCodecContext.gop_size = 1;
                 ^^^^^^^^^^^^

You're forcing every frame to be an I-frame, no surprise ffmpeg can't
respect the bitrate constraints.

> AVCodecContext.pix_fmt = PIX_FMT_YUV420P;
> AVCodecContext.bit_rate = (preferredKbps * 1000);

Why are you multiplying by 1000? The bit_rate field takes _bits per
seconds_, maybe you should divide by 1000 instead.

> AVCodecContext.bit_rate_tolerance = (encoderContext->bit_rate);
> AVCodecContext.rc_max_rate = (preferredKbps * 1000);
> AVCodecContext.rc_min_rate = AVCodecContext. rc_max_rate;
> AVCodecContext.rc_buffer_size = 1000000;
> AVCodecContext.time_base.num = 1;
> AVCodecContext.time_base.den = framesPerSecond;
> AVCodecContext.crf = 0.0f;
> AVCodecContext.cqp = -1;
> AVCodecContext.i_quant_factor = 10.0f;
                                  ^^^^^

Here you're saying to use quantization values for I-frame 10 times
greater than those for the P-frames (while default is 0.8), it doesn't
seem like a good idea (you're going to get a very bad I-frame
quality with a blinking effect).

> AVCodecContext.b_quant_factor = 0.0f;
> AVCodecContext.rc_initial_buffer_occupancy = 0;
> AVCodecContext.rc_buffer_size = 1000000;
> AVCodecContext.me_method = ME_UMH;
> AVCodecContext.qmin = 1;
> AVCodecContext.qmax = 5;
> AVCodecContext.max_qdiff = 4;
> AVCodecContext.max_b_frames = 0;
>
>
> Pete
>
> On Dec 26, 2007, at 11:25 AM, Tom Bilan wrote:

Please *don't top post*.

HTH, regards.
--
ffmpeg-user random tip #15
Please submit bugreports to:
http(s)://roundup.mplayerhq.hu/roundup/ffmpeg/
instead of this mailinglist.
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Controlling bitrate

musikit

On Dec 27, 2007, at 3:28 AM, Stefano Sabatini wrote:

> On date Wednesday 2007-12-26 11:44:09 -0500, Peter Wilkes encoded:
>> Tom,
>>
>> i appreciate the help. that unfortunately didnt do anything but make
>> the video quality go down.
>>
>> i am not using the command line version of ffmpeg. i am trying to use
>> ffmpeg in my C++ project. i tried the dev email list and they shipped
>> me over here.
>>
>> my encoder settings are as follows (this is for FLV but would very
>> much prefer to use h.264)
>>
>> AVCodecContext.width = frameWidth;
>> AVCodecContext.height = frameHeight;
>> AVCodecContext.gop_size = 1;
>                  ^^^^^^^^^^^^
>
> You're forcing every frame to be an I-frame, no surprise ffmpeg can't
> respect the bitrate constraints.
>
>> AVCodecContext.pix_fmt = PIX_FMT_YUV420P;
>> AVCodecContext.bit_rate = (preferredKbps * 1000);
>
> Why are you multiplying by 1000? The bit_rate field takes _bits per
> seconds_, maybe you should divide by 1000 instead.
>
>> AVCodecContext.bit_rate_tolerance = (encoderContext->bit_rate);
>> AVCodecContext.rc_max_rate = (preferredKbps * 1000);
>> AVCodecContext.rc_min_rate = AVCodecContext. rc_max_rate;
>> AVCodecContext.rc_buffer_size = 1000000;
>> AVCodecContext.time_base.num = 1;
>> AVCodecContext.time_base.den = framesPerSecond;
>> AVCodecContext.crf = 0.0f;
>> AVCodecContext.cqp = -1;
>> AVCodecContext.i_quant_factor = 10.0f;
>                                   ^^^^^
>
> Here you're saying to use quantization values for I-frame 10 times
> greater than those for the P-frames (while default is 0.8), it doesn't
> seem like a good idea (you're going to get a very bad I-frame
> quality with a blinking effect).
>
>> AVCodecContext.b_quant_factor = 0.0f;
>> AVCodecContext.rc_initial_buffer_occupancy = 0;
>> AVCodecContext.rc_buffer_size = 1000000;
>> AVCodecContext.me_method = ME_UMH;
>> AVCodecContext.qmin = 1;
>> AVCodecContext.qmax = 5;
>> AVCodecContext.max_qdiff = 4;
>> AVCodecContext.max_b_frames = 0;
>>
>>
>> Pete
>>
>> On Dec 26, 2007, at 11:25 AM, Tom Bilan wrote:
>
> Please *don't top post*.
>
> HTH, regards.
> --  
> ffmpeg-user random tip #15
> Please submit bugreports to:
> http(s)://roundup.mplayerhq.hu/roundup/ffmpeg/
> instead of this mailinglist.
> _______________________________________________
> ffmpeg-user mailing list
> [hidden email]
> http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user


Stefano

thank you very much for the information. after loading x264 directly  
and tracing through the ffmpeg code i was able to trace back some of  
the defaults that x264 sets up and set to better values. i still  
however am having a very hard time setting up the video parameters to  
be able to directly control the bitrate of the stream via 1 variable.  
please forgive me i am in no way a video codec expert. however i  
would very much like to get a set of parameters that would allow me  
to change the bitrate simply by changing the AVCodecContext.bit_rate  
variable. is this even possible? if it is not what is the point of  
this variable?

i was multiplying by a 1000 because i store my bit rate in kilo bits  
per second. example and current parameters below.

thank you for any assistance.

int preferredKbps = 64; //64,000 bits per second
int framesPerSecond = 30;

AVCodecContext.gop_size = 250;
AVCodecContext.pix_fmt = PIX_FMT_YUV420P;
AVCodecContext.bit_rate = (preferredKbps * 1000);
AVCodecContext.bit_rate_tolerance = (preferredKbps * 1000);
AVCodecContext.rc_min_rate = 0;
AVCodecContext.rc_max_rate = 0;
AVCodecContext.rc_buffer_size = 0;
AVCodecContext.flags |= CODEC_FLAG_PSNR;
AVCodecContext.partitions = X264_PART_I4X4 | X264_PART_I8X8 |  
X264_PART_P8X8 | X264_PART_P4X4 | X264_PART_B8X8;
AVCodecContext.crf = 0.0f;
AVCodecContext.cqp = 26;
AVCodecContext.i_quant_factor = 0.769f;
AVCodecContext.b_quant_factor = 1.4f;
AVCodecContext.rc_initial_buffer_occupancy = (int) ( 0.9 *  
encoderContext->rc_buffer_size);
AVCodecContext.rc_buffer_size = 0;
AVCodecContext.time_base.num = 1;
AVCodecContext.time_base.den = framesPerSecond;
AVCodecContext.me_method = ME_HEX;
AVCodecContext.qmin = 10;
AVCodecContext.qmax = 51;
AVCodecContext.max_qdiff = 4;
AVCodecContext.max_b_frames = 4;



_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user
Reply | Threaded
Open this post in threaded view
|

Re: Controlling bitrate

Stefano Sabatini
On date Thursday 2007-12-27 09:06:28 -0500, Peter Wilkes encoded:

>
> On Dec 27, 2007, at 3:28 AM, Stefano Sabatini wrote:
>
> > On date Wednesday 2007-12-26 11:44:09 -0500, Peter Wilkes encoded:
> >> Tom,
> >>
> >> i appreciate the help. that unfortunately didnt do anything but make
> >> the video quality go down.
> >>
> >> i am not using the command line version of ffmpeg. i am trying to use
> >> ffmpeg in my C++ project. i tried the dev email list and they shipped
> >> me over here.
> >>
> >> my encoder settings are as follows (this is for FLV but would very
> >> much prefer to use h.264)
> >>
> >> AVCodecContext.width = frameWidth;
> >> AVCodecContext.height = frameHeight;
> >> AVCodecContext.gop_size = 1;
> >                  ^^^^^^^^^^^^
> >
> > You're forcing every frame to be an I-frame, no surprise ffmpeg can't
> > respect the bitrate constraints.
> >
> >> AVCodecContext.pix_fmt = PIX_FMT_YUV420P;
> >> AVCodecContext.bit_rate = (preferredKbps * 1000);
> >
> > Why are you multiplying by 1000? The bit_rate field takes _bits per
> > seconds_, maybe you should divide by 1000 instead.
> >
> >> AVCodecContext.bit_rate_tolerance = (encoderContext->bit_rate);
> >> AVCodecContext.rc_max_rate = (preferredKbps * 1000);
> >> AVCodecContext.rc_min_rate = AVCodecContext. rc_max_rate;
> >> AVCodecContext.rc_buffer_size = 1000000;
> >> AVCodecContext.time_base.num = 1;
> >> AVCodecContext.time_base.den = framesPerSecond;
> >> AVCodecContext.crf = 0.0f;
> >> AVCodecContext.cqp = -1;
> >> AVCodecContext.i_quant_factor = 10.0f;
> >                                   ^^^^^
> >
> > Here you're saying to use quantization values for I-frame 10 times
> > greater than those for the P-frames (while default is 0.8), it doesn't
> > seem like a good idea (you're going to get a very bad I-frame
> > quality with a blinking effect).
> >
> >> AVCodecContext.b_quant_factor = 0.0f;
> >> AVCodecContext.rc_initial_buffer_occupancy = 0;
> >> AVCodecContext.rc_buffer_size = 1000000;
> >> AVCodecContext.me_method = ME_UMH;
> >> AVCodecContext.qmin = 1;
> >> AVCodecContext.qmax = 5;
> >> AVCodecContext.max_qdiff = 4;
> >> AVCodecContext.max_b_frames = 0;
> >>
> >>
> >> Pete
> >>
> >> On Dec 26, 2007, at 11:25 AM, Tom Bilan wrote:
> >
> > Please *don't top post*.

[...]

> Stefano
>
> thank you very much for the information. after loading x264 directly  
> and tracing through the ffmpeg code i was able to trace back some of  
> the defaults that x264 sets up and set to better values. i still  
> however am having a very hard time setting up the video parameters to  
> be able to directly control the bitrate of the stream via 1 variable.  
> please forgive me i am in no way a video codec expert. however i  
> would very much like to get a set of parameters that would allow me  
> to change the bitrate simply by changing the AVCodecContext.bit_rate  
> variable. is this even possible? if it is not what is the point of  
> this variable?

bit_rate specifies the target bitrate for VBR (Variable BitRate)
encodings, though not always is possible to achieve such bitrate, it
also depends on the size, framerate and complexity of the video to
encode, ffmpeg simply can't work out magic and compresses a video with
an arbitrarily low bitrate value.

> i was multiplying by a 1000 because i store my bit rate in kilo bits  
> per second. example and current parameters below.

Mmh, yes, it was lame from me not to understand that.

> thank you for any assistance.
>
> int preferredKbps = 64; //64,000 bits per second
> int framesPerSecond = 30;
>
> AVCodecContext.gop_size = 250;
> AVCodecContext.pix_fmt = PIX_FMT_YUV420P;

I'm not sure you can specify the *output* pixel format (yet most
codecs support that format).

> AVCodecContext.bit_rate = (preferredKbps * 1000);
> AVCodecContext.bit_rate_tolerance = (preferredKbps * 1000);
> AVCodecContext.rc_min_rate = 0;
> AVCodecContext.rc_max_rate = 0;
> AVCodecContext.rc_buffer_size = 0;
> AVCodecContext.flags |= CODEC_FLAG_PSNR;
> AVCodecContext.partitions = X264_PART_I4X4 | X264_PART_I8X8 |  
> X264_PART_P8X8 | X264_PART_P4X4 | X264_PART_B8X8;
> AVCodecContext.crf = 0.0f;
> AVCodecContext.cqp = 26;
> AVCodecContext.i_quant_factor = 0.769f;
> AVCodecContext.b_quant_factor = 1.4f;
> AVCodecContext.rc_initial_buffer_occupancy = (int) ( 0.9 *  
> encoderContext->rc_buffer_size);
> AVCodecContext.rc_buffer_size = 0;
> AVCodecContext.time_base.num = 1;
> AVCodecContext.time_base.den = framesPerSecond;
> AVCodecContext.me_method = ME_HEX;
> AVCodecContext.qmin = 10;
> AVCodecContext.qmax = 51;
> AVCodecContext.max_qdiff = 4;
> AVCodecContext.max_b_frames = 4;

They seem reasonable parameters.

Also mbd_decision = FF_MB_DECISION_RD, trellis quantization (flags |=
CODEC_FLAG_TRELLIS_QUANT) and an higher lmax value than the default
(e.g. lmax = 100 * QP2LAMBDA) may help trying to achieve the
target VBR bitrate.

HTH, regards.
--
ffmpeg-user random tip #5
FF-mpeg documentation:
http://ffmpeg.mplayerhq.hu/documentation.html
_______________________________________________
ffmpeg-user mailing list
[hidden email]
http://lists.mplayerhq.hu/mailman/listinfo/ffmpeg-user