onix 0.2.0

Decode image files using V4L2
Documentation
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Media API!

//#![warn(missing_docs)]

mod info;
mod ioctl;
mod media;
mod request;
mod topology;
mod util;
mod video;
pub mod vp8;

pub use info::DeviceInfo;
use log::{debug, trace};
pub use media::Media;
pub use request::Request;
pub use topology::{EntityFunction, InterfaceDevnode, LinkFlags, PadFlags, Topology};
pub use video::{
    BufType, Buffer, Capability, CapsFlags, ExportBuffer, ExtControl, ExtControls, FmtDesc, Format,
    MappedBuffer, Memory, Rect, Selection, Video,
};

use core::fmt;
use rustix::fd::OwnedFd;
use std::collections::HashMap;
use std::io;
use std::os::linux::fs::MetadataExt;
use std::path::Path;
use std::rc::Rc;

pub struct DrmFormats;

impl DrmFormats {
    pub const VP8F: u32 = 0x46385056;
    pub const NV12: u32 = 0x3231564e;
    pub const NM12: u32 = 0x32314d4e;
    pub const ST12: u32 = 0x32315453;
    pub const JPEG: u32 = 0x4745504a;
    pub const UYVY: u32 = 0x59565955;
    pub const YUYV: u32 = 0x56595559;
    pub const YM12: u32 = 0x32314d59;
}

struct DrmModifiers;

impl DrmModifiers {
    pub const LINEAR: u64 = 0;
    pub const ALLWINNER_TILED: u64 = 0x09000000_00000001;
}

fn v4l2_format_to_drm_format_and_modifier(format: u32) -> (u32, u64) {
    match format {
        // ST12 is actually NV12 with the Allwinner tiled modifier.
        DrmFormats::ST12 => (DrmFormats::NV12, DrmModifiers::ALLWINNER_TILED),

        // Assume linear otherwise.
        fmt => (fmt, DrmModifiers::LINEAR),
    }
}

#[derive(Debug)]
pub enum Error {
    IoError(io::Error),
    Errno(rustix::io::Errno),
    NoM2M,
    FormatNotFound,
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "media::Error")
    }
}

impl std::error::Error for Error {}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::IoError(err)
    }
}

impl From<rustix::io::Errno> for Error {
    fn from(err: rustix::io::Errno) -> Error {
        Error::Errno(err)
    }
}

#[derive(Debug)]
pub struct Plane {
    pub offset: u32,
    pub pitch: u32,
}

#[derive(Debug)]
pub struct Image {
    width: u32,
    height: u32,
    format: u32,
    modifier: u64,
    planes: [Option<Plane>; 4],

    out: Buffer,
    cap: Buffer,
}

impl Image {
    fn new(
        width: u32,
        height: u32,
        format: u32,
        modifier: u64,
        planes: [Option<Plane>; 4],
        is_mplane: bool,
        num_out_planes: usize,
        num_cap_planes: usize,
    ) -> Image {
        let (out, cap);
        if is_mplane {
            let out_planes = vec![video::Plane::new(); num_out_planes];
            let cap_planes = vec![video::Plane::new(); num_cap_planes];
            out = Buffer::with_planes(BufType::VideoOutputMplane, out_planes);
            cap = Buffer::with_planes(BufType::VideoCaptureMplane, cap_planes);
        } else {
            out = Buffer::new(BufType::VideoOutput);
            cap = Buffer::new(BufType::VideoCapture);
        }

        Image {
            width,
            height,
            format,
            modifier,
            planes,
            out,
            cap,
        }
    }

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

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

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

    pub fn modifier(&self) -> u64 {
        self.modifier
    }

    pub fn iter_planes(&self) -> impl Iterator<Item = &Plane> {
        self.planes.iter().filter_map(|plane| plane.as_ref())
    }

    pub fn out_mut(&mut self) -> &mut Buffer {
        &mut self.out
    }

    pub fn out(&self) -> &Buffer {
        &self.out
    }

