summary refs log tree commit diff
path: root/devices/src/virtio/video/decoder/mod.rs
blob: 3516cad69c9285fdfdc1264004821dd5a38b2b08 (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
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Implementation of a virtio video decoder device backed by LibVDA.

use std::collections::btree_map::Entry;
use std::collections::BTreeMap;
use std::convert::TryInto;
use std::fs::File;
use std::os::unix::io::{AsRawFd, IntoRawFd};

use sys_util::{error, PollContext};

use crate::virtio::resource_bridge::{self, ResourceInfo, ResourceRequestSocket};
use crate::virtio::video::command::{QueueType, VideoCmd};
use crate::virtio::video::control::{CtrlType, CtrlVal, QueryCtrlResponse, QueryCtrlType};
use crate::virtio::video::device::*;
use crate::virtio::video::error::*;
use crate::virtio::video::event::*;
use crate::virtio::video::format::*;
use crate::virtio::video::params::Params;
use crate::virtio::video::response::CmdResponse;

mod capability;
use capability::*;

type StreamId = u32;
type ResourceId = u32;

// ResourceId given by the driver
type InputResourceId = u32;
type OutputResourceId = u32;

// Id for a frame buffer passed to Chrome.
// We cannot use OutputResourceId as is because this ID must be between 0 and ((# of buffers) - 1).
//
// TODO(b/1518105): Once we decide to generate resource_id in the device side,
// we don't need this value and can pass OutputResourceId to Chrome directly.
type FrameBufferId = i32;

type ResourceHandle = u32;
type Timestamp = u64;

// Represents queue types of pending Clear commands if exist.
#[derive(Default)]
struct PendingClearCmds {
    input: bool,
    output: bool,
}

// Context is associated with one `libvda::Session`, which corresponds to one stream from the
// virtio-video's point of view.
#[derive(Default)]
struct Context {
    stream_id: StreamId,

    in_params: Params,
    out_params: Params,

    // Timestamp -> InputResourceId
    timestamp_to_input_res_id: BTreeMap<Timestamp, InputResourceId>,
    // {Input,Output}ResourceId -> ResourceHandle
    res_id_to_res_handle: BTreeMap<u32, ResourceHandle>,

    // OutputResourceId <-> FrameBufferId
    res_id_to_frame_buf_id: BTreeMap<OutputResourceId, FrameBufferId>,
    frame_buf_id_to_res_id: BTreeMap<FrameBufferId, OutputResourceId>,

    keep_resources: Vec<File>,

    // Stores queue types of pending Clear commands if exist.
    // This is needed because libvda's Reset API clears both queues while virtio-video's Clear is
    // called for each queue.
    pending_clear_cmds: PendingClearCmds,

    // This is a flag that shows whether libvda's set_output_buffer_count is called.
    // This will be set to true when ResourceCreate for OutputBuffer is called for the first time.
    //
    // TODO(b/1518105): This field is added as a hack because the current virtio-video v3 spec
    // doesn't have a way to send a number of frame buffers the guest provides.
    // Once we have the way in the virtio-video protocol, we should remove this flag.
    set_output_buffer_count: bool,

    // Reserves output resource that will be used to notify EOS.
    // This resource must not be enqueued to Chrome.
    keep_notification_output_buffer: Option<OutputResourceId>,
}

impl Context {
    fn new(stream_id: StreamId, format: Format) -> Self {
        Context {
            stream_id,
            in_params: Params {
                format: Some(format),
                min_buffers: 2,
                max_buffers: 32,
                plane_formats: vec![Default::default()],
                ..Default::default()
            },
            out_params: Default::default(),
            set_output_buffer_count: false,
            ..Default::default()
        }
    }

    fn get_resource_info(
        &self,
        res_bridge: &ResourceRequestSocket,
        resource_id: u32,
    ) -> VideoResult<ResourceInfo> {
        let handle = self.res_id_to_res_handle.get(&resource_id).copied().ok_or(
            VideoError::InvalidResourceId {
                stream_id: self.stream_id,
                resource_id,
            },
        )?;
        resource_bridge::get_resource_info(res_bridge, handle)
            .map_err(VideoError::ResourceBridgeFailure)
    }

    fn register_buffer(&mut self, resource_id: u32, uuid: &u128) {
        // TODO(stevensd): `Virtio3DBackend::resource_assign_uuid` is currently implemented to use
        // 32-bits resource_handles as UUIDs. Once it starts using real UUIDs, we need to update
        // this conversion.
        let handle = TryInto::<u32>::try_into(*uuid).expect("uuid is larger than 32 bits");
        self.res_id_to_res_handle.insert(resource_id, handle);
    }

    fn register_queued_frame_buffer(&mut self, resource_id: OutputResourceId) -> FrameBufferId {
        // Generate a new FrameBufferId
        let id = self.res_id_to_frame_buf_id.len() as FrameBufferId;
        self.res_id_to_frame_buf_id.insert(resource_id, id);
        self.frame_buf_id_to_res_id.insert(id, resource_id);
        id
    }

    fn reset(&mut self) {
        // Reset `Context` except parameters.
        *self = Context {
            stream_id: self.stream_id,
            in_params: self.in_params.clone(),
            out_params: self.out_params.clone(),
            ..Default::default()
        }
    }

    /*
     * Functions handling libvda events.
     */

    fn handle_provide_picture_buffers(
        &mut self,
        min_num_buffers: u32,
        width: i32,
        height: i32,
        visible_rect_left: i32,
        visible_rect_top: i32,
        visible_rect_right: i32,
        visible_rect_bottom: i32,
    ) {
        // We only support NV12.
        let format = Some(Format::NV12);

        let rect_width: u32 = (visible_rect_right - visible_rect_left) as u32;
        let rect_height: u32 = (visible_rect_bottom - visible_rect_top) as u32;

        let plane_size = rect_width * rect_height;
        let stride = rect_width;
        let plane_formats = vec![
            PlaneFormat { plane_size, stride },
            PlaneFormat { plane_size, stride },
        ];

        self.out_params = Params {
            format,
            // Note that rect_width is sometimes smaller.
            frame_width: width as u32,
            frame_height: height as u32,
            // Adding 1 to `min_buffers` to reserve a resource for `keep_notification_output_buffer`.
            min_buffers: min_num_buffers + 1,
            max_buffers: 32,
            crop: Crop {
                left: visible_rect_left as u32,
                top: visible_rect_top as u32,
                width: rect_width,
                height: rect_height,
            },
            plane_formats,
            // No need to set `frame_rate`, as it's only for the encoder.
            ..Default::default()
        };
    }

    fn handle_picture_ready(
        &mut self,
        buffer_id: FrameBufferId,
        left: i32,
        top: i32,
        right: i32,
        bottom: i32,
    ) -> Option<ResourceId> {
        let plane_size = ((right - left) * (bottom - top)) as u32;
        for fmt in self.out_params.plane_formats.iter_mut() {
            fmt.plane_size = plane_size;
            // We don't need to set `plane_formats[i].stride` for the decoder.
        }

        let resource_id: OutputResourceId = match self.frame_buf_id_to_res_id.get(&buffer_id) {
            Some(id) => *id,
            None => {
                error!(
                    "unknown frame buffer id {} for stream {:?}",
                    buffer_id, self.stream_id
                );
                return None;
            }
        };

        Some(resource_id)
    }

    fn handle_notify_end_of_bitstream_buffer(&mut self, bitstream_id: i32) -> Option<ResourceId> {
        // `bitstream_id` in libvda is a timestamp passed via RESOURCE_QUEUE for the input buffer
        // in second.
        let timestamp: u64 = (bitstream_id as u64) * 1_000_000_000;
        self.timestamp_to_input_res_id
            .remove(&(timestamp as u64))
            .or_else(|| {
                error!("failed to remove a timestamp {}", timestamp);
                None
            })
    }

    fn handle_reset_response(&mut self) -> Option<QueueType> {
        if self.pending_clear_cmds.input {
            self.pending_clear_cmds.input = false;
            Some(QueueType::Input)
        } else if self.pending_clear_cmds.output {
            self.pending_clear_cmds.output = false;

            Some(QueueType::Output)
        } else {
            error!("unexpected ResetResponse");
            None
        }
    }
}

/// A thin wrapper of a map of contexts with error handlings.
#[derive(Default)]
struct ContextMap {
    map: BTreeMap<StreamId, Context>,
}

impl ContextMap {
    fn insert(&mut self, ctx: Context) -> VideoResult<()> {
        match self.map.entry(ctx.stream_id) {
            Entry::Vacant(e) => {
                e.insert(ctx);
                Ok(())
            }
            Entry::Occupied(_) => {
                error!("session {} already exists", ctx.stream_id);
                Err(VideoError::InvalidStreamId(ctx.stream_id))
            }
        }
    }

    fn get(&self, stream_id: &StreamId) -> VideoResult<&Context> {
        self.map.get(&stream_id).ok_or_else(|| {
            error!("failed to get context of stream {}", *stream_id);
            VideoError::InvalidStreamId(*stream_id)
        })
    }

    fn get_mut(&mut self, stream_id: &StreamId) -> VideoResult<&mut Context> {
        self.map.get_mut(&stream_id).ok_or_else(|| {
            error!("failed to get context of stream {}", *stream_id);
            VideoError::InvalidStreamId(*stream_id)
        })
    }
}

/// A thin wrapper of a map of libvda sesssions with error handlings.
#[derive(Default)]
struct SessionMap<'a> {
    map: BTreeMap<u32, libvda::Session<'a>>,
}

