Skip to content

Commit 8c0f717

Browse files
committed
Fix wrong plane count and wrong plane copy
1 parent bbbe95c commit 8c0f717

File tree

5 files changed

+11
-92
lines changed

5 files changed

+11
-92
lines changed

examples/encoding/encoding.ml

+2-5
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ let () =
7272
Av.set_metadata ovs ["Media", "Video"];
7373

7474
let frame = Video.create_frame width height pixel_format in
75-
let planes = Video.copy_frame_to_planes frame in
76-
7775
let video_on_off = [|fill_image_on; fill_image_off|] in
7876
(*
7977
let oss = Av.new_subtitle_stream ~codec_name:Sys.argv.(4) dst in
@@ -92,9 +90,8 @@ let () =
9290

9391
for i = 0 to frame_rate * duration - 1 do
9492
let b = (i mod frame_rate) / 13 in
95-
video_on_off.(b) width height i planes;
96-
Video.copy_planes_to_frame frame planes;
97-
Av.write ovs frame;
93+
Video.frame_visit ~make_writable:true (video_on_off.(b) width height i) frame
94+
|> Av.write ovs;
9895
done;
9996

10097
Av.close dst;

src/avutil.ml

-5
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ module Video = struct
6565

6666
external frame_get_linesize : video frame -> int -> int = "ocaml_avutil_video_frame_get_linesize"
6767

68-
external copy_frame_to_planes : video frame -> planes = "ocaml_avutil_video_copy_frame_to_bigarray_planes"
69-
70-
external copy_planes_to_frame : video frame -> planes -> unit = "ocaml_avutil_video_copy_bigarray_planes_to_frame"
71-
72-
7368
external get_frame_planes : video frame -> bool -> planes = "ocaml_avutil_video_get_frame_bigarray_planes"
7469

7570
let frame_visit ~make_writable visit frame = visit(get_frame_planes frame make_writable); frame

src/avutil.mli

-6
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ module Video : sig
9595
val frame_get_linesize : video frame -> int -> int
9696
(** [Avutil.Video.frame_get_linesize vf n] return the line size of the [n] plane of the [vf] video frame. @raise Failure if [n] is out of boundaries. *)
9797

98-
val copy_frame_to_planes : video frame -> planes
99-
(** [Avutil.Video.copy_frame_to_planes vf] copy the video frame [vf] data to fresh arrays. *)
100-
101-
val copy_planes_to_frame : video frame -> planes -> unit
102-
(** [Avutil.Video.copy_planes_to_frame vf planes] copy the [planes] to the video frame [vf]. @raise Failure if the make frame writable operation failed or if the planes lines sizes and the frame lines sizes are different. *)
103-
10498
val frame_visit : make_writable:bool -> (planes -> unit) -> video frame -> video frame
10599
(** [Avutil.Video.frame_visit ~make_writable:wrt f vf] call the [f] function with planes wrapping the [vf] video frame data. The make_writable:[wrt] parameter must be set to true if the [f] function writes in the planes. Access to the frame through the planes is safe as long as it occurs in the [f] function and the frame is not sent to an encoder. The same frame is returned for convenience. @raise Failure if the make frame writable operation failed. *)
106100
end

src/avutil_stubs.c

+6-73
Original file line numberDiff line numberDiff line change
@@ -172,71 +172,6 @@ CAMLprim value ocaml_avutil_video_frame_get_linesize(value _frame, value _line)
172172
#endif
173173
}
174174