    pub fn cap_mut(&mut self) -> &mut Buffer {
        &mut self.cap
    }

    pub fn cap(&self) -> &Buffer {
        &self.cap
    }
}

/// A hardware decoder for the VP8 format.
pub struct Decoder {
    media: Rc<Media>,
    video: Rc<Video>,
    is_mplane: bool,
    ctrl: Option<vp8::CtrlVp8Frame>,
}

/// A hardware encoder for the JPEG format
pub struct JpegEncoder {
    video: Rc<Video>,
    is_mplane: bool,
}

/// Main entrypoint of this crate.
pub struct Onix {
    decoders: HashMap<u32, (Rc<Media>, Rc<Video>, bool)>,
    encoders: HashMap<u32, (Rc<Media>, Rc<Video>, bool)>,
}

impl Onix {
    /// Find the available hardware decoders on the system.
    ///
    /// This goes through all of the /dev/media* devices, querying their
    /// topology to find a ProcVideoDecoder entity, fetching the interface used
    /// by its source and sink, opening the corresponding /dev/video* device,
    /// and keeping all of these devices open, ordered by supported formats.
    #[inline(never)]
    pub fn find_devices() -> Result<Onix, Error> {
        let mut videos = HashMap::new();
        let mut medias = HashMap::new();
        for dir_entry in std::fs::read_dir("/dev")? {
            let dir_entry = dir_entry?;
            let file_name = dir_entry.file_name();
            let file_name = file_name.to_str().unwrap();
            if file_name.starts_with("video") {
                let path = dir_entry.path();
                if let Ok(metadata) = path.metadata() {
                    let dev_t = metadata.st_rdev();
                    trace!("Found video {} ({dev_t})", path.display());
                    videos.insert(dev_t, path);
                }
            } else if file_name.starts_with("media") {
                let path = dir_entry.path();
                if let Some((media, media_interfaces)) = Self::discover_media(&path) {
                    let media = Rc::new(media);
                    for interface in media_interfaces {
                        medias.insert(interface, Rc::clone(&media));
                    }
                } else {
                    debug!("{} doesn’t have a valid decoder, skipping.", path.display());
                }
            }
        }
        let mut decoders = HashMap::new();
        let mut encoders = HashMap::new();
        for ((is_encoder, interface), media) in medias {
            let path = &videos[&interface];
            if let Ok((video, is_mplane, formats)) = Self::discover_video(path, is_encoder) {
                let video = Rc::new(video);
                let devices = if is_encoder {
                    &mut encoders
                } else {
                    &mut decoders
                };
                for format in formats {
                    devices.insert(format, (Rc::clone(&media), Rc::clone(&video), is_mplane));
                }
            } else {
                debug!("{} isn’t a valid decoder, skipping.", path.display());
            }
        }
        Ok(Onix { decoders, encoders })
    }