impl<'a> SessionMap<'a> {
    fn contains_key(&self, stream_id: StreamId) -> bool {
        self.map.contains_key(&stream_id)
    }

    fn get(&self, stream_id: &StreamId) -> VideoResult<&libvda::Session<'a>> {
        self.map.get(&stream_id).ok_or_else(|| {
            error!("failed to get libvda session {}", *stream_id);
            VideoError::InvalidStreamId(*stream_id)
        })
    }

    fn get_mut(&mut self, stream_id: &StreamId) -> VideoResult<&mut libvda::Session<'a>> {
        self.map.get_mut(&stream_id).ok_or_else(|| {
            error!("failed to get libvda session {}", *stream_id);
            VideoError::InvalidStreamId(*stream_id)
        })
    }

    fn insert(
        &mut self,
        stream_id: StreamId,
        session: libvda::Session<'a>,
    ) -> Option<libvda::Session<'a>> {
        self.map.insert(stream_id, session)
    }
}

/// Represents information of a decoder backed with `libvda`.
pub struct Decoder<'a> {
    vda: &'a libvda::VdaInstance,
    capability: Capability,
    contexts: ContextMap,
    sessions: SessionMap<'a>,
}

impl<'a> Decoder<'a> {
    pub fn new(vda: &'a libvda::VdaInstance) -> Self {
        let capability = Capability::new(vda.get_capabilities());
        Decoder {
            vda,
            capability,
            contexts: Default::default(),
            sessions: Default::default(),
        }
    }

