1use libde265::{De265, Decoder};
2
3const NAL_MIN_0_COUNT: usize = 2;
4#[inline]
5fn nth_nal_index(stream: &[u8], nth: usize) -> Option<usize> {
6 let mut count_0 = 0;
7 let mut n = 0;
8
9 for (i, byte) in stream.iter().enumerate() {
10 match byte {
11 0 => count_0 += 1,
12 1 if count_0 >= NAL_MIN_0_COUNT => {
13 if n == nth {
14 return Some(i - NAL_MIN_0_COUNT);
15 } else {
16 count_0 = 0;
17 n += 1;
18 }
19 }
20 _ => count_0 = 0,
21 }
22 }
23
24 None
25}
26
27pub fn nal_units(mut stream: &[u8]) -> impl Iterator<Item = &[u8]> {
28 std::iter::from_fn(move || {
29 let first = nth_nal_index(stream, 0);
30 let next = nth_nal_index(stream, 1);
31
32 match (first, next) {
33 (Some(f), Some(n)) => {
34 let rval = &stream[f..n];
35 stream = &stream[n..];
36 Some(rval)
37 }
38 (Some(f), None) => {
39 let rval = &stream[f..];
40 stream = &stream[f + NAL_MIN_0_COUNT..];
41 Some(rval)
42 }
43 _ => None,
44 }
45 })
46}
47
48fn main() {
49 let de = De265::new().unwrap();
50 println!("libde265 {}", de.get_version());
51
52 let mut decoder = Decoder::new(de.clone());
53 decoder.start_worker_threads(16).unwrap();
54
55 let h264_in = std::fs::read("/home/andrey/demo/h265/big_buck_bunney.h265").unwrap();
56
57 for packet in nal_units(&h264_in) {
58 decoder.push_data(packet, 0, None).unwrap();
59 decoder.decode().unwrap();
60
61 if let Some(img) = decoder.get_next_picture() {
62 let y = img.get_image_plane(0);
63 let u = img.get_image_plane(1);
64 let v = img.get_image_plane(2);
65
66 println!(
67 "pic {}x{} {:?} {:?} {:?} {:?} {:?} {:?} {:?}",
68 img.get_image_width(0),
69 img.get_image_height(0),
70 img.get_chroma_format(),
71 &y.0[0..20],
72 y.1,
73 &u.0[0..20],
74 u.1,
75 &v.0[0..20],
76 v.1
77 );
78 }
79 }
80}