    pub fn load_from_memory(&self, buffer: &[u8]) -> Result<(Decoder, Image), Error> {
        if buffer.starts_with(b"\xff\xd8\xff") {
            // Skim through the markers up to the SOF0, which contains
            // width/height and other useful info.
            let mut p = buffer;
            let (width, height);
            loop {
                if p[0] != 0xff {
                    return Err(Error::IoError(io::Error::new(
                        io::ErrorKind::Other,
                        "invalid marker in JPEG file",
                    )));
                }
                let marker = u16::from_be_bytes([p[0], p[1]]);
                p = &p[2..];
                if marker == 0xffd8 {
                    continue;
                }
                let size = u16::from_be_bytes([p[0], p[1]]);
                // Found the SOF0!
                if marker == 0xffc0 {
                    p = &p[2..];
                    height = u16::from_be_bytes([p[1], p[2]]) as u32;
                    width = u16::from_be_bytes([p[3], p[4]]) as u32;
                    break;
                }
                p = &p[size as usize..];
            }
            let input_format = DrmFormats::JPEG;

            let mut decoder = self.get_decoder(input_format, None).unwrap();
            let mut image = decoder.prepare(width, height, input_format, buffer.len() as u32)?;

            {
                let buf = image.out_mut();
                let map = decoder.mmap(buf, 0)?;
                map.as_mut_slice().copy_from_slice(buffer);
                buf.set_bytesused(0, buffer.len());
            }

            decoder.queue(&mut image)?;
            Ok((decoder, image))
        } else if buffer.starts_with(b"RIFF") {
            // Read the relevant information from the WebP header.
            //
            // Note: only lossy WebP, both static (non-animated) and opaque (without an
            // alpha channel) are supported for now, hence the asserts.  Lossless WebP
            // and alpha channels use a completely different codec than VP8, so they
            // require software support which isn’t part of this crate.  Animated WebP
            // should be possible to support, but only when every frame is opaque.
            assert_eq!(&buffer[..4], b"RIFF");
            let riff_len = u32::from_le_bytes([buffer[4], buffer[5], buffer[6], buffer[7]]);
            assert_eq!(riff_len as usize, buffer.len() - 8);
            assert_eq!(&buffer[8..12], b"WEBP");
            assert_eq!(&buffer[12..16], b"VP8 ");
            let vp8_len = u32::from_le_bytes([buffer[16], buffer[17], buffer[18], buffer[19]]);
            assert_eq!(vp8_len as usize, buffer.len() - 20);
            let vp8 = &buffer[20..];

            let ctrl = {
                let mut parser = crate::vp8::Parser::new(vp8);
                parser.parse_vp8().unwrap()
            };

            let width = ctrl.width as u32;
            let height = ctrl.height as u32;
            let input_format = DrmFormats::VP8F;

            let mut decoder = self.get_decoder(input_format, Some(ctrl)).unwrap();
            let mut image = decoder.prepare(width, height, input_format, vp8_len)?;

            {
                let buf = image.out_mut();
                let map = decoder.mmap(buf, 0)?;
                map.as_mut_slice().copy_from_slice(vp8);
                buf.set_bytesused(0, vp8_len as usize);
            }

            decoder.queue(&mut image)?;
            Ok((decoder, image))
        } else {
            Err(Error::IoError(io::Error::new(
                io::ErrorKind::Other,
                "unknown file format",
            )))
        }
    }

    pub fn open<P: AsRef<Path>>(&self, path: P) -> Result<(Decoder, Image), Error> {
        let buffer = std::fs::read(path)?;
        self.load_from_memory(&buffer)
    }

    /// Get a decoder for the provided format, if found in [`Self::find_devices`].
    pub fn get_decoder(&self, format: u32, ctrl: Option<vp8::CtrlVp8Frame>) -> Option<Decoder> {
        if let Some((media, video, is_mplane)) = self.decoders.get(&format) {
            Some(Decoder {
                media: media.clone(),
                video: video.clone(),
                is_mplane: *is_mplane,
                ctrl,
            })
        } else {
            None
        }
    }

    /// Get a JPEG encoder, if found in [`Self::find_devices`].
    pub fn jpeg_encoder(&self) -> Option<JpegEncoder> {
        if let Some((_, video, is_mplane)) = self.encoders.get(&DrmFormats::JPEG) {
            Some(JpegEncoder {
                video: video.clone(),
                is_mplane: *is_mplane,
            })
        } else {
            None
        }
    }

