|
| 1 | +# TurboJPEG |
| 2 | + |
| 3 | +Fast JPEG encoding from raw YUV data using [libjpeg-turbo](https://libjpeg-turbo.org/) |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +This library requires libjpeg-turbo to be installed |
| 8 | + |
| 9 | +### Arch linux |
| 10 | + |
| 11 | + sudo pacman -S libjpeg-turbo |
| 12 | + |
| 13 | +### Ubuntu/Debian |
| 14 | + |
| 15 | + sudo apt-get install libjpeg-turbo |
| 16 | + |
| 17 | +### OSX |
| 18 | + |
| 19 | + brew install libjpeg-turbo |
| 20 | + |
| 21 | +If [available in Hex](https://hex.pm/turbojpeg/), the package can be installed |
| 22 | +by adding `turbojpeg` to your list of dependencies in `mix.exs`: |
| 23 | + |
| 24 | +```elixir |
| 25 | +def deps do |
| 26 | + [ |
| 27 | + {:shmex, "~> 0.2.0"}, |
| 28 | + {:turbojpeg, "~> 0.1.0"} |
| 29 | + ] |
| 30 | +end |
| 31 | +``` |
| 32 | + |
| 33 | +## Basic Usage |
| 34 | + |
| 35 | +```elixir |
| 36 | +iex(1)> {:ok, native} = Turbojpeg.Native.create(1920, 1080, 90, :I420) |
| 37 | +{:ok, #Reference<0.938325095.2990669826.234059>} |
| 38 | +iex(2)> frame = File.read!("fixture/i420.yuv") |
| 39 | +<<0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 40 | + 0, 0, 0, 2, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...>> |
| 41 | +iex(3)> Turbojpeg.Native.to_jpeg(Shmex.new(frame), native) |
| 42 | +{:ok, |
| 43 | + %Shmex{ |
| 44 | + capacity: 203783, |
| 45 | + guard: #Reference<0.938325095.2990669827.232440>, |
| 46 | + name: "/shmex-00000005607042890133#000", |
| 47 | + size: 203783 |
| 48 | + }} |
| 49 | +iex(4)> Shmex.to_binary(jpeg) |
| 50 | +<<255, 216, 255, 224, 0, 16, 74, 70, 73, 70, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 255, |
| 51 | + 219, 0, 67, 0, 3, 2, 2, 3, 2, 2, 3, 3, 3, 3, 4, 3, 3, 4, 5, 8, 5, 5, 4, 4, 5, |
| 52 | + 10, 7, 7, 6, ...>> |
| 53 | +iex(5)> File.write!("test.jpg", Shmex.to_binary(jpeg)) |
| 54 | +:ok |
| 55 | +``` |
| 56 | + |
| 57 | +## Membrane Sink Usage |
| 58 | + |
| 59 | +Pleas See [the membrane guide](https://membraneframework.org/guide/v0.5/pipeline.html#content) |
| 60 | +before using this. |
| 61 | + |
| 62 | +```elixir |
| 63 | +defmodule Your.Module.Pipeline do |
| 64 | + use Membrane.Pipeline |
| 65 | + |
| 66 | + @impl true |
| 67 | + def handle_init(location) do |
| 68 | + children = %{ |
| 69 | + source: %SomeMembraneSourceModule{location: location}, |
| 70 | + decoder: Membrane.Element.FFmpeg.H264.Decoder, |
| 71 | + jpeg_converter: %Turbojpeg.Sink{filename: "/tmp/frame.jpeg", quality: 100}, |
| 72 | + } |
| 73 | + |
| 74 | + links = [ |
| 75 | + link(:source) |
| 76 | + |> to(:decoder) |
| 77 | + |> to(:jpeg_converter) |
| 78 | + ] |
| 79 | + |
| 80 | + spec = %ParentSpec{ |
| 81 | + children: children, |
| 82 | + links: links |
| 83 | + } |
| 84 | + |
| 85 | + {{:ok, spec: spec}, %{}} |
| 86 | + end |
| 87 | + |
| 88 | +end |
| 89 | +``` |
| 90 | + |
| 91 | +# Copyright and License |
| 92 | + |
| 93 | +Copyright 2020, Binary Noggin |
0 commit comments