    /*
     * Functions processing virtio-video commands.
     */

    fn query_capabilities(&self, queue_type: QueueType) -> CmdResponse {
        let descs = match queue_type {
            QueueType::Input => self.capability.in_fmts.clone(),
            QueueType::Output => self.capability.out_fmts.clone(),
        };

        CmdResponse::QueryCapability(descs)
    }

    fn create_stream(&mut self, stream_id: StreamId, coded_format: Format) -> VideoResult<()> {
        // Create an instance of `Context`.
        // Note that `libvda::Session` will be created not here but at the first call of
        // `ResourceCreate`. This is because we need to fix a coded format for it, which
        // will be set by `SetParams`.
        self.contexts.insert(Context::new(stream_id, coded_format))
    }

    fn destroy_stream(&mut self, stream_id: StreamId) {
        if self.contexts.map.remove(&stream_id).is_none() {
            error!("Tried to destroy an invalid stream context {}", stream_id);
        }

        // Close a libVDA session, as closing will be done in `Drop` for `session`.
        // Note that `sessions` doesn't have an instance for `stream_id` if the
        // first `ResourceCreate` haven't been called yet.
        self.sessions.map.remove(&stream_id);
    }

    fn create_resource(
        &mut self,
        poll_ctx: &PollContext<Token>,
        stream_id: StreamId,
        queue_type: QueueType,
        resource_id: ResourceId,
        uuid: u128,
    ) -> VideoResult<()> {
        // Create a instance of `libvda::Session` at the first time `ResourceCreate` is
        // called here.
        if !self.sessions.contains_key(stream_id) {
            let ctx = self.contexts.get(&stream_id)?;
            let profile = match ctx.in_params.format {
                Some(Format::VP8) => Ok(libvda::Profile::VP8),
                Some(Format::VP9) => Ok(libvda::Profile::VP9Profile0),
                Some(Format::H264) => Ok(libvda::Profile::H264),
                Some(f) => {
                    error!("specified format is invalid for bitstream: {:?}", f);
                    Err(VideoError::InvalidParameter)
                }
                None => {
                    error!("bitstream format is not specified");
                    Err(VideoError::InvalidParameter)
                }
            }?;

            let session = self.vda.open_session(profile).map_err(|e| {
                error!(
                    "failed to open a session {} for {:?}: {}",
                    stream_id, profile, e
                );
                VideoError::InvalidOperation
            })?;

            poll_ctx
                .add(session.pipe(), Token::EventFd { id: stream_id })
                .map_err(|e| {
                    error!(
                        "failed to add FD to poll context for session {}: {}",
                        stream_id, e
                    );
                    VideoError::InvalidOperation
                })?;

            self.sessions.insert(stream_id, session);
        }

        self.contexts
            .get_mut(&stream_id)?
            .register_buffer(resource_id, &uuid);

        if queue_type == QueueType::Input {
            return Ok(());
        };

        // Set output_buffer_count when ResourceCreate is called for frame buffers for the
        // first time.
        let mut ctx = self.contexts.get_mut(&stream_id)?;
        if !ctx.set_output_buffer_count {
            const OUTPUT_BUFFER_COUNT: usize = 32;

            // Set the buffer count to the maximum value.
            // TODO(b/1518105): This is a hack due to the lack of way of telling a number of
            // frame buffers explictly in virtio-video v3 RFC. Once we have the way,
            // set_output_buffer_count should be called with a value passed by the guest.
            self.sessions
                .get(&stream_id)?
                .set_output_buffer_count(OUTPUT_BUFFER_COUNT)
                .map_err(VideoError::VdaError)?;
            ctx.set_output_buffer_count = true;
        }

        // We assume ResourceCreate is not called to an output resource that is already
        // imported to Chrome for now.
        // TODO(keiichiw): We need to support this case for a guest client who may use
        // arbitrary numbers of buffers. (e.g. C2V4L2Component in ARCVM)
        // Such a client is valid as long as it uses at most 32 buffers at the same time.
        if let Some(frame_buf_id) = ctx.res_id_to_frame_buf_id.get(&resource_id) {
            error!(
                "resource {} has already been imported to Chrome as a frame buffer {}",
                resource_id, frame_buf_id
            );
            return Err(VideoError::InvalidOperation);
        }

        Ok(())
    }

