Skip to content

Commit

Permalink
Add encoding deadline calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Noarkhh committed Jul 3, 2024
1 parent ad7ef14 commit 5778c96
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
7 changes: 4 additions & 3 deletions lib/membrane_vpx/encoder/vp8_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ defmodule Membrane.VP8.Encoder do
alias Membrane.{VP8, VPx}

def_options encoding_deadline: [
spec: Membrane.Time.t(),
default: Membrane.Time.microsecond(),
inspector: &Membrane.Time.inspect/1,
spec: Membrane.Time.t() | :auto,
default: :auto,
description: """
Determines how long should it take the encoder to encode a frame.
The longer the encoding takes the better the quality will be. If set to 0 the
encoder will take as long as it needs to produce the best frame possible. Note that
this is a soft limit, there is no guarantee that the encoding process will never exceed it.
If set to `:auto` the deadline will be calculated based on the framerate provided by
incoming stream format. If it's `nil` a fixed deadline of 10ms will be set.
"""
]

Expand Down
7 changes: 4 additions & 3 deletions lib/membrane_vpx/encoder/vp9_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ defmodule Membrane.VP9.Encoder do
alias Membrane.{VP9, VPx}

def_options encoding_deadline: [
spec: Membrane.Time.t(),
default: Membrane.Time.microsecond(),
inspector: &Membrane.Time.inspect/1,
spec: Membrane.Time.t() | :auto,
default: :auto,
description: """
Determines how long should it take the encoder to encode a frame.
The longer the encoding takes the better the quality will be. If set to 0 the
encoder will take as long as it needs to produce the best frame possible. Note that
this is a soft limit, there is no guarantee that the encoding process will never exceed it.
If set to `:auto` the deadline will be calculated based on the framerate provided by
incoming stream format. If it's `nil` a fixed deadline of 10ms will be set.
"""
]

Expand Down
33 changes: 21 additions & 12 deletions lib/membrane_vpx/encoder/vpx_encoder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule Membrane.VPx.Encoder do
alias Membrane.Element.CallbackContext
alias Membrane.VPx.Encoder.Native

@default_encoding_deadline Membrane.Time.milliseconds(10)

defmodule State do
@moduledoc false

Expand Down Expand Up @@ -58,28 +60,35 @@ defmodule Membrane.VPx.Encoder do
output_stream_format =
struct(codec_module, width: width, height: height, framerate: framerate)

{buffers, native} =
encoding_deadline =
case {state.encoding_deadline, framerate} do
{:auto, nil} -> @default_encoding_deadline |> Membrane.Time.as_microseconds(:round)
{:auto, {num, denom}} -> div(denom * 1_000_000, num)
{fixed_deadline, _framerate} -> fixed_deadline |> Membrane.Time.as_microseconds(:round)
end

{buffers, encoder_ref} =
case ctx.pads.input.stream_format do
nil ->
native =
Native.create!(state.codec, width, height, pixel_format, state.encoding_deadline)
^raw_video_format ->
{[], state.encoder_ref}

{[], native}
nil ->
encoder_ref =
Native.create!(state.codec, width, height, pixel_format, encoding_deadline)

%RawVideo{width: ^width, height: ^height, pixel_format: ^pixel_format} ->
{[], state.encoder_ref}
{[], encoder_ref}

_other_format ->
_changed_format ->
buffers = flush(state.encoder_ref)

native =
Native.create!(state.codec, width, height, pixel_format, state.encoding_deadline)
encoder_ref =
Native.create!(state.codec, width, height, pixel_format, encoding_deadline)

{buffers, native}
{buffers, encoder_ref}
end

{[buffer: {:output, buffers}, stream_format: {:output, output_stream_format}],
%{state | encoder_ref: native}}
%{state | encoder_ref: encoder_ref}}
end

@spec handle_buffer(:input, Membrane.Buffer.t(), CallbackContext.t(), State.t()) ::
Expand Down

0 comments on commit 5778c96

Please sign in to comment.