    fn discover_media(path: &Path) -> Option<(Media, Vec<(bool, u64)>)> {
        let media = Media::open(path).ok()?;
        let topology = media.get_topology().ok()?;
        trace!("Found media {}", path.display());
        for interface in topology.interfaces() {
            trace!("    {interface:?}");
        }
        for pad in topology.pads() {
            trace!("    {pad:?}");
        }
        for link in topology.links() {
            trace!("    {link:?}");
        }
        let mut interfaces = Vec::new();
        for entity in topology.entities() {
            trace!("    {entity:?}");
            if entity.function() == EntityFunction::ProcVideoDecoder {
                trace!("        … is a decoder!");
                let mut interface_id = None;
                for pad in topology.get_pads_for_entity(entity.id()) {
                    trace!("        {pad:?}");
                    let pad_id = if pad.flags().contains(PadFlags::SOURCE) {
                        let link = topology.get_link_by_source_id(pad.id())?;
                        trace!("            {link:?}");
                        link.sink_id()
                    } else
                    /*if pad.flags().contains(PadFlags::SINK)*/
                    {
                        let link = topology.get_link_by_sink_id(pad.id())?;
                        trace!("            {link:?}");
                        link.source_id()
                    };
                    let pad = topology.get_pad(pad_id)?;
                    trace!("                {pad:?}");
                    let entity = topology.get_entity(pad.entity_id())?;
                    trace!("                    {entity:?}");
                    let link = topology.get_link_by_sink_id(entity.id())?;
                    trace!("                        {link:?}");
                    assert!(link.flags().contains(LinkFlags::INTERFACE_LINK));
                    if let Some(interface_id) = interface_id {
                        assert_eq!(interface_id, link.source_id());
                    } else {
                        interface_id = Some(link.source_id());
                    }
                }
                let interface = topology.get_interface(interface_id.unwrap())?;
                trace!("        {interface:?}");
                //assert_eq!(interface.intf_type(), InterfaceType::V4LVideo);
                let InterfaceDevnode { major, minor } = interface.devnode();
                let dev_t = rustix::fs::makedev(major, minor);
                trace!("            {major},{minor} -> {dev_t}");
                interfaces.push((false, dev_t));
            } else if entity.function() == EntityFunction::ProcVideoEncoder {
                trace!("        … is an encoder!");
                let mut interface_id = None;
                for pad in topology.get_pads_for_entity(entity.id()) {
                    trace!("        {pad:?}");
                    let pad_id = if pad.flags().contains(PadFlags::SOURCE) {
                        let link = topology.get_link_by_source_id(pad.id())?;
                        trace!("            {link:?}");
                        link.sink_id()
                    } else
                    /*if pad.flags().contains(PadFlags::SINK)*/
                    {
                        let link = topology.get_link_by_sink_id(pad.id())?;
                        trace!("            {link:?}");
                        link.source_id()
                    };
                    let pad = topology.get_pad(pad_id)?;
                    trace!("                {pad:?}");
                    let entity = topology.get_entity(pad.entity_id())?;
                    trace!("                    {entity:?}");
                    let link = topology.get_link_by_sink_id(entity.id())?;
                    trace!("                        {link:?}");
                    assert!(link.flags().contains(LinkFlags::INTERFACE_LINK));
                    if let Some(interface_id) = interface_id {
                        assert_eq!(interface_id, link.source_id());
                    } else {
                        interface_id = Some(link.source_id());
                    }
                }
                let interface = topology.get_interface(interface_id.unwrap())?;
                trace!("        {interface:?}");
                //assert_eq!(interface.intf_type(), InterfaceType::V4LVideo);
                let InterfaceDevnode { major, minor } = interface.devnode();
                let dev_t = rustix::fs::makedev(major, minor);
                trace!("            {major},{minor} -> {dev_t}");
                interfaces.push((true, dev_t));
            }
        }
        Some((media, interfaces))
    }

    fn discover_video(path: &Path, is_encoder: bool) -> Result<(Video, bool, Vec<u32>), Error> {
        let video = Video::open(path)?;
        let caps = video.querycap()?;
        let is_mplane = if caps.capabilities().contains(CapsFlags::VIDEO_M2M) {
            false
        } else if caps.capabilities().contains(CapsFlags::VIDEO_M2M_MPLANE) {
            true
        } else {
            return Err(Error::NoM2M);
        };
        let buf_type = if is_encoder {
            BufType::capture(is_mplane)
        } else {
            BufType::output(is_mplane)
        };
        let formats = video
            .enum_fmts(buf_type)?
            .iter()
            .map(|fmt| fmt.pixelformat())
            .collect();
        Ok((video, is_mplane, formats))
    }
}

