-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev v4l2 stateless #83
Open
semigle
wants to merge
7
commits into
chromeos:main
Choose a base branch
from
semigle:dev-v4l2_stateless
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8d99b44
codec/h264: Add missing V4l2 fields to SliceHeader
semigle 7ca7c7d
device/v4l2: Add v4l2 stateless device abstraction
semigle 52f81bd
backend/v4l2: Add v4l2 stateless decoder backend
semigle 2a66965
decoder/h264: Add support for v4l2 backend
semigle 07dfccd
examples/ccdec: Add example for v4l2 backend
semigle 1af2ce1
decoder/stateless/v4l2/h264: remove unneeded `V4l2CtrlH264Sps` and `V…
Gnurou f51face
decoder/stateless/v4l2: remove RefCell from V4l2Request
Gnurou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
// Copyright 2024 The ChromiumOS Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
use std::borrow::Cow; | ||
use std::fs::File; | ||
use std::io::Read; | ||
use std::io::Write; | ||
use std::path::PathBuf; | ||
use std::str::FromStr; | ||
|
||
use argh::FromArgs; | ||
use cros_codecs::backend::v4l2::decoder::stateless::V4l2StatelessDecoderHandle; | ||
use cros_codecs::codec::h264::parser::Nalu as H264Nalu; | ||
use cros_codecs::decoder::stateless::h264::H264; | ||
use cros_codecs::decoder::stateless::StatelessDecoder; | ||
use cros_codecs::decoder::stateless::StatelessVideoDecoder; | ||
use cros_codecs::decoder::BlockingMode; | ||
use cros_codecs::decoder::DecodedHandle; | ||
use cros_codecs::multiple_desc_type; | ||
use cros_codecs::utils::simple_playback_loop; | ||
use cros_codecs::utils::simple_playback_loop_owned_frames; | ||
use cros_codecs::utils::DmabufFrame; | ||
use cros_codecs::utils::NalIterator; | ||
use cros_codecs::utils::UserPtrFrame; | ||
use cros_codecs::DecodedFormat; | ||
|
||
multiple_desc_type! { | ||
enum BufferDescriptor { | ||
Managed(()), | ||
Dmabuf(DmabufFrame), | ||
User(UserPtrFrame), | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
enum EncodedFormat { | ||
H264, | ||
H265, | ||
VP8, | ||
VP9, | ||
AV1, | ||
} | ||
|
||
impl FromStr for EncodedFormat { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"h264" | "H264" => Ok(EncodedFormat::H264), | ||
"h265" | "H265" => Ok(EncodedFormat::H265), | ||
"vp8" | "VP8" => Ok(EncodedFormat::VP8), | ||
"vp9" | "VP9" => Ok(EncodedFormat::VP9), | ||
"av1" | "AV1" => Ok(EncodedFormat::AV1), | ||
_ => Err("unrecognized input format. Valid values: h264, h265, vp8, vp9, av1"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Copy, Clone)] | ||
enum FrameMemoryType { | ||
Managed, | ||
Prime, | ||
User, | ||
} | ||
|
||
impl FromStr for FrameMemoryType { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"managed" => Ok(FrameMemoryType::Managed), | ||
"prime" => Ok(FrameMemoryType::Prime), | ||
"user" => Ok(FrameMemoryType::User), | ||
_ => Err("unrecognized memory type. Valid values: managed, prime, user"), | ||
} | ||
} | ||
} | ||
|
||
/// Simple player using cros-codecs | ||
#[derive(Debug, FromArgs)] | ||
struct Args { | ||
/// input file | ||
#[argh(positional)] | ||
input: PathBuf, | ||
|
||
/// output file to write the decoded frames to | ||
#[argh(option)] | ||
output: Option<PathBuf>, | ||
|
||
/// input format to decode from. | ||
#[argh(option)] | ||
input_format: EncodedFormat, | ||
|
||
//TODO /// pixel format to decode into. Default: i420 | ||
//TODO #[argh(option, default = "DecodedFormat::I420")] | ||
//TODO output_format: DecodedFormat, | ||
/// origin of the memory for decoded buffers (managed, prime or user). Default: managed. | ||
#[argh(option, default = "FrameMemoryType::Managed")] | ||
frame_memory: FrameMemoryType, | ||
|
||
//TODO /// path to the GBM device to use if frame-memory=prime | ||
//TODO #[argh(option)] | ||
//TODO gbm_device: Option<PathBuf>, | ||
/// whether to decode frames synchronously | ||
#[argh(switch)] | ||
synchronous: bool, | ||
//TODO /// whether to display the MD5 of the decoded stream, and at which granularity (stream or | ||
//TODO /// frame) | ||
//TODO #[argh(option)] | ||
//TODO compute_md5: Option<Md5Computation>, | ||
} | ||
|
||
fn main() { | ||
env_logger::init(); | ||
|
||
let args: Args = argh::from_env(); | ||
|
||
let input = { | ||
let mut buf = Vec::new(); | ||
File::open(args.input) | ||
.expect("error opening input file") | ||
.read_to_end(&mut buf) | ||
.expect("error reading input file"); | ||
buf | ||
}; | ||
|
||
let mut output = args | ||
.output | ||
.as_ref() | ||
.map(|p| File::create(p).expect("Failed to create output file")); | ||
|
||
let blocking_mode = if args.synchronous { | ||
todo!() // BlockingMode::Blocking | ||
} else { | ||
BlockingMode::NonBlocking | ||
}; | ||
|
||
let (mut decoder, frame_iter) = match args.input_format { | ||
EncodedFormat::H264 => { | ||
let frame_iter = Box::new(NalIterator::<H264Nalu>::new(&input)) | ||
as Box<dyn Iterator<Item = Cow<[u8]>>>; | ||
|
||
let decoder = Box::new(StatelessDecoder::<H264, _>::new_v4l2(blocking_mode)) | ||
as Box<dyn StatelessVideoDecoder<_>>; | ||
|
||
(decoder, frame_iter) | ||
} | ||
EncodedFormat::VP8 => todo!(), | ||
EncodedFormat::VP9 => todo!(), | ||
EncodedFormat::H265 => todo!(), | ||
EncodedFormat::AV1 => todo!(), | ||
}; | ||
|
||
let mut on_new_frame = |handle: V4l2StatelessDecoderHandle| { | ||
let picture = handle.dyn_picture(); | ||
let mut handle = picture.dyn_mappable_handle().unwrap(); | ||
let buffer_size = handle.image_size(); | ||
let mut frame_data = vec![0; buffer_size]; | ||
handle.read(&mut frame_data).unwrap(); | ||
if let Some(output) = &mut output { | ||
output | ||
.write_all(&frame_data) | ||
.expect("Failed to write output file"); | ||
} | ||
}; | ||
|
||
simple_playback_loop( | ||
decoder.as_mut(), | ||
frame_iter, | ||
&mut on_new_frame, | ||
&mut |stream_info, nb_frames| { | ||
Ok(match args.frame_memory { | ||
FrameMemoryType::Managed => { | ||
simple_playback_loop_owned_frames(stream_info, nb_frames)? | ||
.into_iter() | ||
.collect() | ||
} | ||
FrameMemoryType::Prime => todo!(), | ||
FrameMemoryType::User => todo!(), | ||
}) | ||
}, | ||
DecodedFormat::NV12, | ||
blocking_mode, | ||
) | ||
.expect("error during playback loop"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2024 The ChromiumOS Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
pub mod stateless; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe we should be able to integrate this into the existing
ccdec
instead of creating a new program. I did a recent change that made the decoders codec and backend agnostic, so you should be able to plug a V4L2 stateless decoder there without any problem - please let me know if you see any blocker though.