    fn destroy_all_resources(&mut self, stream_id: StreamId) -> VideoResult<()> {
        // Reset the associated context.
        self.contexts.get_mut(&stream_id)?.reset();
        Ok(())
    }

    fn queue_input_resource(
        &mut self,
        resource_bridge: &ResourceRequestSocket,
        stream_id: StreamId,
        resource_id: ResourceId,
        timestamp: u64,
        data_sizes: Vec<u32>,
    ) -> VideoResult<()> {
        let session = self.sessions.get(&stream_id)?;

        if data_sizes.len() != 1 {
            error!("num_data_sizes must be 1 but {}", data_sizes.len());
            return Err(VideoError::InvalidOperation);
        }

        // Take an ownership of this file by `into_raw_fd()` as this file will be closed by libvda.
        let fd = self
            .contexts
            .get_mut(&stream_id)?
            .get_resource_info(resource_bridge, resource_id)?
            .file
            .into_raw_fd();

        // Register  a mapping of timestamp to resource_id
        self.contexts
            .get_mut(&stream_id)?
            .timestamp_to_input_res_id
            .insert(timestamp, resource_id);

        // While the virtio-video driver handles timestamps as nanoseconds,
        // Chrome assumes per-second timestamps coming. So, we need a conversion from nsec
        // to sec.
        // Note that this value should not be an unix time stamp but a frame number that
        // a guest passes to a driver as a 32-bit integer in our implementation.
        // So, overflow must not happen in this conversion.
        let ts_sec: i32 = (timestamp / 1_000_000_000) as i32;
        session
            .decode(
                ts_sec,
                fd,            // fd
                0,             // offset is always 0 due to the driver implementation.
                data_sizes[0], // bytes_used
            )
            .map_err(VideoError::VdaError)?;

        Ok(())
    }