impl JpegEncoder {
    /// Returns the path to the [`Video`] device used by this decoder.
    pub fn video_path(&self) -> &Path {
        self.video.path()
    }

    /// Returns the [`Capability`] of the [`Video`] device used by this decoder.
    pub fn video_querycap(&self) -> Result<Capability, Error> {
        Ok(self.video.querycap()?)
    }

    pub fn prepare(
        &mut self,
        width: u32,
        height: u32,
        input_format: u32,
        compression_quality: i32,
    ) -> Result<Image, Error> {
        let num_planes = match input_format {
            DrmFormats::YUYV => 1,
            DrmFormats::UYVY => 1,
            DrmFormats::NM12 => 2,
            DrmFormats::YM12 => 3,
            format => panic!("TODO: Add support for format {format}"),
        };

        let out_type = BufType::output(self.is_mplane);
        let cap_type = BufType::capture(self.is_mplane);

        // Set the capture format.
        let format = DrmFormats::JPEG;
        let mut format = Format::new(cap_type, width, height, format);
        self.video.s_fmt(&mut format)?;

        // Set the output format.
        let mut format = Format::new(out_type, width, height, input_format);
        self.video.s_fmt(&mut format)?;

        // Configure the compression quality.
        const V4L2_CID_JPEG_COMPRESSION_QUALITY: u32 = 0x009d0903;
        let mut query = video::QueryCtrl::new(V4L2_CID_JPEG_COMPRESSION_QUALITY);
        self.video.queryctrl(&mut query)?;
        let compression_quality = compression_quality.clamp(query.minimum(), query.maximum());
        let mut ctrl = video::Control::new(V4L2_CID_JPEG_COMPRESSION_QUALITY, compression_quality);
        self.video.s_ctrl(&mut ctrl)?;

        // Set the proper resolution in the final image.
        const V4L2_SEL_TGT_CROP: u32 = 0;
        let rect = Rect::new(0, 0, width, height);
        let mut selection =
            Selection::new(BufType::output(self.is_mplane), V4L2_SEL_TGT_CROP, rect);
        self.video.s_selection(&mut selection)?;

        let pitch = format.bytesperline(0);
        let planes = [Some(Plane { offset: 0, pitch }), None, None, None];

        // Allocate the buffers.
        let mut image = Image::new(
            width,
            height,
            DrmFormats::JPEG,
            DrmModifiers::LINEAR,
            planes,
            self.is_mplane,
            num_planes,
            1,
        );
        self.video.reqbufs(Memory::Mmap, out_type, 1)?;
        self.video.querybuf(&mut image.out)?;
        self.video.reqbufs(Memory::Mmap, cap_type, 1)?;
        self.video.querybuf(&mut image.cap)?;

        // Start streaming.
        self.video.streamon(out_type)?;
        self.video.streamon(cap_type)?;

        Ok(image)
    }

    pub fn mmap(&mut self, buf: &mut Buffer, num_plane: usize) -> Result<MappedBuffer, Error> {
        Ok(self.video.mmap(buf, num_plane)?)
    }

    pub fn queue(&mut self, image: &mut Image) -> Result<(), Error> {
        self.video.qbuf(&mut image.out)?;
        self.video.qbuf(&mut image.cap)?;
        Ok(())
    }

    pub fn dequeue(&mut self, image: &mut Image) -> Result<(), Error> {
        self.video.dqbuf(&mut image.out)?;
        self.video.dqbuf(&mut image.cap)?;
        if image.cap.is_error() {
            Err(io::Error::new(io::ErrorKind::Other, "error while encoding"))?;
        }
        Ok(())
    }

    /// Finish the decoding process and return everything needed to import it
    /// into other APIs.
    pub fn stop(&mut self) -> Result<(), Error> {
        self.video.streamoff(BufType::output(self.is_mplane))?;
        self.video.streamoff(BufType::capture(self.is_mplane))?;
        Ok(())
    }
}

impl Decoder {
    /// Returns the [`DeviceInfo`] of the [`Media`] device used by this decoder.
    pub fn media_device_info(&self) -> Result<DeviceInfo, Error> {
        Ok(self.media.device_info()?)
    }

