From f73c64312ab7c5a9391d2fe8915c1cc35e088274 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20B=C5=82aszk=C3=B3w?= Date: Mon, 21 Feb 2022 10:52:43 +0100 Subject: [PATCH 1/3] Rename from 'caps' and update template --- .circleci/config.yml | 38 +++- .credo.exs | 187 ++++++++++++++++++ README.md | 8 +- ...aps_video_raw.ex => membrane_raw_video.ex} | 30 ++- mix.exs | 24 ++- mix.lock | 18 +- 6 files changed, 270 insertions(+), 35 deletions(-) create mode 100644 .credo.exs rename lib/{membrane_caps_video_raw.ex => membrane_raw_video.ex} (66%) diff --git a/.circleci/config.yml b/.circleci/config.yml index 34b5331..acad432 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -1,14 +1,38 @@ version: 2.0 jobs: - build: + test: docker: - - image: membrane/bionic-membrane:latest - environment: - MIX_ENV: test - working_directory: '~/app' + - image: membraneframeworklabs/docker_membrane:latest + environment: + MIX_ENV: test + + working_directory: ~/app steps: - - checkout + - checkout - run: mix deps.get - - run: mix format --check-formatted + - run: mix compile --force --warnings-as-errors - run: mix test + + lint: + docker: + - image: membraneframeworklabs/docker_membrane:latest + environment: + MIX_ENV: dev + + working_directory: ~/app + + steps: + - checkout + - run: mix deps.get + - run: mix format --check-formatted + - run: mix compile + - run: mix credo + - run: mix docs && mix docs 2>&1 | (! grep -q "warning:") + +workflows: + version: 2 + build: + jobs: + - test + - lint diff --git a/.credo.exs b/.credo.exs new file mode 100644 index 0000000..917f7b7 --- /dev/null +++ b/.credo.exs @@ -0,0 +1,187 @@ +# This file contains the configuration for Credo and you are probably reading +# this after creating it with `mix credo.gen.config`. +# +# If you find anything wrong or unclear in this file, please report an +# issue on GitHub: https://github.com/rrrene/credo/issues +# +%{ + # + # You can have as many configs as you like in the `configs:` field. + configs: [ + %{ + # + # Run any config using `mix credo -C `. If no config name is given + # "default" is used. + # + name: "default", + # + # These are the files included in the analysis: + files: %{ + # + # You can give explicit globs or simply directories. + # In the latter case `**/*.{ex,exs}` will be used. + # + included: [ + "lib/", + "src/", + "test/", + "web/", + "apps/*/lib/", + "apps/*/src/", + "apps/*/test/", + "apps/*/web/" + ], + excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"] + }, + # + # Load and configure plugins here: + # + plugins: [], + # + # If you create your own checks, you must specify the source files for + # them here, so they can be loaded by Credo before running the analysis. + # + requires: [], + # + # If you want to enforce a style guide and need a more traditional linting + # experience, you can change `strict` to `true` below: + # + strict: false, + # + # To modify the timeout for parsing files, change this value: + # + parse_timeout: 5000, + # + # If you want to use uncolored output by default, you can change `color` + # to `false` below: + # + color: true, + # + # You can customize the parameters of any check by adding a second element + # to the tuple. + # + # To disable a check put `false` as second element: + # + # {Credo.Check.Design.DuplicatedCode, false} + # + checks: [ + # + ## Consistency Checks + # + {Credo.Check.Consistency.ExceptionNames, []}, + {Credo.Check.Consistency.LineEndings, []}, + {Credo.Check.Consistency.ParameterPatternMatching, []}, + {Credo.Check.Consistency.SpaceAroundOperators, []}, + {Credo.Check.Consistency.SpaceInParentheses, []}, + {Credo.Check.Consistency.TabsOrSpaces, []}, + + # + ## Design Checks + # + # You can customize the priority of any check + # Priority values are: `low, normal, high, higher` + # + {Credo.Check.Design.AliasUsage, + [priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]}, + # You can also customize the exit_status of each check. + # If you don't want TODO comments to cause `mix credo` to fail, just + # set this value to 0 (zero). + # + {Credo.Check.Design.TagTODO, [exit_status: 0]}, + {Credo.Check.Design.TagFIXME, []}, + + # + ## Readability Checks + # + {Credo.Check.Readability.AliasOrder, []}, + {Credo.Check.Readability.FunctionNames, []}, + {Credo.Check.Readability.LargeNumbers, []}, + {Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]}, + {Credo.Check.Readability.ModuleAttributeNames, []}, + {Credo.Check.Readability.ModuleDoc, []}, + {Credo.Check.Readability.ModuleNames, []}, + {Credo.Check.Readability.ParenthesesInCondition, []}, + {Credo.Check.Readability.ParenthesesOnZeroArityDefs, parens: true}, + {Credo.Check.Readability.PredicateFunctionNames, []}, + {Credo.Check.Readability.PreferImplicitTry, []}, + {Credo.Check.Readability.RedundantBlankLines, []}, + {Credo.Check.Readability.Semicolons, []}, + {Credo.Check.Readability.SpaceAfterCommas, []}, + {Credo.Check.Readability.StringSigils, []}, + {Credo.Check.Readability.TrailingBlankLine, []}, + {Credo.Check.Readability.TrailingWhiteSpace, []}, + {Credo.Check.Readability.UnnecessaryAliasExpansion, []}, + {Credo.Check.Readability.VariableNames, []}, + {Credo.Check.Readability.WithSingleClause, false}, + + # + ## Refactoring Opportunities + # + {Credo.Check.Refactor.CondStatements, []}, + {Credo.Check.Refactor.CyclomaticComplexity, []}, + {Credo.Check.Refactor.FunctionArity, []}, + {Credo.Check.Refactor.LongQuoteBlocks, []}, + {Credo.Check.Refactor.MapInto, false}, + {Credo.Check.Refactor.MatchInCondition, []}, + {Credo.Check.Refactor.NegatedConditionsInUnless, []}, + {Credo.Check.Refactor.NegatedConditionsWithElse, []}, + {Credo.Check.Refactor.Nesting, []}, + {Credo.Check.Refactor.UnlessWithElse, []}, + {Credo.Check.Refactor.WithClauses, []}, + + # + ## Warnings + # + {Credo.Check.Warning.BoolOperationOnSameValues, []}, + {Credo.Check.Warning.ExpensiveEmptyEnumCheck, []}, + {Credo.Check.Warning.IExPry, []}, + {Credo.Check.Warning.IoInspect, []}, + {Credo.Check.Warning.LazyLogging, false}, + {Credo.Check.Warning.MixEnv, []}, + {Credo.Check.Warning.OperationOnSameValues, []}, + {Credo.Check.Warning.OperationWithConstantResult, []}, + {Credo.Check.Warning.RaiseInsideRescue, []}, + {Credo.Check.Warning.UnusedEnumOperation, []}, + {Credo.Check.Warning.UnusedFileOperation, []}, + {Credo.Check.Warning.UnusedKeywordOperation, []}, + {Credo.Check.Warning.UnusedListOperation, []}, + {Credo.Check.Warning.UnusedPathOperation, []}, + {Credo.Check.Warning.UnusedRegexOperation, []}, + {Credo.Check.Warning.UnusedStringOperation, []}, + {Credo.Check.Warning.UnusedTupleOperation, []}, + {Credo.Check.Warning.UnsafeExec, []}, + + # + # Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`) + + # + # Controversial and experimental checks (opt-in, just replace `false` with `[]`) + # + {Credo.Check.Readability.StrictModuleLayout, + priority: :normal, order: ~w/shortdoc moduledoc behaviour use import require alias/a}, + {Credo.Check.Consistency.MultiAliasImportRequireUse, false}, + {Credo.Check.Consistency.UnusedVariableNames, force: :meaningful}, + {Credo.Check.Design.DuplicatedCode, false}, + {Credo.Check.Readability.AliasAs, false}, + {Credo.Check.Readability.MultiAlias, false}, + {Credo.Check.Readability.Specs, []}, + {Credo.Check.Readability.SinglePipe, false}, + {Credo.Check.Readability.WithCustomTaggedTuple, false}, + {Credo.Check.Refactor.ABCSize, false}, + {Credo.Check.Refactor.AppendSingleItem, false}, + {Credo.Check.Refactor.DoubleBooleanNegation, false}, + {Credo.Check.Refactor.ModuleDependencies, false}, + {Credo.Check.Refactor.NegatedIsNil, false}, + {Credo.Check.Refactor.PipeChainStart, false}, + {Credo.Check.Refactor.VariableRebinding, false}, + {Credo.Check.Warning.LeakyEnvironment, false}, + {Credo.Check.Warning.MapGetUnsafePass, false}, + {Credo.Check.Warning.UnsafeToAtom, false} + + # + # Custom checks can be created using `mix credo.gen.check`. + # + ] + } + ] +} diff --git a/README.md b/README.md index 2f6f740..6bee316 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,10 @@ # Membrane Multimedia Framework: Raw video format definition -[![CircleCI](https://circleci.com/gh/membraneframework/membrane-caps-video-raw.svg?style=svg)](https://circleci.com/gh/membraneframework/membrane-caps-video-raw) +[![Hex.pm](https://img.shields.io/hexpm/v/membrane_raw_video_format.svg)](https://hex.pm/packages/membrane_raw_video_format) +[![API Docs](https://img.shields.io/badge/api-docs-yellow.svg?style=flat)](https://hexdocs.pm/membrane_raw_video_format/) +[![CircleCI](https://circleci.com/gh/membraneframework/membrane_raw_video_format.svg?style=svg)](https://circleci.com/gh/membraneframework/membrane_raw_video_format) -This package provides raw video format definition (so-called caps) for the +This package provides the definition of raw (uncompressed) video frames format for the elements written with [Membrane Multimedia Framework](https://membraneframework.org). ## Installation @@ -16,7 +18,7 @@ reason, just add the following line to your `deps` in the `mix.exs` and run `mix deps.get`. ```elixir -{:membrane_caps_video_raw, "~> 0.1"} +{:membrane_raw_video_format, "~> 0.2"} ``` ## Copyright and License diff --git a/lib/membrane_caps_video_raw.ex b/lib/membrane_raw_video.ex similarity index 66% rename from lib/membrane_caps_video_raw.ex rename to lib/membrane_raw_video.ex index 23c6b2a..a7fb768 100644 --- a/lib/membrane_caps_video_raw.ex +++ b/lib/membrane_raw_video.ex @@ -1,6 +1,6 @@ -defmodule Membrane.Caps.Video.Raw do +defmodule Membrane.RawVideo do @moduledoc """ - This module provides caps struct for raw video frames. + This module provides a struct (`t:#{inspect(__MODULE__)}.t/0`) describing raw video frames. """ require Integer @@ -23,9 +23,10 @@ defmodule Membrane.Caps.Video.Raw do @type framerate_t :: {frames :: non_neg_integer, seconds :: pos_integer} @typedoc """ - Format used to encode color of each pixel in each video frame. + Format used to encode the color of every pixel in each video frame. """ - @type format_t :: :I420 | :I422 | :I444 | :RGB | :BGRA | :RGBA | :NV12 | :NV21 | :YV12 | :AYUV + @type pixel_format_t :: + :I420 | :I422 | :I444 | :RGB | :BGRA | :RGBA | :NV12 | :NV21 | :YV12 | :AYUV @typedoc """ Determines, whether buffers are aligned i.e. each buffer contains one frame. @@ -36,19 +37,22 @@ defmodule Membrane.Caps.Video.Raw do width: width_t(), height: height_t(), framerate: framerate_t(), - format: format_t(), + pixel_format: pixel_format_t(), aligned: aligned_t() } - @enforce_keys [:width, :height, :framerate, :format, :aligned] + @enforce_keys [:width, :height, :framerate, :pixel_format, :aligned] defstruct @enforce_keys + @supported_pixel_formats [:I420, :I422, :I444, :RGB, :BGRA, :RGBA, :NV12, :NV21, :YV12, :AYUV] + @doc """ Simple wrapper over `frame_size/3`. Returns the size of raw video frame in bytes for the given caps. """ - @spec frame_size(t()) :: Bunch.Type.try_t(pos_integer) - def frame_size(%__MODULE__{format: format, width: width, height: height}) do + @spec frame_size(t()) :: {:ok, pos_integer()} | {:error, reason} + when reason: :invalid_dims | :invalid_pixel_format + def frame_size(%__MODULE__{pixel_format: format, width: width, height: height}) do frame_size(format, width, height) end @@ -58,7 +62,9 @@ defmodule Membrane.Caps.Video.Raw do It may result in error when dimensions don't fulfill requirements for the given format (e.g. I420 requires both dimensions to be divisible by 2). """ - @spec frame_size(Raw.format_t(), Raw.width_t(), Raw.height()) :: Bunch.Type.try_t(pos_integer) + @spec frame_size(pixel_format_t(), width_t(), height_t()) :: + {:ok, pos_integer()} | {:error, reason} + when reason: :invalid_dims | :invalid_pixel_format def frame_size(format, width, height) when format in [:I420, :YV12, :NV12, :NV21] and Integer.is_even(width) and Integer.is_even(height) do @@ -85,7 +91,11 @@ defmodule Membrane.Caps.Video.Raw do {:ok, width * height * 4} end - def frame_size(_, _, _) do + def frame_size(format, _width, _height) when format in @supported_pixel_formats do {:error, :invalid_dims} end + + def frame_size(_format, _width, _height) do + {:error, :invalid_pixel_format} + end end diff --git a/mix.exs b/mix.exs index ec376d1..8104557 100644 --- a/mix.exs +++ b/mix.exs @@ -1,17 +1,18 @@ -defmodule Membrane.Caps.Video.Raw.Mixfile do +defmodule Membrane.RawVideo.Mixfile do use Mix.Project - @version "0.1.0" - @github_url "https://github.com/membraneframework/membrane-caps-video-raw" + @version "0.2.0" + @github_url "https://github.com/membraneframework/membrane_raw_video_format" def project do [ - app: :membrane_caps_video_raw, + app: :membrane_raw_video_format, version: @version, - elixir: "~> 1.7", - description: "Membrane Multimedia Framework (Raw video format definition)", + elixir: "~> 1.12", + description: + "Definition of raw (uncompressed) video format for Membrane Multimedia Framework", package: package(), - name: "Membrane Caps: Video Raw", + name: "Membrane: Raw video format", source_url: @github_url, docs: docs(), deps: deps() @@ -21,7 +22,8 @@ defmodule Membrane.Caps.Video.Raw.Mixfile do defp docs do [ main: "readme", - extras: ["README.md"], + extras: ["README.md", LICENSE: [title: "License"]], + formatters: ["html"], source_ref: "v#{@version}" ] end @@ -29,7 +31,7 @@ defmodule Membrane.Caps.Video.Raw.Mixfile do defp package do [ maintainers: ["Membrane Team"], - licenses: ["Apache 2.0"], + licenses: ["Apache-2.0"], links: %{ "GitHub" => @github_url, "Membrane Framework Homepage" => "https://membraneframework.org" @@ -39,7 +41,9 @@ defmodule Membrane.Caps.Video.Raw.Mixfile do defp deps do [ - {:ex_doc, "~> 0.19", only: :dev, runtime: false} + {:ex_doc, "~> 0.28", only: :dev, runtime: false}, + {:dialyxir, "~> 1.1", only: :dev, runtime: false}, + {:credo, "~> 1.6", only: :dev, runtime: false} ] end end diff --git a/mix.lock b/mix.lock index a757be0..3c94f33 100644 --- a/mix.lock +++ b/mix.lock @@ -1,7 +1,15 @@ %{ - "earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm"}, - "ex_doc": {:hex, :ex_doc, "0.19.1", "519bb9c19526ca51d326c060cb1778d4a9056b190086a8c6c115828eaccea6cf", [:mix], [{:earmark, "~> 1.1", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.7", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"}, - "makeup": {:hex, :makeup, "0.5.5", "9e08dfc45280c5684d771ad58159f718a7b5788596099bdfb0284597d368a882", [:mix], [{:nimble_parsec, "~> 0.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.10.0", "0f09c2ddf352887a956d84f8f7e702111122ca32fbbc84c2f0569b8b65cbf7fa", [:mix], [{:makeup, "~> 0.5.5", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"}, - "nimble_parsec": {:hex, :nimble_parsec, "0.4.0", "ee261bb53214943679422be70f1658fff573c5d0b0a1ecd0f18738944f818efe", [:mix], [], "hexpm"}, + "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, + "credo": {:hex, :credo, "1.6.3", "0a9f8925dbc8f940031b789f4623fc9a0eea99d3eed600fe831e403eb96c6a83", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "1167cde00e6661d740fc54da2ee268e35d3982f027399b64d3e2e83af57a1180"}, + "dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"}, + "earmark": {:hex, :earmark, "1.2.6", "b6da42b3831458d3ecc57314dff3051b080b9b2be88c2e5aa41cd642a5b044ed", [:mix], [], "hexpm", "b42a23e9bd92d65d16db2f75553982e58519054095356a418bb8320bbacb58b1"}, + "earmark_parser": {:hex, :earmark_parser, "1.4.19", "de0d033d5ff9fc396a24eadc2fcf2afa3d120841eb3f1004d138cbf9273210e8", [:mix], [], "hexpm", "527ab6630b5c75c3a3960b75844c314ec305c76d9899bb30f71cb85952a9dc45"}, + "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, + "ex_doc": {:hex, :ex_doc, "0.28.0", "7eaf526dd8c80ae8c04d52ac8801594426ae322b52a6156cd038f30bafa8226f", [:mix], [{:earmark_parser, "~> 1.4.19", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e55cdadf69a5d1f4cfd8477122ebac5e1fadd433a8c1022dafc5025e48db0131"}, + "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, + "jason": {:hex, :jason, "1.3.0", "fa6b82a934feb176263ad2df0dbd91bf633d4a46ebfdffea0c8ae82953714946", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "53fc1f51255390e0ec7e50f9cb41e751c260d065dcba2bf0d08dc51a4002c2ac"}, + "makeup": {:hex, :makeup, "1.1.0", "6b67c8bc2882a6b6a445859952a602afc1a41c2e08379ca057c0f525366fc3ca", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "0a45ed501f4a8897f580eabf99a2e5234ea3e75a4373c8a52824f6e873be57a6"}, + "makeup_elixir": {:hex, :makeup_elixir, "0.15.2", "dc72dfe17eb240552857465cc00cce390960d9a0c055c4ccd38b70629227e97c", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "fd23ae48d09b32eff49d4ced2b43c9f086d402ee4fd4fcb2d7fad97fa8823e75"}, + "makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"}, + "nimble_parsec": {:hex, :nimble_parsec, "1.2.2", "b99ca56bbce410e9d5ee4f9155a212e942e224e259c7ebbf8f2c86ac21d4fa3c", [:mix], [], "hexpm", "98d51bd64d5f6a2a9c6bb7586ee8129e27dfaab1140b5a4753f24dac0ba27d2f"}, } From dae2071617e6ddf11d96e566603dbecdb953ff50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20B=C5=82aszk=C3=B3w?= Date: Mon, 21 Feb 2022 11:18:04 +0100 Subject: [PATCH 2/3] Fix & improve tests --- test/membrane_caps_video_raw_test.exs | 74 --------------- test/membrane_raw_video_test.exs | 128 ++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 74 deletions(-) delete mode 100644 test/membrane_caps_video_raw_test.exs create mode 100644 test/membrane_raw_video_test.exs diff --git a/test/membrane_caps_video_raw_test.exs b/test/membrane_caps_video_raw_test.exs deleted file mode 100644 index 5e6598a..0000000 --- a/test/membrane_caps_video_raw_test.exs +++ /dev/null @@ -1,74 +0,0 @@ -defmodule Membrane.Caps.Membrane.Caps.Video.Raw.Test do - use ExUnit.Case, async: true - @module Membrane.Caps.Video.Raw - - test "frame_size for :I420 format" do - format = :I420 - assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} - end - - test "frame_size for :I422 format" do - format = :I422 - assert @module.frame_size(format, 10, 20) == {:ok, 400} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:ok, 380} - end - - test "frame_size for :I444 format" do - format = :I444 - assert @module.frame_size(format, 10, 20) == {:ok, 600} - assert @module.frame_size(format, 9, 20) == {:ok, 540} - assert @module.frame_size(format, 10, 19) == {:ok, 570} - end - - test "frame_size for :RGB format" do - format = :RGB - assert @module.frame_size(format, 10, 20) == {:ok, 600} - assert @module.frame_size(format, 9, 20) == {:ok, 540} - assert @module.frame_size(format, 10, 19) == {:ok, 570} - end - - test "frame_size for :BGRA format" do - format = :BGRA - assert @module.frame_size(format, 10, 20) == {:ok, 800} - assert @module.frame_size(format, 9, 20) == {:ok, 720} - assert @module.frame_size(format, 10, 19) == {:ok, 760} - end - - test "frame_size for :RGBA format" do - format = :RGBA - assert @module.frame_size(format, 10, 20) == {:ok, 800} - assert @module.frame_size(format, 9, 20) == {:ok, 720} - assert @module.frame_size(format, 10, 19) == {:ok, 760} - end - - test "frame_size for :NV12 format" do - format = :NV12 - assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} - end - - test "frame_size for :NV21 format" do - format = :NV21 - assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} - end - - test "frame_size for :YV12 format" do - format = :YV12 - assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} - end - - test "frame_size for :AYUV format" do - format = :AYUV - assert @module.frame_size(format, 10, 20) == {:ok, 800} - assert @module.frame_size(format, 9, 20) == {:ok, 720} - assert @module.frame_size(format, 10, 19) == {:ok, 760} - end -end diff --git a/test/membrane_raw_video_test.exs b/test/membrane_raw_video_test.exs new file mode 100644 index 0000000..1def7f0 --- /dev/null +++ b/test/membrane_raw_video_test.exs @@ -0,0 +1,128 @@ +defmodule Membrane.RawVideo.Test do + use ExUnit.Case, async: true + @module Membrane.RawVideo + + defp format_struct(format, width, height) do + %@module{ + pixel_format: format, + width: width, + height: height, + framerate: {30, 1}, + aligned: true + } + end + + test "frame_size for :I420 format" do + format = :I420 + assert @module.frame_size(format, 10, 20) == {:ok, 300} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + end + + test "frame_size for :I422 format" do + format = :I422 + assert @module.frame_size(format, 10, 20) == {:ok, 400} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} + assert @module.frame_size(format, 10, 19) == {:ok, 380} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 400} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 380} + end + + test "frame_size for :I444 format" do + format = :I444 + assert @module.frame_size(format, 10, 20) == {:ok, 600} + assert @module.frame_size(format, 9, 20) == {:ok, 540} + assert @module.frame_size(format, 10, 19) == {:ok, 570} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 600} + assert @module.frame_size(format_struct(format, 9, 20)) == {:ok, 540} + assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 570} + end + + test "frame_size for :RGB format" do + format = :RGB + assert @module.frame_size(format, 10, 20) == {:ok, 600} + assert @module.frame_size(format, 9, 20) == {:ok, 540} + assert @module.frame_size(format, 10, 19) == {:ok, 570} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 600} + assert @module.frame_size(format_struct(format, 9, 20)) == {:ok, 540} + assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 570} + end + + test "frame_size for :BGRA format" do + format = :BGRA + assert @module.frame_size(format, 10, 20) == {:ok, 800} + assert @module.frame_size(format, 9, 20) == {:ok, 720} + assert @module.frame_size(format, 10, 19) == {:ok, 760} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 800} + assert @module.frame_size(format_struct(format, 9, 20)) == {:ok, 720} + assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 760} + end + + test "frame_size for :RGBA format" do + format = :RGBA + assert @module.frame_size(format, 10, 20) == {:ok, 800} + assert @module.frame_size(format, 9, 20) == {:ok, 720} + assert @module.frame_size(format, 10, 19) == {:ok, 760} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 800} + assert @module.frame_size(format_struct(format, 9, 20)) == {:ok, 720} + assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 760} + end + + test "frame_size for :NV12 format" do + format = :NV12 + assert @module.frame_size(format, 10, 20) == {:ok, 300} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + end + + test "frame_size for :NV21 format" do + format = :NV21 + assert @module.frame_size(format, 10, 20) == {:ok, 300} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + end + + test "frame_size for :YV12 format" do + format = :YV12 + assert @module.frame_size(format, 10, 20) == {:ok, 300} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + end + + test "frame_size for :AYUV format" do + format = :AYUV + assert @module.frame_size(format, 10, 20) == {:ok, 800} + assert @module.frame_size(format, 9, 20) == {:ok, 720} + assert @module.frame_size(format, 10, 19) == {:ok, 760} + + assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 800} + assert @module.frame_size(format_struct(format, 9, 20)) == {:ok, 720} + assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 760} + end + + test "frame_size error on unknown pixel format" do + assert @module.frame_size(:yuv_240p, 10, 20) == {:error, :invalid_pixel_format} + end +end From b9a4f605a750cf27b6ff835e9cf8a294726ee953 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20B=C5=82aszk=C3=B3w?= Date: Mon, 21 Feb 2022 23:01:15 +0100 Subject: [PATCH 3/3] dims -> dimensions --- lib/membrane_raw_video.ex | 6 +++--- test/membrane_raw_video_test.exs | 36 ++++++++++++++++---------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/membrane_raw_video.ex b/lib/membrane_raw_video.ex index a7fb768..b92b5f7 100644 --- a/lib/membrane_raw_video.ex +++ b/lib/membrane_raw_video.ex @@ -51,7 +51,7 @@ defmodule Membrane.RawVideo do in bytes for the given caps. """ @spec frame_size(t()) :: {:ok, pos_integer()} | {:error, reason} - when reason: :invalid_dims | :invalid_pixel_format + when reason: :invalid_dimensions | :invalid_pixel_format def frame_size(%__MODULE__{pixel_format: format, width: width, height: height}) do frame_size(format, width, height) end @@ -64,7 +64,7 @@ defmodule Membrane.RawVideo do """ @spec frame_size(pixel_format_t(), width_t(), height_t()) :: {:ok, pos_integer()} | {:error, reason} - when reason: :invalid_dims | :invalid_pixel_format + when reason: :invalid_dimensions | :invalid_pixel_format def frame_size(format, width, height) when format in [:I420, :YV12, :NV12, :NV21] and Integer.is_even(width) and Integer.is_even(height) do @@ -92,7 +92,7 @@ defmodule Membrane.RawVideo do end def frame_size(format, _width, _height) when format in @supported_pixel_formats do - {:error, :invalid_dims} + {:error, :invalid_dimensions} end def frame_size(_format, _width, _height) do diff --git a/test/membrane_raw_video_test.exs b/test/membrane_raw_video_test.exs index 1def7f0..9043bb6 100644 --- a/test/membrane_raw_video_test.exs +++ b/test/membrane_raw_video_test.exs @@ -15,22 +15,22 @@ defmodule Membrane.RawVideo.Test do test "frame_size for :I420 format" do format = :I420 assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dimensions} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dimensions} assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} - assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} - assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dimensions} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dimensions} end test "frame_size for :I422 format" do format = :I422 assert @module.frame_size(format, 10, 20) == {:ok, 400} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dimensions} assert @module.frame_size(format, 10, 19) == {:ok, 380} assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 400} - assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dimensions} assert @module.frame_size(format_struct(format, 10, 19)) == {:ok, 380} end @@ -81,34 +81,34 @@ defmodule Membrane.RawVideo.Test do test "frame_size for :NV12 format" do format = :NV12 assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dimensions} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dimensions} assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} - assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} - assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dimensions} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dimensions} end test "frame_size for :NV21 format" do format = :NV21 assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dimensions} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dimensions} assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} - assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} - assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dimensions} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dimensions} end test "frame_size for :YV12 format" do format = :YV12 assert @module.frame_size(format, 10, 20) == {:ok, 300} - assert @module.frame_size(format, 9, 20) == {:error, :invalid_dims} - assert @module.frame_size(format, 10, 19) == {:error, :invalid_dims} + assert @module.frame_size(format, 9, 20) == {:error, :invalid_dimensions} + assert @module.frame_size(format, 10, 19) == {:error, :invalid_dimensions} assert @module.frame_size(format_struct(format, 10, 20)) == {:ok, 300} - assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dims} - assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dims} + assert @module.frame_size(format_struct(format, 9, 20)) == {:error, :invalid_dimensions} + assert @module.frame_size(format_struct(format, 10, 19)) == {:error, :invalid_dimensions} end test "frame_size for :AYUV format" do