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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::marker::PhantomData;
use std::mem;
use std::slice;

use ffi::*;
use format;
use libc::{c_int, size_t};
use Error;

pub struct Picture<'a> {
    ptr: *mut AVPicture,

    format: format::Pixel,
    width: u32,
    height: u32,

    _own: bool,
    _marker: PhantomData<&'a ()>,
}

impl<'a> Picture<'a> {
    pub unsafe fn wrap(
        ptr: *mut AVPicture,
        format: format::Pixel,
        width: u32,
        height: u32,
    ) -> Self {
        Picture {
            ptr,

            format,
            width,
            height,

            _own: false,
            _marker: PhantomData,
        }
    }

    pub unsafe fn as_ptr(&self) -> *const AVPicture {
        self.ptr as *const _
    }

    pub unsafe fn as_mut_ptr(&mut self) -> *mut AVPicture {
        self.ptr
    }
}

impl<'a> Picture<'a> {
    pub fn size(format: format::Pixel, width: u32, height: u32) -> Result<usize, Error> {
        unsafe {
            match avpicture_get_size(format.into(), width as c_int, height as c_int) {
                v if v >= 0 => Ok(v as usize),
                e => Err(Error::from(e)),
            }
        }
    }

    pub fn new(format: format::Pixel, width: u32, height: u32) -> Result<Self, Error> {
        unsafe {
            let ptr = av_malloc(mem::size_of::<AVPicture>() as size_t) as *mut AVPicture;

            match avpicture_alloc(ptr, format.into(), width as c_int, height as c_int) {
                0 => Ok(Picture {
                    ptr,

                    format,
                    width,
                    height,

                    _own: true,
                    _marker: PhantomData,
                }),

                e => Err(Error::from(e)),
            }
        }
    }

    pub fn format(&self) -> format::Pixel {
        self.format
    }

    pub fn width(&self) -> u32 {
        self.width
    }

    pub fn height(&self) -> u32 {
        self.height
    }

    pub fn layout(&self, out: &mut [u8]) -> Result<usize, Error> {
        unsafe {
            match avpicture_layout(
                self.ptr,
                self.format.into(),
                self.width as c_int,
                self.height as c_int,
                out.as_mut_ptr(),
                out.len() as c_int,
            ) {
                s if s >= 0 => Ok(s as usize),
                e => Err(Error::from(e)),
            }
        }
    }

    pub fn layout_as(
        &self,
        format: format::Pixel,
        width: u32,
        height: u32,
        out: &mut [u8],
    ) -> Result<usize, Error> {
        unsafe {
            match avpicture_layout(
                self.as_ptr(),
                format.into(),
                width as c_int,
                height as c_int,
                out.as_mut_ptr(),
                out.len() as c_int,
            ) {
                s if s >= 0 => Ok(s as usize),
                e => Err(Error::from(e)),
            }
        }
    }

    pub fn crop(&self, source: &mut Picture, top: u32, left: u32) -> Result<(), Error> {
        if self.format != source.format {
            return Err(Error::Bug);
        }

        unsafe {
            match av_picture_crop(
                source.as_mut_ptr(),
                self.as_ptr(),
                self.format.into(),
                top as c_int,
                left as c_int,
            ) {
                0 => Ok(()),
                e => Err(Error::from(e)),
            }
        }
    }

    pub fn data(&self) -> Vec<&[u8]> {
        let mut result = Vec::new();

        unsafe {
            for (i, length) in (*self.as_ptr())
                .linesize
                .iter()
                .take_while(|l| **l > 0)
                .enumerate()
            {
                result.push(slice::from_raw_parts(
                    (*self.as_ptr()).data[i],
                    (*length as usize) * (self.height as usize),
                ))
            }
        }

        result
    }

    pub fn data_mut(&mut self) -> Vec<&mut [u8]> {
        let mut result = Vec::new();

        unsafe {
            for (i, length) in (*self.as_ptr())
                .linesize
                .iter()
                .take_while(|l| **l > 0)
                .enumerate()
            {
                result.push(slice::from_raw_parts_mut(
                    (*self.as_ptr()).data[i],
                    (*length as usize) * (self.height as usize),
                ))
            }
        }

        result
    }
}

impl<'a> Clone for Picture<'a> {
    fn clone(&self) -> Self {
        let mut pic = Picture::new(self.format, self.width, self.height).unwrap();
        pic.clone_from(self);

        pic
    }

    fn clone_from(&mut self, source: &Self) {
        unsafe {
            av_picture_copy(
                self.as_mut_ptr(),
                source.as_ptr(),
                source.format.into(),
                source.width as c_int,
                source.height as c_int,
            );
        }
    }
}

impl<'a> Drop for Picture<'a> {
    fn drop(&mut self) {
        if self._own {
            unsafe {
                av_free(self.as_mut_ptr() as *mut _);
            }
        }
    }
}