    fn queue_output_resource(
        &mut self,
        resource_bridge: &ResourceRequestSocket,
        stream_id: StreamId,
        resource_id: ResourceId,
    ) -> VideoResult<()> {
        let session = self.sessions.get(&stream_id)?;
        let ctx = self.contexts.get_mut(&stream_id)?;

        // Check if the current pixel format is set to NV12.
        match ctx.out_params.format {
            Some(Format::NV12) => (), // OK
            Some(f) => {
                error!(
                    "video decoder only supports NV12 as a frame format but {:?}",
                    f
                );
                return Err(VideoError::InvalidOperation);
            }
            None => {
                error!("output format is not set");
                return Err(VideoError::InvalidOperation);
            }
        };

        if ctx.keep_notification_output_buffer.is_none() {
            // Stores an output buffer to notify EOS.
            ctx.keep_notification_output_buffer = Some(resource_id);

            // Don't enqueue this resource to Chrome.
            return Ok(());
        }

        // In case a given resource has been imported to VDA, call
        // `session.reuse_output_buffer()` and return a response.
        // Otherwise, `session.use_output_buffer()` will be called below.
        match ctx.res_id_to_frame_buf_id.get(&resource_id) {
            Some(buffer_id) => {
                session
                    .reuse_output_buffer(*buffer_id)
                    .map_err(VideoError::VdaError)?;
                return Ok(());
            }
            None => (),
        };

        let resource_info = ctx.get_resource_info(resource_bridge, resource_id)?;
        let fd = resource_info.file.as_raw_fd();

        // Take an ownership of `resource_info.file`.
        // This file will be kept until the stream is destroyed.
        self.contexts
            .get_mut(&stream_id)?
            .keep_resources
            .push(resource_info.file);

        let planes = vec![
            libvda::FramePlane {
                offset: resource_info.planes[0].offset as i32,
                stride: resource_info.planes[0].stride as i32,
            },
            libvda::FramePlane {
                offset: resource_info.planes[1].offset as i32,
                stride: resource_info.planes[1].stride as i32,
            },
        ];

        let buffer_id = self
            .contexts
            .get_mut(&stream_id)?
            .register_queued_frame_buffer(resource_id);

        session
            .use_output_buffer(buffer_id as i32, libvda::PixelFormat::NV12, fd, &planes)
            .map_err(VideoError::VdaError)?;

        Ok(())
    }