    /// Returns the [`Capability`] of the [`Video`] device used by this decoder.
    pub fn video_querycap(&self) -> Result<Capability, Error> {
        Ok(self.video.querycap()?)
    }

    /// Sets the input format for this decoder.
    pub fn prepare(
        &mut self,
        width: u32,
        height: u32,
        input_format: u32,
        length: u32,
    ) -> Result<Image, Error> {
        let out_type = BufType::output(self.is_mplane);
        let cap_type = BufType::capture(self.is_mplane);

        // TODO: apply the constraints we get here, for instance reject
        // width/height bigger than this.
        if let Ok(sizes) = self.video.enum_framesizes(input_format) {
            for size in sizes {
                trace!("{size:#?}");
            }
        }

        let mut format = Format::new(out_type, width, height, input_format);
        format.set_sizeimage(0, length);
        self.video.s_fmt(&mut format)?;

        // Figure out the best capture format.
        let mut format = self.video.g_fmt(cap_type)?;

        let pitch = format.bytesperline(0);
        let offset = format.height() * pitch;
        let (drm_format, drm_modifier) =
            v4l2_format_to_drm_format_and_modifier(format.pixelformat());
        let planes = [
            Some(Plane { offset: 0, pitch }),
            Some(Plane { offset, pitch }),
            None,
            None,
        ];

        // Allocate the buffers.
        let mut image = Image::new(
            width,
            height,
            drm_format,
            drm_modifier,
            planes,
            self.is_mplane,
            1,
            1,
        );
        self.video.reqbufs(Memory::Mmap, out_type, 1)?;
        self.video.querybuf(&mut image.out)?;
        self.video.reqbufs(Memory::Mmap, cap_type, 1)?;
        self.video.querybuf(&mut image.cap)?;

        // Start streaming.
        self.video.streamon(cap_type)?;
        self.video.streamon(out_type)?;

        Ok(image)
    }

    pub fn queue(&mut self, image: &mut Image) -> Result<(), Error> {
        let request = if let Some(ctrl) = &mut self.ctrl {
            let request = self.media.request_alloc()?;

            image.out.set_request(&request);

            let mut ext_ctrl = ExtControl::new(vp8::uapi::V4L2_CID_STATELESS_VP8_FRAME, ctrl);
            let mut ext_ctrls = ExtControls::new(1, &mut ext_ctrl);
            ext_ctrls.set_request(&request);
            self.video.s_ext_ctrls(&mut ext_ctrls)?;

            Some(request)
        } else {
            None
        };

        self.video.qbuf(&mut image.cap)?;
        self.video.qbuf(&mut image.out)?;

        if let Some(request) = request {
            request.queue()?;
        }

        Ok(())
    }

    pub fn expbuf(&mut self, buf: &Buffer, num_plane: usize) -> Result<OwnedFd, Error> {
        const O_CLOEXEC: u32 = 0x00080000;
        let mut expbuf = ExportBuffer::new(buf.type_(), buf.index(), num_plane as u32, O_CLOEXEC);
        self.video.expbuf(&mut expbuf)?;
        Ok(expbuf.into_fd())
    }

    pub fn mmap(&mut self, buf: &mut Buffer, num_plane: usize) -> io::Result<MappedBuffer> {
        Ok(self.video.mmap(buf, num_plane)?)
    }

    pub fn dequeue(&mut self, image: &mut Image) -> Result<(), Error> {
        self.video.dqbuf(&mut image.out)?;
        self.video.dqbuf(&mut image.cap)?;
        if image.cap.is_error() {
            Err(io::Error::new(io::ErrorKind::Other, "error while decoding"))?;
        }
        Ok(())
    }

    pub fn stop(&mut self) -> Result<(), Error> {
        self.video.streamoff(BufType::output(self.is_mplane))?;
        self.video.streamoff(BufType::capture(self.is_mplane))?;
        Ok(())
    }
}