1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (C) 2018 Víctor Jáquez <vjaquez@igalia.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use glib_sys;
use gst_gl_sys;
use gst_sys;
use gst_video_sys;

use glib::translate::{from_glib, ToGlibPtr};
use gst;
use gst_video::video_frame::Readable;
use gst_video::*;

use byteorder::{NativeEndian, ReadBytesExt};
use std::mem;

pub trait VideoFrameGLExt {
    fn from_buffer_readable_gl(
        buffer: gst::Buffer,
        info: &VideoInfo,
    ) -> Result<VideoFrame<Readable>, gst::Buffer>;

    fn from_buffer_ref_readable_gl<'a, 'b>(
        buffer: &'a gst::BufferRef,
        info: &'b VideoInfo,
    ) -> Result<VideoFrameRef<&'a gst::BufferRef>, glib::error::BoolError>;

    fn get_texture_id(&self, idx: u32) -> Option<u32>;
}

impl VideoFrameGLExt for VideoFrame<Readable> {
    fn from_buffer_readable_gl(
        buffer: gst::Buffer,
        info: &VideoInfo,
    ) -> Result<VideoFrame<Readable>, gst::Buffer> {
        skip_assert_initialized!();
        VideoFrameRef::<&gst::BufferRef>::from_buffer_readable_gl(buffer, info)
    }

    fn from_buffer_ref_readable_gl<'a, 'b>(
        buffer: &'a gst::BufferRef,
        info: &'b VideoInfo,
    ) -> Result<VideoFrameRef<&'a gst::BufferRef>, glib::error::BoolError> {
        skip_assert_initialized!();
        VideoFrameRef::<&gst::BufferRef>::from_buffer_ref_readable_gl(buffer, info)
    }

    fn get_texture_id(&self, idx: u32) -> Option<u32> {
        self.as_video_frame_ref().get_texture_id(idx)
    }
}

impl<'a> VideoFrameGLExt for VideoFrameRef<&'a gst::BufferRef> {
    fn from_buffer_readable_gl(
        buffer: gst::Buffer,
        info: &VideoInfo,
    ) -> Result<VideoFrame<Readable>, gst::Buffer> {
        skip_assert_initialized!();

        let n_mem = match buffer_n_gl_memory(buffer.as_ref()) {
            Some(n) => n,
            None => return Err(buffer),
        };

        // FIXME: planes are not memories, in multiview use case,
        // number of memories = planes * views, but the raw memory is
        // not exposed in videoframe
        if n_mem != info.n_planes() {
            return Err(buffer);
        }

        unsafe {
            let mut frame = mem::MaybeUninit::zeroed();
            let res: bool = from_glib(gst_video_sys::gst_video_frame_map(
                frame.as_mut_ptr(),
                info.to_glib_none().0 as *mut _,
                buffer.to_glib_none().0,
                gst_video_sys::GST_VIDEO_FRAME_MAP_FLAG_NO_REF
                    | gst_sys::GST_MAP_READ
                    | gst_gl_sys::GST_MAP_GL as u32,
            ));

            if !res {
                Err(buffer)
            } else {
                Ok(VideoFrame::from_glib_full(frame.assume_init()))
            }
        }
    }

    fn from_buffer_ref_readable_gl<'b, 'c>(
        buffer: &'b gst::BufferRef,
        info: &'c VideoInfo,
    ) -> Result<VideoFrameRef<&'b gst::BufferRef>, glib::error::BoolError> {
        skip_assert_initialized!();

        let n_mem = match buffer_n_gl_memory(buffer) {
            Some(n) => n,
            None => return Err(glib_bool_error!("Memory is not a GstGLMemory")),
        };

        // FIXME: planes are not memories, in multiview use case,
        // number of memories = planes * views, but the raw memory is
        // not exposed in videoframe
        if n_mem != info.n_planes() {
            return Err(glib_bool_error!(
                "Number of planes and memories is not matching"
            ));
        }

        unsafe {
            let mut frame = mem::MaybeUninit::zeroed();
            let res: bool = from_glib(gst_video_sys::gst_video_frame_map(
                frame.as_mut_ptr(),
                info.to_glib_none().0 as *mut _,
                buffer.as_mut_ptr(),
                gst_video_sys::GST_VIDEO_FRAME_MAP_FLAG_NO_REF
                    | gst_sys::GST_MAP_READ
                    | gst_gl_sys::GST_MAP_GL as u32,
            ));

            if !res {
                Err(glib_bool_error!(
                    "Failed to fill in the values of GstVideoFrame"
                ))
            } else {
                Ok(VideoFrameRef::from_glib_full(frame.assume_init()))
            }
        }
    }

    fn get_texture_id(&self, idx: u32) -> Option<u32> {
        let len = buffer_n_gl_memory(self.buffer())?;

        if idx >= len {
            return None;
        }

        // FIXME: planes are not memories
        if idx > self.n_planes() {
            return None;
        }

        let mut data = self.plane_data(idx).ok()?;
        let id = &data.read_u32::<NativeEndian>().ok()?;

        Some(*id)
    }
}

fn buffer_n_gl_memory(buffer: &gst::BufferRef) -> Option<u32> {
    skip_assert_initialized!();
    unsafe {
        let buf = buffer.as_mut_ptr();
        let num = gst_sys::gst_buffer_n_memory(buf);
        for i in 0..num - 1 {
            let mem = gst_sys::gst_buffer_peek_memory(buf, i);
            if gst_gl_sys::gst_is_gl_memory(mem) != glib_sys::GTRUE {
                return None;
            }
        }
        Some(num as u32)
    }
}