You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+60-3Lines changed: 60 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,11 +34,68 @@ and follow the [instructions here](https://github.com/ros-misc-utilities/.github
34
34
35
35
Make sure to source your workspace's ``install/setup.bash`` afterwards.
36
36
37
-
## Parameters
37
+
## ROS Parameters
38
38
39
-
This package has no parameters. It is the upper layer's responsibility to e.g. manage the mapping between encoder and decoder, i.e. to tell the decoder class which libav decoder should be used for the decoding, or to set the encoding parameters.
39
+
This package does not expose ROS parameters. It is the upper layer's responsibility to e.g. manage the mapping between encoder and decoder, i.e. to tell the decoder class which libav decoder should be used for the decoding, or to set the encoding parameters.
40
40
41
-
### How to use a custom version of libav (aka ffmpeg)
41
+
42
+
## API usage
43
+
44
+
### Encoder
45
+
46
+
Using the encoder involves the following steps:
47
+
- instantiating the ``Encoder`` object.
48
+
- setting properties like the libav encoder to use, the encoding formats, and AV options.
49
+
- initializing the encoder object. This requires knowledge of the image size and therefore
50
+
can only be done when the first image is available. Note that many properties
51
+
(encoding formats etc) must have been set *before* initializing the encoder.
52
+
- feeding images to the encoder (and handling the callbacks when encoded packets become available)
53
+
- flushing the encoder (may result in additional callbacks with encoded packets)
54
+
- destroying the ``Encoder`` object.
55
+
56
+
The ``Encoder`` class description has a short example code snippet.
57
+
58
+
When using libav it is important to understand the difference between the encoder and the codec. The *codec* is the standardized format in which images are encoded, for instance ``h264`` or ``hevc``. The *encoder* is a software module that can encode images for a given codec. For instance ``libx264``, ``libx264rgb``, ``h264_nvenc``, and ``h264_vaapi`` are all *encoders* that encode for the *codec* h264.
59
+
Some of the encoders are hardware accelerated, some can handle more image formats than others, but in the end they all encode video for a specific codec. You set the libav encoder with the ``setEncoder()`` method.
60
+
61
+
For the many AV options available for various libav encoders, and for ``qmax``, ``bit_rate`` and similar settings please refer to the ffmpeg documentation.
62
+
63
+
The topic of "pixel formats" (also called "encodings") can be very confusing and frustrating, so here a few lines about it.
64
+
- Images in ROS arrive as messages that are encoded in one of the formats allowed by the [sensor\_msgs/Image encodings](https://github.com/ros2/common_interfaces/blob/a2ef867438e6d4eed074d6e3668ae45187e7de86/sensor_msgs/include/sensor_msgs/image_encodings.hpp). If you pass that message in via the ``encodeImage(const Image & msg)`` message, it will first will be converted to an opencv matrix using the [cv_bridge](https://github.com/ros-perception/vision_opencv).
65
+
If you don't set the ``cv_bridge_target_format`` via ``setCVBridgeTargetFormat(const std::string & fmt)``, the image will be converted to the default format of ``bgr8``. Depending on how performance sensitive you are (or if you have a ``mono8`` (gray) image!), this may not be what you want. Ideally, you should pick a ``cv_bridge_target_format`` that can be directly used by the libav decoder that you have chosen. You can use ffmpeg to list the formats that the libav encoder directly supports:
66
+
```
67
+
ffmpeg -h encoder=libx264 | grep formats
68
+
```
69
+
Note that the ffmpeg/libav format strings often diverge from the ROS encoding strings, so some guesswork and experimentation may be necessary to pick the right ``cv_bridge_target_format``.
70
+
If you choose to bypass the cv\_bridge conversion by feeding the images to the encoder directly via the ``encodeImage(const cv::Mat & img ...)`` method, you must still set the ``cv_bridge_target_format`` such that encoder knows what format the ``img`` argument has.
71
+
- Once the image is available in ``cv_bridge_target_format`` the encoder may perform another conversion to an image format that the libav encoder supports, the ``av_source_pixel_format``. Again, ``ffmpeg -h encoder=<your encoder>`` shows the supported formats. If you don't set the ``av_source_pixel_format`` with ``setAVSourcePixelFormat()``, the encoder will try to pick one that is supported by the libav encoder. That often works but may not be optimal.
72
+
- Finally, the libav encoder produces a packet in its output codec, e.g. ``h264``, ``hevc`` etc. This encoding format is provided as the ``codec`` parameter when the encoder calls back with the encoded packet. Later, the codec needs to be provided to the decoder so it can pick a suitable libav decoder.
By properly choosing ``cv_bridge_target_format`` and ``av_source_pixel_format`` two of those conversions
79
+
may become no-ops, but to what extend the cv\_bridge and libav actually recognize and optimize for this has not been looked into yet.
80
+
81
+
Note that only very few combinations of libav encoders, ``cv_bridge_target_format`` and ``av_source_pixel_format`` have been tested. Please provide feedback if you observe crashes or find obvious bugs. PRs are always appreciated!
82
+
83
+
### Decoder
84
+
85
+
Using the decoder involves the following steps:
86
+
- instantiating the ``Decoder`` object.
87
+
- if so desired, setting the ROS output (image encoding) format.
88
+
- initializing the decoder object. For this you need to know the encoding (codec, e.g. "h264").
89
+
During initialization you also have to present an ordered list of libav decoders that
90
+
should be used. If an empty list is provided, the decoder will attempt to find a suitable
91
+
libav decoder based on the encoding, with a preference for hardware accelerated decoding.
92
+
- feeding encoded packets to the decoder (and handling the callbacks
93
+
when decoded images become available)
94
+
- flushing the decoder (may result in additional callbacks with decoded images)
95
+
- destroying the ``Decoder`` object.
96
+
The ``Decoder`` class description has a short example code snippet.
97
+
98
+
## How to use a custom version of libav (aka ffmpeg)
42
99
43
100
Compile *and install* ffmpeg. Let's say the install directory is
44
101
``/home/foo/ffmpeg/build``, then for it to be found while building,
0 commit comments