aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-codec-support/src/codecs/mod.rs
blob: 6589e477e4e3eaab4f57d9a0a9498c06a2ed39df (plain) (blame)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//! Decoder support functions and definitions.
use std::fmt;
use std::ops::{Add, AddAssign, Sub, SubAssign};

pub use nihav_core::frame::*;
use std::mem;

/// Frame manager for hold-and-modify codecs.
///
/// This frame manager simplifies frame management for the case when codec decodes new frame by updating parts of the previous frame.
///
/// # Examples
///
/// ````norun
/// let mut frame = if is_intra_frame {
///         allocate_video_frame()
///     } else {
///         let ret = shuffler.clone_ref();
///         if ret.is_none() {
///             return Err(DecodingError::MissingReference);
///         }
///         ret.unwrap()
///     };
/// // output data into the frame
/// shuffler.add_frame(frame.clone()); // tells frame manager to use the frame as the next reference
/// ````
#[allow(dead_code)]
pub struct HAMShuffler {
    lastframe: Option<NAVideoBufferRef<u8>>,
}

impl HAMShuffler {
    /// Constructs a new instance of frame manager.
    #[allow(dead_code)]
    pub fn new() -> Self { HAMShuffler { lastframe: None } }
    /// Clears the reference.
    #[allow(dead_code)]
    pub fn clear(&mut self) { self.lastframe = None; }
    /// Sets a new frame reference.
    #[allow(dead_code)]
    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
        self.lastframe = Some(buf);
    }
    /// Provides a copy of the reference frame if present or `None` if it is not.
    #[allow(dead_code)]
    pub fn clone_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
        if let Some(ref mut frm) = self.lastframe {
            let newfrm = frm.copy_buffer();
            *frm = newfrm.clone().into_ref();
            Some(newfrm.into_ref())
        } else {
            None
        }
    }
    /// Returns the original saved reference frame or `None` if it is not present.
    #[allow(dead_code)]
    pub fn get_output_frame(&mut self) -> Option<NAVideoBufferRef<u8>> {
        match self.lastframe {
            Some(ref frm) => Some(frm.clone()),
            None => None,
        }
    }
}

impl Default for HAMShuffler {
    fn default() -> Self { Self { lastframe: None } }
}

/// Frame manager for codecs with intra and inter frames.
///
/// This frame manager simplifies frame management for the case when codec decodes new frame using previous frame as source of some data.
///
/// # Examples
///
/// ````norun
/// let mut frame = allocate_video_frame();
/// if is_inter_frame {
///     let ret = shuffler.get_ref();
///     if ret.is_none() {
///         return Err(DecodingError::MissingReference);
///     }
///     let ref_frame = ret.unwrap();
///     // keep decoding using data from ref_frame
/// }
/// shuffler.add_frame(frame.clone()); // tells frame manager to use the frame as the next reference
/// ````
#[allow(dead_code)]
pub struct IPShuffler {
    lastframe: Option<NAVideoBufferRef<u8>>,
}

impl IPShuffler {
    /// Constructs a new instance of frame manager.
    #[allow(dead_code)]
    pub fn new() -> Self { IPShuffler { lastframe: None } }
    /// Clears the reference.
    #[allow(dead_code)]
    pub fn clear(&mut self) { self.lastframe = None; }
    /// Sets a new frame reference.
    #[allow(dead_code)]
    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
        self.lastframe = Some(buf);
    }
    /// Returns the original saved reference frame or `None` if it is not present.
    #[allow(dead_code)]
    pub fn get_ref(&mut self) -> Option<NAVideoBufferRef<u8>> {
        if let Some(ref frm) = self.lastframe {
            Some(frm.clone())
        } else {
            None
        }
    }
}

impl Default for IPShuffler {
    fn default() -> Self { Self { lastframe: None } }
}

/// Frame manager for codecs with I-, P- and B-frames.
///
/// This frame manager simplifies frame management for the case when codec uses I/P/B frame scheme.
///
/// # Examples
///
/// ````norun
/// let mut frame = allocate_video_frame();
/// for mb in all_macroblocks {
///     // decode macroblock type
///     match mb_type {
///         MBType::Inter => {
///             do_mc(&mut frame, shuffler.get_lastref().unwrap());
///         },
///         MBType::BForward => {
///             do_mc(&mut frame, shuffler.get_b_fwdref().unwrap());
///         },
///         MBType::BBackward => {
///             do_mc(&mut frame, shuffler.get_b_bwdref().unwrap());
///         },
///         // handle the rest of cases
///     };
/// if is_random_access_frame {
///     shuffler.clear(); // remove all saved references
/// }
/// if is_intra_frame || is_p_frame {
///     shuffler.add_frame(frame.clone()); // tells frame manager to use the frame as the next reference
/// }
/// ````
#[allow(dead_code)]
pub struct IPBShuffler {
    lastframe: Option<NAVideoBufferRef<u8>>,
    nextframe: Option<NAVideoBufferRef<u8>>,
}

