@@ -7,23 +7,17 @@ namespace nb = nanobind;
77using namespace viteo ;
88
99// / Create MLX array from raw BGRA buffer
10- nb::object create_mlx_array (uint8_t * data, int height, int width) {
11- if (!data) return nb::none ();
10+ mlx::core::array create_mlx_array (uint8_t * data, int height, int width) {
11+ auto arr = mlx::core::array (
12+ data,
13+ mlx::core::Shape{ (int32_t )height, (int32_t )width, int32_t (4 ) },
14+ mlx::core::uint8
15+ );
1216
13- // Import MLX
14- nb::object mlx = nb::module_::import_ (" mlx.core" );
15- nb::object mx_array = mlx.attr (" array" );
16- nb::object mx_uint8 = mlx.attr (" uint8" );
17+ // Eval the array
18+ mlx::core::eval ({arr});
1719
18- // Create memory view
19- size_t size = height * width * 4 ;
20- nb::object memview = nb::steal (PyMemoryView_FromMemory (
21- reinterpret_cast <char *>(data), size, PyBUF_READ
22- ));
23-
24- // Create MLX array and reshape
25- nb::object arr = mx_array (memview, mx_uint8);
26- return arr.attr (" reshape" )(nb::make_tuple (height, width, 4 ));
20+ return arr;
2721}
2822
2923NB_MODULE (_viteo, m) {
@@ -34,13 +28,12 @@ NB_MODULE(_viteo, m) {
3428 .def (" open" , &FrameExtractor::open, nb::arg (" path" ),
3529 " Open video file for extraction" )
3630 .def (" next_frame" ,
37- [](FrameExtractor& self) -> nb::object {
31+ [](FrameExtractor& self) -> mlx::core::array {
3832 uint8_t * frame_data;
3933 {
4034 nb::gil_scoped_release release;
4135 frame_data = self.next_frame ();
4236 }
43- if (!frame_data) return nb::none ();
4437 return create_mlx_array (frame_data, self.height (), self.width ());
4538 },
4639 " Get next frame as MLX array (None when done)" )
@@ -52,7 +45,7 @@ NB_MODULE(_viteo, m) {
5245 .def_prop_ro (" total_frames" , &FrameExtractor::total_frames, " Total frames" )
5346 .def (" __iter__" , [](nb::object self) { return self; })
5447 .def (" __next__" ,
55- [](FrameExtractor& self) -> nb::object {
48+ [](FrameExtractor& self) -> mlx::core::array {
5649 uint8_t * frame_data;
5750 {
5851 nb::gil_scoped_release release;
0 commit comments