Skip to content

Commit a9578e6

Browse files
authored
Merge pull request ocaml-community#16 from LemonBoy/pixfmt
Add Pixel_format.{of_string,to_string,planes}
2 parents 8c0f717 + ea9fc2f commit a9578e6

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

Diff for: src/avutil.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ let () =
2626
module Pixel_format = struct
2727
type t = Pixel_format.t
2828

29-
external bits : t -> int = "ocaml_avutil_bits_per_pixel"
29+
external bits : t -> int = "ocaml_avutil_pixelformat_bits_per_pixel"
30+
external planes : t -> int = "ocaml_avutil_pixelformat_planes"
31+
external to_string : t -> string = "ocaml_avutil_pixelformat_to_string"
32+
external of_string : string -> t = "ocaml_avutil_pixelformat_of_string"
3033

3134
let bits (*?(padding=true)*) p =
3235
let n = bits p in

Diff for: src/avutil.mli

+9
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ module Pixel_format : sig
8383

8484
(** Return the number of bits of the pixel format. *)
8585
val bits : (*?padding:bool ->*) t -> int
86+
87+
(** Return the number of planes of the pixel format. *)
88+
val planes : t -> int
89+
90+
(** [Pixel_format.to_string f] Return a string representation of the pixel format [f]. *)
91+
val to_string : t -> string
92+
93+
(** [Pixel_format.of_string s] Convert the string [s] into a [Pixel_format.t]. @raise Failure if [s] is not a valid format. *)
94+
val of_string : string -> t
8695
end
8796

8897
module Video : sig

Diff for: src/avutil_stubs.c

+31-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
#include <caml/bigarray.h>
88
#include <caml/threads.h>
99

10+
#include <assert.h>
11+
1012
#include <libavutil/pixfmt.h>
1113
#include <libavutil/pixdesc.h>
12-
#include "libavutil/avstring.h"
14+
#include <libavutil/avstring.h>
1315

1416
#include "avutil_stubs.h"
1517
#include "pixel_format_stubs.h"
@@ -19,17 +21,41 @@
1921
char ocaml_av_error_msg[ERROR_MSG_SIZE + 1];
2022
char ocaml_av_exn_msg[ERROR_MSG_SIZE + 1];
2123

22-
CAMLprim value ocaml_avutil_bits_per_pixel(value pixel)
24+
CAMLprim value ocaml_avutil_pixelformat_bits_per_pixel(value pixel)
25+
{
26+
CAMLparam1(pixel);
27+
enum AVPixelFormat p = PixelFormat_val(pixel);
28+
29+
CAMLreturn(Val_int(av_get_bits_per_pixel(av_pix_fmt_desc_get(p))));
30+
}
31+
32+
CAMLprim value ocaml_avutil_pixelformat_planes(value pixel)
2333
{
2434
CAMLparam1(pixel);
2535
enum AVPixelFormat p = PixelFormat_val(pixel);
26-
int ans;
2736

28-
ans = av_get_bits_per_pixel(av_pix_fmt_desc_get(p));
37+
CAMLreturn(Val_int(av_pix_fmt_count_planes(p)));
38+
}
39+
40+
CAMLprim value ocaml_avutil_pixelformat_to_string(value pixel)
41+
{
42+
CAMLparam1(pixel);
43+
enum AVPixelFormat p = PixelFormat_val(pixel);
2944

30-
CAMLreturn(Val_int(ans));
45+
CAMLreturn(caml_copy_string(av_get_pix_fmt_name(p)));
3146
}
3247

48+
CAMLprim value ocaml_avutil_pixelformat_of_string(value name)
49+
{
50+
CAMLparam1(name);
51+
enum AVPixelFormat p;
52+
53+
p = av_get_pix_fmt(String_val(name));
54+
if (p != AV_PIX_FMT_NONE)
55+
CAMLreturn(Val_PixelFormat(p));
56+
57+
Raise(EXN_FAILURE, "Invalid format name");
58+
}
3359

3460
/**** Time format ****/
3561
int64_t second_fractions_of_time_format(value time_format)

0 commit comments

Comments
 (0)