Skip to content

Commit

Permalink
encoder: Add logic to auto-detect H.264 encoder type
Browse files Browse the repository at this point in the history
Add a function to automatically select the H.264 codec based on the
platform availability.  The hardware H.264 codec is preferred over the
sofware x264/libav codec.

The "auto" codec option is now the default.

Signed-off-by: Naushir Patuck <naush@raspberrypi.com>
  • Loading branch information
naushir committed Apr 11, 2023
1 parent b90f65a commit 213b166
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
31 changes: 29 additions & 2 deletions encoder/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
*/

#include <cstring>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <linux/videodev2.h>

#include "encoder.hpp"
#include "h264_encoder.hpp"
Expand All @@ -16,12 +21,34 @@
#include "libav_encoder.hpp"
#endif

Encoder *Encoder::Create(VideoOptions const *options, const StreamInfo &info)
Encoder *h264_codec_select(VideoOptions *options, const StreamInfo &info)
{
const char hw_codec[] = "/dev/video11";
struct v4l2_capability caps;
memset(&caps, 0, sizeof(caps));
int fd = open(hw_codec, O_RDWR, 0);
if (fd)
{
int ret = ioctl(fd, VIDIOC_QUERYCAP, &caps);
if (!ret && !strncmp((char *)caps.card, "bcm2835-codec-encode", sizeof(caps.card)))
return new H264Encoder(options, info);
}

#if LIBAV_PRESENT
// No hardware codec available, use x264 through libav.
options->libav_video_codec = "libx264";
return new LibAvEncoder(options, info);
#endif

throw std::runtime_error("Unable to find an appropriate H.264 codec");
}

Encoder *Encoder::Create(VideoOptions *options, const StreamInfo &info)
{
if (strcasecmp(options->codec.c_str(), "yuv420") == 0)
return new NullEncoder(options);
else if (strcasecmp(options->codec.c_str(), "h264") == 0)
return new H264Encoder(options, info);
return h264_codec_select(options, info);
#if LIBAV_PRESENT
else if (strcasecmp(options->codec.c_str(), "libav") == 0)
return new LibAvEncoder(options, info);
Expand Down
2 changes: 1 addition & 1 deletion encoder/encoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef std::function<void(void *, size_t, int64_t, bool)> OutputReadyCallback;
class Encoder
{
public:
static Encoder *Create(VideoOptions const *options, StreamInfo const &info);
static Encoder *Create(VideoOptions *options, StreamInfo const &info);

Encoder(VideoOptions const *options) : options_(options) {}
virtual ~Encoder() {}
Expand Down

0 comments on commit 213b166

Please sign in to comment.