Skip to content

Commit

Permalink
libav_encoder: Allow other libav encoders to be selected.
Browse files Browse the repository at this point in the history
Add a general codec options function that will allow other libav
encoders to be selected correctly.

Signed-off-by: Naushir Patuck <[email protected]>
  • Loading branch information
naushir committed Nov 20, 2023
1 parent dd744cb commit 7dbe5e3
Showing 1 changed file with 29 additions and 24 deletions.
53 changes: 29 additions & 24 deletions encoder/libav_encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,40 @@

namespace {

void encoderOptionsH264M2M(VideoOptions const *options, AVCodecContext *codec)
void encoderOptionsGeneral(VideoOptions const *options, AVCodecContext *codec)
{
codec->pix_fmt = AV_PIX_FMT_DRM_PRIME;
codec->max_b_frames = 0;
codec->framerate = { (int)(options->framerate.value_or(DEFAULT_FRAMERATE) * 1000), 1000 };

if (!options->profile.empty())
{
static const std::map<std::string, int> profile_map = {
{ "baseline", FF_PROFILE_H264_BASELINE },
{ "main", FF_PROFILE_H264_MAIN },
{ "high", FF_PROFILE_H264_HIGH }
};

auto it = profile_map.find(options->profile);
if (it == profile_map.end())
throw std::runtime_error("libav: no such profile " + options->profile);

codec->profile = it->second;
}

codec->level = options->level.empty() ? FF_LEVEL_UNKNOWN : std::stof(options->level) * 10;
codec->gop_size = options->intra ? options->intra : (int)(options->framerate.value_or(DEFAULT_FRAMERATE));

if (options->bitrate)
codec->bit_rate = options->bitrate.bps();
}

void encoderOptionsLibx264(VideoOptions const *options, AVCodecContext *codec)
void encoderOptionsH264M2M(VideoOptions const *options, AVCodecContext *codec)
{
codec->pix_fmt = AV_PIX_FMT_YUV420P;
codec->pix_fmt = AV_PIX_FMT_DRM_PRIME;
codec->max_b_frames = 0;
}

void encoderOptionsLibx264(VideoOptions const *options, AVCodecContext *codec)
{
if (options->bitrate)
codec->bit_rate = options->bitrate.bps();

Expand Down Expand Up @@ -76,8 +97,8 @@ void LibAvEncoder::initVideoCodec(VideoOptions const *options, StreamInfo const
codec_ctx_[Video]->height = info.height;
// usec timebase
codec_ctx_[Video]->time_base = { 1, 1000 * 1000 };
codec_ctx_[Video]->framerate = { (int)(options->framerate.value_or(DEFAULT_FRAMERATE) * 1000), 1000 };
codec_ctx_[Video]->sw_pix_fmt = AV_PIX_FMT_YUV420P;
codec_ctx_[Video]->pix_fmt = AV_PIX_FMT_YUV420P;

if (info.colour_space)
{
Expand Down Expand Up @@ -121,24 +142,8 @@ void LibAvEncoder::initVideoCodec(VideoOptions const *options, StreamInfo const
info.colour_space->range == ColorSpace::Range::Full ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
}

if (!options->profile.empty())
{
static const std::map<std::string, int> profile_map = {
{ "baseline", FF_PROFILE_H264_BASELINE },
{ "main", FF_PROFILE_H264_MAIN },
{ "high", FF_PROFILE_H264_HIGH }
};

auto it = profile_map.find(options->profile);
if (it == profile_map.end())
throw std::runtime_error("libav: no such profile " + options->profile);

codec_ctx_[Video]->profile = it->second;
}

codec_ctx_[Video]->level = options->level.empty() ? FF_LEVEL_UNKNOWN : std::stof(options->level) * 10;
codec_ctx_[Video]->gop_size = options->intra ? options->intra
: (int)(options->framerate.value_or(DEFAULT_FRAMERATE));
// Apply general options.
encoderOptionsGeneral(options, codec_ctx_[Video]);

// Apply any codec specific options:
auto fn = optionsMap.find(options->libav_video_codec);
Expand Down

0 comments on commit 7dbe5e3

Please sign in to comment.