    fn get_params(&self, stream_id: StreamId, queue_type: QueueType) -> VideoResult<Params> {
        let ctx = self.contexts.get(&stream_id)?;
        Ok(match queue_type {
            QueueType::Input => ctx.in_params.clone(),
            QueueType::Output => ctx.out_params.clone(),
        })
    }

    fn set_params(
        &mut self,
        stream_id: StreamId,
        queue_type: QueueType,
        params: Params,
    ) -> VideoResult<()> {
        let ctx = self.contexts.get_mut(&stream_id)?;
        match queue_type {
            QueueType::Input => {
                if self.sessions.contains_key(stream_id) {
                    error!("parameter for input cannot be changed once decoding started");
                    return Err(VideoError::InvalidParameter);
                }

                // Only a few parameters can be changed by the guest.
                ctx.in_params.format = params.format;
                ctx.in_params.plane_formats = params.plane_formats.clone();
            }
            QueueType::Output => {
                // The guest cannot update parameters for output queue in the decoder.
            }
        };
        Ok(())
    }

    fn query_control(&self, ctrl_type: QueryCtrlType) -> VideoResult<QueryCtrlResponse> {
        self.capability.query_control(&ctrl_type).ok_or_else(|| {
            error!("querying an unsupported control: {:?}", ctrl_type);
            VideoError::InvalidArgument
        })
    }

    fn get_control(&self, stream_id: StreamId, ctrl_type: CtrlType) -> VideoResult<CtrlVal> {
        let ctx = self.contexts.get(&stream_id)?;
        match ctrl_type {
            CtrlType::Profile => {
                let profile = match ctx.in_params.format {
                    Some(Format::VP8) => Profile::VP8Profile0,
                    Some(Format::VP9) => Profile::VP9Profile0,
                    Some(Format::H264) => Profile::H264Baseline,
                    Some(f) => {
                        error!("specified format is invalid: {:?}", f);
                        return Err(VideoError::InvalidArgument);
                    }
                    None => {
                        error!("bitstream format is not set");
                        return Err(VideoError::InvalidArgument);
                    }
                };

                Ok(CtrlVal::Profile(profile))
            }
            CtrlType::Level => {
                let level = match ctx.in_params.format {
                    Some(Format::H264) => Level::H264_1_0,
                    Some(f) => {
                        error!("specified format has no level: {:?}", f);
                        return Err(VideoError::InvalidArgument);
                    }
                    None => {
                        error!("bitstream format is not set");
                        return Err(VideoError::InvalidArgument);
                    }
                };

                Ok(CtrlVal::Level(level))
            }
            t => {
                error!("cannot get a control value: {:?}", t);
                Err(VideoError::InvalidArgument)
            }
        }
    }

    fn drain_stream(&mut self, stream_id: StreamId) -> VideoResult<()> {
        self.sessions
            .get(&stream_id)?
            .flush()
            .map_err(VideoError::VdaError)?;
        Ok(())
    }

    fn clear_queue(&mut self, stream_id: StreamId, queue_type: QueueType) -> VideoResult<()> {
        let ctx = self.contexts.get_mut(&stream_id)?;
        let session = self.sessions.get(&stream_id)?;

        let pending_clear_cmd = match queue_type {
            QueueType::Input => &mut ctx.pending_clear_cmds.input,
            QueueType::Output => &mut ctx.pending_clear_cmds.output,
        };

        if *pending_clear_cmd {
            error!("Clear command is already in process");
            return Err(VideoError::InvalidOperation);
        }

        // TODO(b/153406792): Though QUEUE_CLEAR is defined as a per-queue command in the
        // specification, Chrome VDA's `reset()` clears both input and output buffers.
        // So, this code can be a problem when a guest application wants to reset only one
        // queue by REQBUFS(0).
        // To handle this problem, we need to
        // (i) update libvda interface or,
        // (ii) re-enqueue discarded requests that were pending for the other direction.
        session.reset().map_err(VideoError::VdaError)?;

        *pending_clear_cmd = true;

        Ok(())
    }
}

