Skip to content

Commit

Permalink
get pixel formats and sample formats
Browse files Browse the repository at this point in the history
  • Loading branch information
gBillal committed Feb 3, 2025
1 parent 029ee0d commit 08b7d1a
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 1 deletion.
39 changes: 38 additions & 1 deletion c_src/xav/xav_decoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,41 @@ ERL_NIF_TERM flush(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
return xav_nif_ok(env, enif_make_list_from_array(env, frame_terms, frames_count));
}

ERL_NIF_TERM pixel_formats(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM result = enif_make_list(env, 0);

const AVPixFmtDescriptor *desc = NULL;

while ((desc = av_pix_fmt_desc_next(desc))) {
ERL_NIF_TERM name = enif_make_atom(env, desc->name);
ERL_NIF_TERM nb_components = enif_make_int(env, desc->nb_components);
ERL_NIF_TERM is_hwaccel =
enif_make_atom(env, desc->flags & AV_PIX_FMT_FLAG_HWACCEL ? "true" : "false");

result =
enif_make_list_cell(env, enif_make_tuple3(env, name, nb_components, is_hwaccel), result);
}

return result;
}

ERL_NIF_TERM sample_formats(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM result = enif_make_list(env, 0);

for (int fmt = 0; fmt < AV_SAMPLE_FMT_NB; fmt++) {
enum AVSampleFormat sample_format = (enum AVSampleFormat)fmt;
const char *name = av_get_sample_fmt_name(sample_format);
int nb_bytes = av_get_bytes_per_sample(sample_format);

ERL_NIF_TERM desc =
enif_make_tuple2(env, enif_make_atom(env, name), enif_make_int(env, nb_bytes));

result = enif_make_list_cell(env, desc, result);
}

return result;
}

static int init_audio_converter(struct XavDecoder *xav_decoder) {
xav_decoder->ac = audio_converter_alloc();

Expand Down Expand Up @@ -345,7 +380,9 @@ void free_xav_decoder(ErlNifEnv *env, void *obj) {

static ErlNifFunc xav_funcs[] = {{"new", 6, new},
{"decode", 4, decode, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"flush", 1, flush, ERL_NIF_DIRTY_JOB_CPU_BOUND}};
{"flush", 1, flush, ERL_NIF_DIRTY_JOB_CPU_BOUND},
{"pixel_formats", 0, pixel_formats},
{"sample_formats", 0, sample_formats}};

static int load(ErlNifEnv *env, void **priv, ERL_NIF_TERM load_info) {
xav_decoder_resource_type =
Expand Down
24 changes: 24 additions & 0 deletions lib/xav.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Xav do
@moduledoc File.read!("README.md")

@doc """
Get all available pixel formats.
The result is a list of 3-element tuples `{name, nb_components, hw_accelerated_format?}`:
* `name` - The name of the pixel format.
* `nb_components` - The number of the components in the pixel format.
* `hw_accelerated_format?` - Whether the pixel format is a hardware accelerated format.
"""
@spec pixel_formats() :: [{atom(), integer(), boolean()}]
def pixel_formats(), do: Xav.Decoder.NIF.pixel_formats() |> Enum.reverse()

@doc """
Get all available audio sample formats.
The result is a list of 2-element tuples `{name, nb_bytes}`:
* `name` - The name of the sample format.
* `nb_bytes` - The number of bytes per sample.
"""
@spec sample_formats() :: [{atom(), integer()}]
def sample_formats(), do: Xav.Decoder.NIF.sample_formats() |> Enum.reverse()
end
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/decoder_nif.ex → lib/xav/decoder_nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ defmodule Xav.Decoder.NIF do
def decode(_decoder, _data, _pts, _dts), do: :erlang.nif_error(:undef)

def flush(_decoder), do: :erlang.nif_error(:undef)

def pixel_formats(), do: :erlang.nif_error(:undef)

def sample_formats(), do: :erlang.nif_error(:undef)
end
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 08b7d1a

Please sign in to comment.