2 #include <caml/alloc.h>
3 #include <caml/memory.h>
5 #include <libswscale/swscale.h>
11 struct SwsContext
*sws
;
17 static State glob_state
;
19 static void sws_reset (State
*s
)
21 #ifdef ARCH_BIG_ENDIAN
22 int fmt
= PIX_FMT_RGB565BE
;
24 int fmt
= PIX_FMT_RGB565LE
;
28 sws_freeContext (s
->sws
);
32 s
->sws
= sws_getContext (s
->w
, s
->h
, fmt
,
33 s
->out_w
, s
->out_h
, PIX_FMT_YUV420P
,
37 caml_failwith ("sws_getContext failed");
40 static void scale (State
*s
, uint8_t *in
, uint8_t *out
)
46 plane
[1] = plane
[0] + s
->offset
[1];
47 plane
[2] = plane
[1] + s
->offset
[2];
49 ret
= sws_scale (s
->sws
, &in
, &s
->pitch
,
50 0, s
->h
, plane
, s
->linesize
);
52 caml_failwith ("sws_scale failed");
55 CAMLprim value
ml_scale_reset (value params_v
)
57 CAMLparam1 (params_v
);
58 State
*s
= &glob_state
;
60 s
->w
= Int_val (Field (params_v
, 0));
61 s
->h
= Int_val (Field (params_v
, 1));
62 s
->out_w
= Int_val (Field (params_v
, 2));
63 s
->out_h
= Int_val (Field (params_v
, 3));
64 s
->pitch
= Int_val (Field (params_v
, 4));
66 s
->outsize
= s
->out_w
* s
->out_h
* 3 / 2;
68 s
->linesize
[0] = s
->out_w
;
69 s
->linesize
[1] = s
->out_w
/ 2;
70 s
->linesize
[2] = s
->out_w
/ 2;
73 s
->offset
[1] = s
->out_w
* s
->out_h
;
74 s
->offset
[2] = s
->offset
[1] / 4;
78 CAMLreturn (Val_int (s
->outsize
));
81 CAMLprim value
ml_scale (value in_v
, value out_v
)
83 CAMLparam2 (in_v
, out_v
);
84 State
*s
= &glob_state
;
86 scale (s
, String_val (in_v
), String_val (out_v
));
87 CAMLreturn (Val_unit
);