Skip to content

Commit

Permalink
Use packed bigarray in ffmpeg swscale calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Oct 21, 2024
1 parent 3d2cb3b commit ac2fa6c
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 16 deletions.
4 changes: 2 additions & 2 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
(dssi (< 0.1.3))
(faad (< 0.5.0))
(fdkaac (< 0.3.1))
(ffmpeg (< 1.2.0))
(ffmpeg-avutil (< 1.2.0))
(ffmpeg (< 1.2.1))
(ffmpeg-avutil (< 1.2.1))
(flac (< 0.3.0))
(frei0r (< 0.1.0))
(inotify (< 1.0))
Expand Down
4 changes: 2 additions & 2 deletions liquidsoap-core.opam
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ conflicts: [
"dssi" {< "0.1.3"}
"faad" {< "0.5.0"}
"fdkaac" {< "0.3.1"}
"ffmpeg" {< "1.2.0"}
"ffmpeg-avutil" {< "1.2.0"}
"ffmpeg" {< "1.2.1"}
"ffmpeg-avutil" {< "1.2.1"}
"flac" {< "0.3.0"}
"frei0r" {< "0.1.0"}
"inotify" {< "1.0"}
Expand Down
2 changes: 1 addition & 1 deletion src/core/builtins/builtins_ffmpeg_decoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let ffmpeg_raw_decode =
module InternalResampler =
Swresample.Make (Swresample.Frame) (Swresample.PlanarFloatArray)

module InternalScaler = Swscale.Make (Swscale.Frame) (Swscale.BigArray)
module InternalScaler = Swscale.Make (Swscale.Frame) (Swscale.PackedBigArray)

let log = Log.make ["ffmpeg"; "internal"; "decoder"]

Expand Down
2 changes: 1 addition & 1 deletion src/core/builtins/builtins_ffmpeg_encoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ let ffmpeg_raw_encode =
module InternalResampler =
Swresample.Make (Swresample.PlanarFloatArray) (Swresample.Frame)

module InternalScaler = Swscale.Make (Swscale.BigArray) (Swscale.Frame)
module InternalScaler = Swscale.Make (Swscale.PackedBigArray) (Swscale.Frame)

type source_idx = { source : Source.source; idx : int64 }

Expand Down
2 changes: 1 addition & 1 deletion src/core/decoder/ffmpeg_internal_decoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module Converter_pcm_f32 = struct
include ConverterInput (Swresample.FltPlanarBigArray)
end

module Scaler = Swscale.Make (Swscale.Frame) (Swscale.BigArray)
module Scaler = Swscale.Make (Swscale.Frame) (Swscale.PackedBigArray)

let mk_audio_decoder ~channels ~stream ~field ~pcm_kind codec =
let converter =
Expand Down
2 changes: 1 addition & 1 deletion src/core/decoder/image/ffmpeg_image_decoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*****************************************************************************)

module Scaler = Swscale.Make (Swscale.Frame) (Swscale.BigArray)
module Scaler = Swscale.Make (Swscale.Frame) (Swscale.PackedBigArray)

let log = Log.make ["decoder"; "ffmpeg"; "image"]

Expand Down
2 changes: 1 addition & 1 deletion src/core/encoder/encoders/ffmpeg_internal_encoder.ml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ module InternalResampler_pcm_f32 = struct
end

module RawResampler = Swresample.Make (Swresample.Frame) (Swresample.Frame)
module InternalScaler = Swscale.Make (Swscale.BigArray) (Swscale.Frame)
module InternalScaler = Swscale.Make (Swscale.PackedBigArray) (Swscale.Frame)
module RawScaler = Swscale.Make (Swscale.Frame) (Swscale.Frame)

let log = Log.make ["ffmpeg"; "encoder"; "internal"]
Expand Down
43 changes: 38 additions & 5 deletions src/core/tools/ffmpeg_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,52 @@ let liq_frame_time_base () =
let liq_frame_pixel_format () = `Yuv420p

let pack_image f =
let data = Image.YUV420.packed_data f in
let y, u, v = Image.YUV420.data f in
let sy = Image.YUV420.y_stride f in
let s = Image.YUV420.uv_stride f in
[| (y, sy); (u, s); (v, s) |]
{
Swscale.PackedBigArray.data;
planes =
[|
{ plane_size = Image.Data.length y; stride = sy };
{ plane_size = Image.Data.length u; stride = s };
{ plane_size = Image.Data.length v; stride = s };
|];
}

let unpack_image ~width ~height = function
| [| (y, sy); (u, su); (v, sv) |] ->
| {
Swscale.PackedBigArray.data;
planes =
[|
{ plane_size = y_len; stride = sy };
{ plane_size = u_len; stride = su };
{ plane_size = v_len; stride = sv };
|];
} ->
assert (su = sv);
Image.YUV420.make width height y sy u v su
| [| (y, sy); (u, su); (v, sv); (alpha, sa) |] ->
let y = Image.Data.sub data 0 y_len in
let u = Image.Data.sub data y_len u_len in
let v = Image.Data.sub data (y_len + u_len) v_len in
Image.YUV420.make ~packed_data:data width height y sy u v su
| {
Swscale.PackedBigArray.data;
planes =
[|
{ plane_size = y_len; stride = sy };
{ plane_size = u_len; stride = su };
{ plane_size = v_len; stride = sv };
{ plane_size = alpha_len; stride = sa };
|];
} ->
assert (su = sv);
assert (sa = sy);
Image.YUV420.make width height ~alpha y sy u v su
let y = Image.Data.sub data 0 y_len in
let u = Image.Data.sub data y_len u_len in
let v = Image.Data.sub data (y_len + u_len) v_len in
let alpha = Image.Data.sub data (y_len + u_len + v_len) alpha_len in
Image.YUV420.make ~packed_data:data width height ~alpha y sy u v su
| _ -> assert false

let convert_time_base ~src ~dst pts =
Expand Down
4 changes: 2 additions & 2 deletions src/core/tools/ffmpeg_utils.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ val liq_video_sample_time_base : unit -> Avutil.rational
val liq_frame_time_base : unit -> Avutil.rational
val liq_frame_pixel_format : unit -> Avutil.Pixel_format.t
val pixel_format : 'a Avcodec.Video.t -> string option -> Avutil.Pixel_format.t
val pack_image : Image.YUV420.t -> (Image.Data.t * int) array
val pack_image : Image.YUV420.t -> Swscale.PackedBigArray.t

val unpack_image :
width:int -> height:int -> (Image.Data.t * int) array -> Image.YUV420.t
width:int -> height:int -> Swscale.PackedBigArray.t -> Image.YUV420.t

val convert_time_base :
src:Avutil.rational -> dst:Avutil.rational -> int64 -> int64
Expand Down

0 comments on commit ac2fa6c

Please sign in to comment.