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
#![warn(missing_docs)]

//! Encoding video with x264.

extern crate framing;
extern crate x264_sys;

mod data;
mod format;
mod image;
mod setup;

pub use data::{Data, Priority, Unit};
pub use format::Format;
pub use image::Image;
pub use setup::{Preset, Setup, Tune};

use std::{mem, ptr};
use std::marker::PhantomData;
use x264_sys::x264;

/// Encodes your video. Be nice to it!
pub struct Encoder<F> {
    raw: *mut x264::x264_t,
    spooky: PhantomData<F>
}

impl<F> Encoder<F> {
    /// Feeds the encoder.
    ///
    /// You have to give the encoder an image and a
    /// [`pts`](https://en.wikipedia.org/wiki/Presentation_timestamp) to encode.
    /// The output is a byte sequence you can feed to stuff, and some image
    /// data that you can use, too - you hopefully being a muxer or something.
    pub fn encode<T>(&mut self, pts: i64, image: &T)
        -> Result<(Data, Picture), Error>
    where
        T: Image<Format = F>,
        F: Format
    {
        let mut picture = unsafe {
            let mut picture = mem::uninitialized();
            x264::x264_picture_init(&mut picture);
            picture.i_pts = pts;
            picture.img = x264ify(image);
            picture
        };

        let mut len = 0;
        let mut stuff = unsafe { mem::uninitialized() };
        let mut raw = unsafe { mem::uninitialized() };

        let err = unsafe {
            x264::x264_encoder_encode(
                self.raw,
                &mut stuff,
                &mut len,
                &mut picture,
                &mut raw
            )
        };

        if err < 0 {
            Err(Error)
        } else {
            let data = unsafe { Data::from_raw_parts(stuff, len as _) };
            Ok((data, Picture { raw }))
        }
    }

    /// Tells the encoder to keep encoding.
    ///
    /// I guess you'd use this because the encoder somehow delayed some frames
    /// or something and so isn't done yet. Don't ask me, I don't know how any
    /// of this works either! That said, you might call in a loop until it's
    /// `done` before disposing of it, if you aren't using the encoder for
    /// streaming purposes (e.g. if you're actually saving the file.)
    pub fn work(&mut self) -> Result<(Data, Picture), Error> {
        let mut len = 0;
        let mut stuff = unsafe { mem::uninitialized() };
        let mut raw = unsafe { mem::uninitialized() };

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

        if err < 0 {
            Err(Error)
        } else {
            let data = unsafe { Data::from_raw_parts(stuff, len as _) };
            Ok((data, Picture { raw }))
        }
    }

    /// Gets the video headers.
    ///
    /// Send this before sending other things.
    pub fn headers(&mut self) -> Result<Data, Error> {
        let mut len = 0;
        let mut stuff = unsafe { mem::uninitialized() };

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

        if 0 > err {
            return Err(Error);
        }

        Ok(unsafe { Data::from_raw_parts(stuff, len as _) })
    }

    /// Indicates whether the encoder is done.
    ///
    /// Again, not sure on the specifics, but it's something to do with delayed
    /// frames, and if it's not done it *probably* won't be done until you call
    /// `work` until it says it **is** done.
    pub fn done(&self) -> bool {
        unsafe { 0 == x264::x264_encoder_delayed_frames(self.raw) }
    }
}

impl<F> Drop for Encoder<F> {
    fn drop(&mut self) {
        unsafe { x264::x264_encoder_close(self.raw); }
    }
}

/// Output picture data.
pub struct Picture {
    raw: x264::x264_picture_t
}

impl Picture {
    /// Whether the picture is a keyframe.
    pub fn keyframe(&self) -> bool {
        self.raw.b_keyframe != 0
    }

    /// The presentation timestamp.
    pub fn pts(&self) -> i64 {
        self.raw.i_pts
    }

    /// The decoding timestamp.
    pub fn dts(&self) -> i64 {
        self.raw.i_dts
    }
}

/// An opaque error.
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Error;

fn x264ify<T: Image>(img: &T) -> x264::x264_image_t {
    let planes = img.planes();
    x264::x264_image_t {
        i_csp: T::Format::colorspace(),
        i_plane: T::Format::plane_count(),
        i_stride: img.strides(),
        plane:
            [
                planes[0] as _,
                planes[1] as _,
                planes[2] as _,
                planes[3] as _
            ]
    }
}