impl IPBShuffler {
    /// Constructs a new instance of frame manager.
    #[allow(dead_code)]
    pub fn new() -> Self { IPBShuffler { lastframe: None, nextframe: None } }
    /// Clears the reference.
    #[allow(dead_code)]
    pub fn clear(&mut self) { self.lastframe = None; self.nextframe = None; }
    /// Sets a new frame reference.
    #[allow(dead_code)]
    pub fn add_frame(&mut self, buf: NAVideoBufferRef<u8>) {
        mem::swap(&mut self.lastframe, &mut self.nextframe);
        self.lastframe = Some(buf);
    }
    /// Returns the previous reference frame or `None` if it is not present.
    #[allow(dead_code)]
    pub fn get_lastref(&mut self) -> Option<NAVideoBufferRef<u8>> {
        if let Some(ref frm) = self.lastframe {
            Some(frm.clone())
        } else {
            None
        }
    }
    /// Returns second last reference frame or `None` if it is not present.
    #[allow(dead_code)]
    pub fn get_nextref(&mut self) -> Option<NAVideoBufferRef<u8>> {
        if let Some(ref frm) = self.nextframe {
            Some(frm.clone())
        } else {
            None
        }
    }
    /// Returns the temporally following reference for B-frame or `None` if it is not present.
    #[allow(dead_code)]
    pub fn get_b_fwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
        if let Some(ref frm) = self.nextframe {
            Some(frm.clone())
        } else {
            None
        }
    }
    /// Returns the temporally preceeding reference for B-frame or `None` if it is not present.
    #[allow(dead_code)]
    pub fn get_b_bwdref(&mut self) -> Option<NAVideoBufferRef<u8>> {
        if let Some(ref frm) = self.lastframe {
            Some(frm.clone())
        } else {
            None
        }
    }
}

impl Default for IPBShuffler {
    fn default() -> Self { Self { lastframe: None, nextframe: None } }
}

/// Motion vector data type.
///
/// # Examples
///
/// ```
/// use nihav_codec_support::codecs::MV;
///
/// let mv0 = MV::new(1, 3);
/// let mv1 = MV { x: 2, y: 3 }; // choose whatever style you prefer
/// let mv2 = mv1 - mv0;
/// let mv_pred = MV::pred(mv0, mv1, mv2); // get median prediction for the vectors (1, 0)
/// ```
#[derive(Debug,Clone,Copy,Default,PartialEq)]
pub struct MV {
    /// X coordinate of the vector.
    pub x: i16,
    /// Y coordinate of the vector.
    pub y: i16,
}

#[allow(clippy::many_single_char_names)]
#[allow(clippy::collapsible_if)]
impl MV {
    /// Creates a new motion vector instance.
    pub fn new(x: i16, y: i16) -> Self { MV{ x, y } }
    /// Predicts median from provided motion vectors.
    ///
    /// Each component of the vector is predicted as the median of corresponding input vector components.
    pub fn pred(a: MV, b: MV, c: MV) -> Self {
        let x;
        if a.x < b.x {
            if b.x < c.x {
                x = b.x;
            } else {
                if a.x < c.x { x = c.x; } else { x = a.x; }
            }
        } else {
            if b.x < c.x {
                if a.x < c.x { x = a.x; } else { x = c.x; }
            } else {
                x = b.x;
            }
        }
        let y;
        if a.y < b.y {
            if b.y < c.y {
                y = b.y;
            } else {
                if a.y < c.y { y = c.y; } else { y = a.y; }
            }
        } else {
            if b.y < c.y {
                if a.y < c.y { y = a.y; } else { y = c.y; }
            } else {
                y = b.y;
            }
        }
        MV { x, y }
    }
}

/// Zero motion vector.
pub const ZERO_MV: MV = MV { x: 0, y: 0 };

impl Add for MV {
    type Output = MV;
    fn add(self, other: MV) -> MV { MV { x: self.x + other.x, y: self.y + other.y } }
}

impl AddAssign for MV {
    fn add_assign(&mut self, other: MV) { self.x += other.x; self.y += other.y; }
}

impl Sub for MV {
    type Output = MV;
    fn sub(self, other: MV) -> MV { MV { x: self.x - other.x, y: self.y - other.y } }
}

impl SubAssign for MV {
    fn sub_assign(&mut self, other: MV) { self.x -= other.x; self.y -= other.y; }
}

impl fmt::Display for MV {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{},{}", self.x, self.y)
    }
}

#[cfg(any(feature="blockdsp"))]
pub mod blockdsp;

#[cfg(feature="h263")]
#[allow(clippy::collapsible_if)]
#[allow(clippy::manual_memcpy)]
#[allow(clippy::needless_range_loop)]
pub mod h263;

/// The common 8x8 zigzag scan.
pub const ZIGZAG: [usize; 64] = [
     0,  1,  8, 16,  9,  2,  3, 10,
    17, 24, 32, 25, 18, 11,  4,  5,
    12, 19, 26, 33, 40, 48, 41, 34,
    27, 20, 13,  6,  7, 14, 21, 28,
    35, 42, 49, 56, 57, 50, 43, 36,
    29, 22, 15, 23, 30, 37, 44, 51,
    58, 59, 52, 45, 38, 31, 39, 46,
    53, 60, 61, 54, 47, 55, 62, 63
];

pub mod imaadpcm;