Skip to content

Commit 79ea994

Browse files
mat-hekConnorRigby
authored andcommitted
add filter
1 parent adbd8ab commit 79ea994

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

.formatter.exs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Used by "mix format"
22
[
3-
inputs: ["{mix,.formatter,bundlex}.exs", "{config,lib,test}/**/*.{ex,exs}"]
3+
inputs: ["{mix,.formatter,bundlex}.exs", "{config,lib,test}/**/*.{ex,exs}"],
4+
import_deps: [:membrane_core]
45
]

lib/turbojpeg/filter.ex

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
defmodule Turbojpeg.Filter do
2+
use Membrane.Filter
3+
alias Membrane.Buffer
4+
5+
def_input_pad :input, demand_unit: :buffers, caps: Membrane.Caps.Video.Raw
6+
def_output_pad :output, caps: Membrane.Caps.Video.Raw
7+
8+
def_options quality: [
9+
type: :integer,
10+
spec: Turbojpeg.quality(),
11+
default: 80,
12+
description: "Jpeg encoding quality"
13+
]
14+
15+
@impl true
16+
def handle_demand(:output, size, :buffers, _ctx, state) do
17+
{{:ok, demand: {:input, size}}, state}
18+
end
19+
20+
@impl true
21+
def handle_process(:input, buffer, ctx, state) do
22+
%{caps: caps} = ctx.pads.input
23+
24+
with {:ok, payload} <-
25+
Turbojpeg.yuv_to_jpeg(
26+
buffer.payload,
27+
caps.width,
28+
caps.height,
29+
state.quality,
30+
caps.format
31+
) do
32+
buffer = %Buffer{buffer | payload: payload}
33+
{{:ok, buffer: {:output, buffer}}, state}
34+
else
35+
{:error, _} = error -> {error, state}
36+
end
37+
end
38+
end

lib/turbojpeg/sink.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ defmodule Turbojpeg.Sink do
33
alias Membrane.{Buffer, Time}
44
alias Membrane.Caps.Video.Raw
55

6-
def_input_pad(:input, caps: Raw, demand_unit: :buffers)
6+
def_input_pad :input, caps: Raw, demand_unit: :buffers
77

8-
def_options(
9-
filename: [type: :binary, description: "File to write the jpeg file"],
10-
quality: [type: :integer, description: "Jpeg encoding quality"]
11-
)
8+
def_options filename: [type: :binary, description: "File to write the jpeg file"],
9+
quality: [type: :integer, description: "Jpeg encoding quality"]
1210

1311
@impl true
1412
def handle_init(options) do

0 commit comments

Comments
 (0)