175-
CAMLprim value ocaml_avutil_video_copy_frame_to_bigarray_planes(value _frame)
176-
{
177-
CAMLparam1(_frame);
178-
CAMLlocal3(ans, data, plane);
179-
#ifndef HAS_FRAME
180-
caml_failwith("Not implemented.");
181-
#else
182-
AVFrame *frame = Frame_val(_frame);
183-
int i, nb_planes;
184-
185-
for(nb_planes = 0; frame->buf[nb_planes]; nb_planes++);
186-
187-
ans = caml_alloc_tuple(nb_planes);
188-
189-
for(i = 0; i < nb_planes; i++) {
190-
AVBufferRef* buffer = frame->buf[i];
191-
intnat out_size = buffer->size;
192-
193-
data = caml_ba_alloc(CAML_BA_C_LAYOUT | CAML_BA_UINT8, 1, NULL, &out_size);
194-
195-
memcpy(Caml_ba_data_val(data), buffer->data, buffer->size);
196-
197-
plane = caml_alloc_tuple(2);
198-
199-
Store_field(plane, 0, data);
200-
Store_field(plane, 1, Val_int(frame->linesize[i]));
201-
Store_field(ans, i, plane);
202-
}
203-
#endif
204-
CAMLreturn(ans);
205-
}
206-
207-
208-
CAMLprim value ocaml_avutil_video_copy_bigarray_planes_to_frame(value _frame, value _planes)
209-
{
210-
CAMLparam2(_planes, _frame);
211-
CAMLlocal2(data, plane);
212-
#ifndef HAS_FRAME
213-
caml_failwith("Not implemented.");
214-
#else
215-
int i, nb_planes = Wosize_val(_planes);
216-
AVFrame *frame = Frame_val(_frame);
217-
218-
int ret = av_frame_make_writable(frame);
219-
if (ret < 0) Raise(EXN_FAILURE, "Failed to make frame writable : %s", av_err2str(ret));
220-
221-
for(i = 0; i < nb_planes && frame->buf[i]; i++) {
222-
AVBufferRef* buffer = frame->buf[i];
223-
plane = Field(_planes, i);
224-
data = Field(plane, 0);
225-
int src_linesize = Int_val(Field(plane, 1));
226-
227-
if(src_linesize != frame->linesize[i]) Raise(EXN_FAILURE, "Failed to copy planes to frame : incompatible linesize");
228-
229-
size_t size = buffer->size;
230-
231-
if(Caml_ba_array_val(data)->dim[0] < size) size = Caml_ba_array_val(data)->dim[0];
232-
233-
memcpy(buffer->data, Caml_ba_data_val(data), size);
234-
}
235-
#endif
236-
CAMLreturn(Val_unit);
237-
}
238-
239-
240175
CAMLprim value ocaml_avutil_video_get_frame_bigarray_planes(value _frame, value _make_writable)
241176
{
242177
CAMLparam1(_frame);
@@ -245,25 +180,23 @@ CAMLprim value ocaml_avutil_video_get_frame_bigarray_planes(value _frame, value
245180
caml_failwith("Not implemented.");
246181
#else
247182
AVFrame *frame = Frame_val(_frame);
248-
int i, nb_planes;
183+
int i;
249184

250185
if(Bool_val(_make_writable)) {
251186
int ret = av_frame_make_writable(frame);
252187
if (ret < 0) Raise(EXN_FAILURE, "Failed to make frame writable : %s", av_err2str(ret));
253188
}
254-
255-
for(nb_planes = 0; frame->buf[nb_planes]; nb_planes++);
256189

190+
int nb_planes = av_pix_fmt_count_planes((enum AVPixelFormat)frame->format);
191+
if(nb_planes < 0) Raise(EXN_FAILURE, "Failed to get frame planes count : %s", av_err2str(nb_planes));
192+
257193
ans = caml_alloc_tuple(nb_planes);
258194

259195
for(i = 0; i < nb_planes; i++) {
260-
AVBufferRef* buffer = av_frame_get_plane_buffer(frame, i);
261-
if( ! buffer) Raise(EXN_FAILURE, "Failed to get frame plane buffer");
262-
263-
intnat out_size = buffer->size;
196+
intnat out_size = frame->linesize[i] * frame->height;
264197
plane = caml_alloc_tuple(2);
265198

266-
Store_field(plane, 0, caml_ba_alloc(CAML_BA_C_LAYOUT | CAML_BA_UINT8, 1, buffer->data, &out_size));
199+
Store_field(plane, 0, caml_ba_alloc(CAML_BA_C_LAYOUT | CAML_BA_UINT8, 1, frame->data[i], &out_size));
267200
Store_field(plane, 1, Val_int(frame->linesize[i]));
268201
Store_field(ans, i, plane);
269202
}

src/avutil_stubs.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
#define Log(...) snprintf(ocaml_av_error_msg, ERROR_MSG_SIZE, __VA_ARGS__)
2626

27-
#define Fail(...) { \
28-
Log(__VA_ARGS__); \
29-
return NULL; \
27+
#define Fail(...) { \
28+
snprintf(ocaml_av_error_msg, ERROR_MSG_SIZE, __VA_ARGS__); \
29+
return NULL; \
3030
}
3131

3232
#define Raise(exn, ...) { \

0 commit comments

Comments
 (0)