impl<'a> Device for Decoder<'a> {
    fn process_cmd(
        &mut self,
        cmd: VideoCmd,
        poll_ctx: &PollContext<Token>,
        resource_bridge: &ResourceRequestSocket,
    ) -> VideoResult<VideoCmdResponseType> {
        use VideoCmd::*;
        use VideoCmdResponseType::{Async, Sync};

        match cmd {
            QueryCapability { queue_type } => Ok(Sync(self.query_capabilities(queue_type))),
            StreamCreate {
                stream_id,
                coded_format,
            } => {
                self.create_stream(stream_id, coded_format)?;
                Ok(Sync(CmdResponse::NoData))
            }
            StreamDestroy { stream_id } => {
                self.destroy_stream(stream_id);
                Ok(Sync(CmdResponse::NoData))
            }
            ResourceCreate {
                stream_id,
                queue_type,
                resource_id,
                uuid,
                // ignore `plane_offsets` as we use `resource_info` given by `resource_bridge` instead.
                ..
            } => {
                self.create_resource(poll_ctx, stream_id, queue_type, resource_id, uuid)?;
                Ok(Sync(CmdResponse::NoData))
            }
            ResourceDestroyAll { stream_id } => {
                self.destroy_all_resources(stream_id)?;
                Ok(Sync(CmdResponse::NoData))
            }
            ResourceQueue {
                stream_id,
                queue_type: QueueType::Input,
                resource_id,
                timestamp,
                data_sizes,
            } => {
                self.queue_input_resource(
                    resource_bridge,
                    stream_id,
                    resource_id,
                    timestamp,
                    data_sizes,
                )?;
                Ok(Async(AsyncCmdTag::Queue {
                    stream_id,
                    queue_type: QueueType::Input,
                    resource_id,
                }))
            }
            ResourceQueue {
                stream_id,
                queue_type: QueueType::Output,
                resource_id,
                ..
            } => {
                self.queue_output_resource(resource_bridge, stream_id, resource_id)?;
                Ok(Async(AsyncCmdTag::Queue {
                    stream_id,
                    queue_type: QueueType::Output,
                    resource_id,
                }))
            }
            GetParams {
                stream_id,
                queue_type,
            } => {
                let params = self.get_params(stream_id, queue_type)?;
                Ok(Sync(CmdResponse::GetParams { queue_type, params }))
            }
            SetParams {
                stream_id,
                queue_type,
                params,
            } => {
                self.set_params(stream_id, queue_type, params)?;
                Ok(Sync(CmdResponse::NoData))
            }
            QueryControl { query_ctrl_type } => {
                let resp = self.query_control(query_ctrl_type)?;
                Ok(Sync(CmdResponse::QueryControl(resp)))
            }
            GetControl {
                stream_id,
                ctrl_type,
            } => {
                let ctrl_val = self.get_control(stream_id, ctrl_type)?;
                Ok(Sync(CmdResponse::GetControl(ctrl_val)))
            }
            SetControl { .. } => {
                error!("SET_CONTROL is not allowed for decoder");
                Err(VideoError::InvalidOperation)
            }
            StreamDrain { stream_id } => {
                self.drain_stream(stream_id)?;
                Ok(Async(AsyncCmdTag::Drain { stream_id }))
            }
            QueueClear {
                stream_id,
                queue_type,
            } => {
                self.clear_queue(stream_id, queue_type)?;
                Ok(Async(AsyncCmdTag::Clear {
                    stream_id,
                    queue_type,
                }))
            }
        }
    }

