Skip to content

Commit ab76dd8

Browse files
shternAlexey Shtern
and
Alexey Shtern
authored
Configurable CRF (#34)
* Added CRF support * fixed gop parameter in the README examples * PR review fixes --------- Co-authored-by: Alexey Shtern <alexey.shtern@xtend.me>
1 parent f749b92 commit ab76dd8

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,16 @@ The plugin has a few parameters that allow for some amount of control.
8484
- ``gop_size``: The number of frames inbetween keyframes. Default is ``15``.
8585
The larger this number the more latency you will have, but also the more efficient
8686
the transmission becomes.
87-
- ``bit_rate``: The max bit rate [in bits/s] that the encoding will target. Default is ``8242880`.
87+
- ``bit_rate``: The max bit rate [in bits/s] that the encoding will target. Default is ``8242880``.
88+
- ``crf``: Constant Rate Factor, affects the image quality. Value range is ``[0, 51]``; ``0`` is lossless, ``23`` is default, ``51`` is worst quality.
8889

8990
The parameters are under the ``ffmpeg_image_transport`` variable block. So if you launch
9091
your publisher node (camera driver), you can give it a parameter list on the way like so:
9192
```
9293
parameters=[{'ffmpeg_image_transport.encoding': 'hevc_nvenc',
9394
'ffmpeg_image_transport.profile': 'main',
9495
'ffmpeg_image_transport.preset': 'll',
95-
'ffmpeg_image_transport.gop': 15}]
96+
'ffmpeg_image_transport.gop_size': 15}]
9697
```
9798

9899
### Subscriber (viewer)
@@ -189,7 +190,7 @@ nvmpi like so:
189190
parameters=[{'ffmpeg_image_transport.encoding': 'h264_nvmpi',
190191
'ffmpeg_image_transport.profile': 'main',
191192
'ffmpeg_image_transport.preset': 'll',
192-
'ffmpeg_image_transport.gop': 15}]
193+
'ffmpeg_image_transport.gop_size': 15}]
193194
```
194195

195196

include/ffmpeg_image_transport/ffmpeg_encoder.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class FFMPEGEncoder
138138
std::string preset_; // e.g. "slow", "medium", "lossless"
139139
std::string profile_; // e.g. "main", "high", "rext"
140140
std::string tune_; // e.g. "tune"
141+
std::string crf_; // Range is [0, 51]; 0 is lossless, 23 is default, 51 is worst quality.
141142
std::string delay_; // default is 4 frames for parallel processing. 0 is lowest latency
142143
int qmax_{0}; // max allowed quantization. The lower the better quality
143144
int GOPSize_{15}; // distance between two keyframes

src/ffmpeg_encoder.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ void FFMPEGEncoder::setParameters(rclcpp::Node * node)
9696
preset_ = get_safe_param<std::string>(node, ns + "preset", "");
9797
tune_ = get_safe_param<std::string>(node, ns + "tune", "");
9898
delay_ = get_safe_param<std::string>(node, ns + "delay", "");
99+
crf_ = get_safe_param<std::string>(node, ns + "crf", "");
99100
qmax_ = get_safe_param<int>(node, ns + "qmax", 10);
100101
bitRate_ = get_safe_param<int64_t>(node, ns + "bit_rate", 8242880);
101102
GOPSize_ = get_safe_param<int64_t>(node, ns + "gop_size", 15);
@@ -227,6 +228,7 @@ void FFMPEGEncoder::doOpenCodec(int width, int height)
227228
setAVOption("preset", preset_);
228229
setAVOption("tune", tune_);
229230
setAVOption("delay", delay_);
231+
setAVOption("crf", crf_);
230232
RCLCPP_DEBUG(
231233
logger_,
232234
"codec: %10s, profile: %10s, preset: %10s,"

0 commit comments

Comments
 (0)