Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to specify FFmpeg encoding parameters #114

Merged
merged 5 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions c_src/membrane_h264_ffmpeg_plugin/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ static void set_x264_defaults(AVDictionary **params, char *preset) {
UNIFEX_TERM create(UnifexEnv *env, int width, int height, char *pix_fmt,
char *preset, char *tune, char *profile, int max_b_frames,
int gop_size, int timebase_num, int timebase_den, int crf,
int sc_threshold, ffmpeg_option *ffmpeg_options,
unsigned int ffmpeg_options_length) {
int sc_threshold, ffmpeg_param *ffmpeg_params,
unsigned int ffmpeg_params_length) {
UNIFEX_TERM res;
AVDictionary *params = NULL;
State *state = unifex_alloc_state(env);
Expand Down Expand Up @@ -106,8 +106,8 @@ UNIFEX_TERM create(UnifexEnv *env, int width, int height, char *pix_fmt,

set_x264_defaults(&params, preset);

for (unsigned int i = 0; i < ffmpeg_options_length; i++) {
av_dict_set(&params, ffmpeg_options[i].key, ffmpeg_options[i].value, 0);
for (unsigned int i = 0; i < ffmpeg_params_length; i++) {
av_dict_set(&params, ffmpeg_params[i].key, ffmpeg_params[i].value, 0);
}

av_dict_set(&params, "preset", preset, 0);
Expand Down
4 changes: 2 additions & 2 deletions c_src/membrane_h264_ffmpeg_plugin/encoder.spec.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Membrane.H264.FFmpeg.Encoder.Native
state_type "State"

type(
ffmpeg_option :: %Membrane.H264.FFmpeg.Encoder.FFmpegOption{
ffmpeg_param :: %Membrane.H264.FFmpeg.Encoder.FFmpegParam{
key: string,
value: string
}
Expand All @@ -22,7 +22,7 @@ spec create(
timebase_den :: int,
crf :: int,
sc_threshold :: int,
ffmpeg_options :: [ffmpeg_option]
ffmpeg_params :: [ffmpeg_param]
) :: {:ok :: label, state} | {:error :: label, reason :: atom}

spec get_frame_size(state) :: {:ok :: label, frame_size :: int} | {:error :: label}
Expand Down
40 changes: 34 additions & 6 deletions lib/membrane_h264_ffmpeg/encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ defmodule Membrane.H264.FFmpeg.Encoder do
Please check `t:t/0` for available options.
"""
use Membrane.Filter
require Membrane.Logger, as: Logger
alias __MODULE__.Native
alias Membrane.Buffer
alias Membrane.H264
Expand Down Expand Up @@ -122,22 +123,29 @@ defmodule Membrane.H264.FFmpeg.Encoder do
""",
default: @default_sc_threshold
],
ffmpeg_options: [
ffmpeg_params: [
spec: %{String.t() => String.t()},
description: """
A map with options that will be passed to the encoder.
A map with parameters that are passed to the encoder.

You can use options from: https://ffmpeg.org/ffmpeg-codecs.html#libx264_002c-libx264rgb
and https://ffmpeg.org/ffmpeg-codecs.html#Codec-Options
Remember not to overwrite options that are available as a separate module options
(like `sc_threshold` or `crf`).
varsill marked this conversation as resolved.
Show resolved Hide resolved
""",
default: %{}
]

defmodule FFmpegOption do
defmodule FFmpegParam do
@moduledoc false
@enforce_keys [:key, :value]
defstruct @enforce_keys
end

@impl true
def handle_init(_ctx, opts) do
warn_if_ffmpeg_params_overwrite_module_options(opts.ffmpeg_params)

state =
opts
|> Map.put(:encoder_ref, nil)
Expand Down Expand Up @@ -178,8 +186,8 @@ defmodule Membrane.H264.FFmpeg.Encoder do
frames_per_second when is_integer(frames_per_second) -> {1, frames_per_second}
end

ffmpeg_options =
Enum.map(state.ffmpeg_options, fn {key, value} -> %FFmpegOption{key: key, value: value} end)
ffmpeg_params =
Enum.map(state.ffmpeg_params, fn {key, value} -> %FFmpegParam{key: key, value: value} end)

with buffers <- flush_encoder_if_exists(state),
{:ok, new_encoder_ref} <-
Expand All @@ -196,7 +204,7 @@ defmodule Membrane.H264.FFmpeg.Encoder do
timebase_den,
state.crf,
state.sc_threshold,
ffmpeg_options
ffmpeg_params
) do
stream_format = create_new_stream_format(stream_format, state)
actions = buffers ++ [stream_format: stream_format]
Expand Down Expand Up @@ -274,4 +282,24 @@ defmodule Membrane.H264.FFmpeg.Encoder do
other -> other
end
end

defp warn_if_ffmpeg_params_overwrite_module_options(ffmpeg_params) do
params_to_options_mapping = %{
"crf" => "crf",
"preset" => "preset",
"profile" => "profile",
"tune" => "tune",
"max_b_frames" => "max_b_frames",
"g" => "gop_size",
"sc_threshold" => "sc_threshold"
}

Map.keys(ffmpeg_params)
|> Enum.filter(fn param_name -> params_to_options_mapping[param_name] != nil end)
|> Enum.each(fn param_name ->
Logger.warning(
"The parameter: `#{param_name}` you provided in the `ffmpeg_params` map overwrites the setting from the modules option: `#{params_to_options_mapping[param_name]}`."
)
end)
end
end