    fn process_event_fd(&mut self, stream_id: u32) -> Option<VideoEvtResponseType> {
        use crate::virtio::video::device::VideoEvtResponseType::*;
        use libvda::Event::*;

        let session = match self.sessions.get_mut(&stream_id) {
            Ok(s) => s,
            Err(_) => {
                error!("an event notified for an unknown session {}", stream_id);
                return None;
            }
        };

        let event = match session.read_event() {
            Ok(event) => event,
            Err(e) => {
                error!("failed to read an event from session {}: {}", stream_id, e);
                return None;
            }
        };

        let ctx = match self.contexts.get_mut(&stream_id) {
            Ok(ctx) => ctx,
            Err(_) => {
                error!(
                    "failed to get a context for session {}: {:?}",
                    stream_id, event
                );
                return None;
            }
        };

        match event {
            ProvidePictureBuffers {
                min_num_buffers,
                width,
                height,
                visible_rect_left,
                visible_rect_top,
                visible_rect_right,
                visible_rect_bottom,
            } => {
                ctx.handle_provide_picture_buffers(
                    min_num_buffers,
                    width,
                    height,
                    visible_rect_left,
                    visible_rect_top,
                    visible_rect_right,
                    visible_rect_bottom,
                );
                Some(Event(VideoEvt {
                    typ: EvtType::DecResChanged,
                    stream_id,
                }))
            }
            PictureReady {
                buffer_id, // FrameBufferId
                bitstream_id: ts_sec,
                left,
                top,
                right,
                bottom,
            } => {
                let resource_id = ctx.handle_picture_ready(buffer_id, left, top, right, bottom)?;
                Some(AsyncCmd {
                    tag: AsyncCmdTag::Queue {
                        stream_id,
                        queue_type: QueueType::Output,
                        resource_id,
                    },
                    resp: Ok(CmdResponse::ResourceQueue {
                        // Conversion from sec to nsec.
                        timestamp: (ts_sec as u64) * 1_000_000_000,
                        // TODO(b/149725148): Set buffer flags once libvda exposes them.
                        flags: 0,
                        // `size` is only used for the encoder.
                        size: 0,
                    }),
                })
            }
            NotifyEndOfBitstreamBuffer { bitstream_id } => {
                let resource_id = ctx.handle_notify_end_of_bitstream_buffer(bitstream_id)?;
                Some(AsyncCmd {
                    tag: AsyncCmdTag::Queue {
                        stream_id,
                        queue_type: QueueType::Input,
                        resource_id,
                    },
                    resp: Ok(CmdResponse::ResourceQueue {
                        timestamp: 0, // ignored for bitstream buffers.
                        flags: 0,     // no flag is raised, as it's returned successfully.
                        size: 0,      // this field is only for encoder
                    }),
                })
            }
            FlushResponse(resp) => {
                let tag = AsyncCmdTag::Drain { stream_id };
                match resp {
                    libvda::Response::Success => Some(AsyncCmd {
                        tag,
                        resp: Ok(CmdResponse::NoData),
                    }),
                    _ => {
                        // TODO(b/151810591): If `resp` is `libvda::Response::Canceled`,
                        // we should notify it to the driver in some way.
                        error!("failed to 'Flush' in VDA: {:?}", resp);
                        Some(AsyncCmd {
                            tag,
                            resp: Err(VideoError::VdaFailure(resp)),
                        })
                    }
                }
            }
            ResetResponse(resp) => {
                let tag = AsyncCmdTag::Clear {
                    stream_id,
                    queue_type: ctx.handle_reset_response()?,
                };
                match resp {
                    libvda::Response::Success => Some(AsyncCmd {
                        tag,
                        resp: Ok(CmdResponse::NoData),
                    }),
                    _ => {
                        error!("failed to 'Reset' in VDA: {:?}", resp);
                        Some(AsyncCmd {
                            tag,
                            resp: Err(VideoError::VdaFailure(resp)),
                        })
                    }
                }
            }
            NotifyError(resp) => {
                error!("an error is notified by VDA: {}", resp);
                Some(Event(VideoEvt {
                    typ: EvtType::Error,
                    stream_id,
                }))
            }
        }
    }

    fn take_resource_id_to_notify_eos(&mut self, stream_id: u32) -> Option<u32> {
        self.contexts
            .get_mut(&stream_id)
            .ok()
            .and_then(|s| s.keep_notification_output_buffer.take())
    }
}