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
use core::{mem::MaybeUninit, ptr};
use x264::*;
use {Data, Encoding, Error, Image, Picture, Result, Setup};

/// Encodes video.
pub struct Encoder {
    raw: *mut x264_t,
    params: x264_param_t,
}

impl Encoder {
    /// Creates a new builder with default options.
    ///
    /// For more options see `Setup::new`.
    pub fn builder() -> Setup {
        Setup::default()
    }

    #[doc(hidden)]
    pub unsafe fn from_raw(raw: *mut x264_t) -> Self {
        let mut params = MaybeUninit::uninit();
        x264_encoder_parameters(raw, params.as_mut_ptr());
        Self {
            raw,
            params: params.assume_init(),
        }
    }

    /// Feeds a frame to the encoder.
    ///
    /// # Panics
    ///
    /// Panics if there is a mismatch between the image and the encoder
    /// regarding width, height or colorspace.
    pub fn encode(&mut self, pts: i64, image: Image) -> Result<(Data, Picture)> {
        assert_eq!(image.width(), self.width());
        assert_eq!(image.height(), self.height());
        assert_eq!(image.encoding(), self.encoding());
        unsafe { self.encode_unchecked(pts, image) }
    }

    /// Feeds a frame to the encoder.
    ///
    /// # Unsafety
    ///
    /// The caller must ensure that the width, height *and* colorspace
    /// of the image are the same as that of the encoder.
    pub unsafe fn encode_unchecked(&mut self, pts: i64, image: Image) -> Result<(Data, Picture)> {
        let image = image.raw();

        let mut picture = MaybeUninit::uninit();
        x264_picture_init(picture.as_mut_ptr());
        let mut picture = picture.assume_init();
        picture.i_pts = pts;
        picture.img = image;

        let mut len = 0;
        let mut stuff = MaybeUninit::uninit();
        let mut raw = MaybeUninit::uninit();

        let err = x264_encoder_encode(self.raw, stuff.as_mut_ptr(), &mut len, &mut picture, raw.as_mut_ptr());

        if err < 0 {
            Err(Error)
        } else {
            let stuff = stuff.assume_init();
            let raw = raw.assume_init();
            let data = Data::from_raw_parts(stuff, len as usize);
            let picture = Picture::from_raw(raw);
            Ok((data, picture))
        }
    }

    /// Gets the video headers, which should be sent first.
    pub fn headers(&mut self) -> Result<Data> {
        let mut len = 0;
        let mut stuff = MaybeUninit::uninit();

        let err = unsafe { x264_encoder_headers(self.raw, stuff.as_mut_ptr(), &mut len) };

        if err < 0 {
            Err(Error)
        } else {
            let stuff = unsafe { stuff.assume_init() };
            Ok(unsafe { Data::from_raw_parts(stuff, len as usize) })
        }
    }

    /// Begins flushing the encoder, to handle any delayed frames.
    ///
    /// ```rust
    /// # use x264::{Colorspace, Setup};
    /// # let encoder = Setup::default().build(Colorspace::RGB, 1920, 1080).unwrap();
    /// #
    /// let mut flush = encoder.flush();
    ///
    /// while let Some(result) = flush.next() {
    ///     if let Ok((data, picture)) = result {
    ///         // Handle data.
    ///     }
    /// }
    /// ```
    pub fn flush(self) -> Flush {
        Flush { encoder: self }
    }

    /// The width required of any input images.
    pub fn width(&self) -> i32 {
        self.params.i_width
    }
    /// The height required of any input images.
    pub fn height(&self) -> i32 {
        self.params.i_height
    }
    /// The encoding required of any input images.
    pub fn encoding(&self) -> Encoding {
        unsafe { Encoding::from_raw(self.params.i_csp) }
    }
}

impl Drop for Encoder {
    fn drop(&mut self) {
        unsafe {
            x264_encoder_close(self.raw);
        }
    }
}

/// Iterate through any delayed frames.
pub struct Flush {
    encoder: Encoder,
}

// TODO: Change to iterator when GATs are stabilized

//impl Iterator for Flush {
impl Flush {
    //type Item<'a> = Result<(Data<'a>, Picture)>;

    /// Keeps flushing.
    pub fn next(&mut self) -> Option<Result<(Data, Picture)>> {
        let enc = self.encoder.raw;

        if unsafe { x264_encoder_delayed_frames(enc) } == 0 {
            return None;
        }

        let mut len = 0;
        let mut stuff = MaybeUninit::uninit();
        let mut raw = MaybeUninit::uninit();

        let err =
            unsafe { x264_encoder_encode(enc, stuff.as_mut_ptr(), &mut len, ptr::null_mut(), raw.as_mut_ptr()) };

        Some(if err < 0 {
            Err(Error)
        } else {
            Ok(unsafe {
                (
                    Data::from_raw_parts(stuff.assume_init(), len as usize),
                    Picture::from_raw(raw.assume_init()),
                )
            })
        })
    }
}