Skip to content

Commit

Permalink
Merge pull request #511 from shiguredo/feature/mp4-media-stream-h265
Browse files Browse the repository at this point in the history
`Mp4MediaStream` の対応コーデックに H.265 を追加する
  • Loading branch information
sile authored Dec 6, 2024
2 parents 471a422 + e046102 commit e4b840a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

## develop

- [ADD] `Mp4MediaStream` の対応コーデックに H.265 を追加する
- @sile
- [ADD] `Mp4MediaStream` の対応コーデックに AV1 を追加する
- @sile
- [ADD] `Mp4MediaStream` の対応コーデックに VP9 を追加する
Expand Down
1 change: 1 addition & 0 deletions packages/mp4-media-stream/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ video.srcObject = stream

- 映像:
- H.264
- H.265
- VP8
- VP9
- AV1
Expand Down
57 changes: 55 additions & 2 deletions packages/mp4-media-stream/wasm/src/mp4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use serde::Serialize;
use shiguredo_mp4::{
aux::SampleTableAccessor,
boxes::{
Av01Box, Avc1Box, FtypBox, HdlrBox, IgnoredBox, MoovBox, Mp4aBox, OpusBox, SampleEntry,
StblBox, TrakBox, Vp08Box, Vp09Box,
Av01Box, Avc1Box, FtypBox, HdlrBox, Hev1Box, IgnoredBox, MoovBox, Mp4aBox, OpusBox,
SampleEntry, StblBox, TrakBox, Vp08Box, Vp09Box,
},
BaseBox, Decode, Either, Encode,
};
Expand Down Expand Up @@ -39,6 +39,55 @@ impl VideoDecoderConfig {
}
}

pub fn from_hev1_box(b: &Hev1Box) -> Self {
let mut description = Vec::new();
b.hvcc_box.encode(&mut description).expect("unreachable");
description.drain(..8); // ボックスヘッダ部分を取り除く

let mut constraints = b
.hvcc_box
.general_constraint_indicator_flags
.get()
.to_be_bytes()
.to_vec();
while constraints.len() > 1 && constraints.last() == Some(&0) {
constraints.pop();
}

Self {
// ISO / IEC 14496-15 E.3
codec: format!(
"hev1.{}.{:X}.{}.{}",
match b.hvcc_box.general_profile_space.get() {
1 => format!("A{}", b.hvcc_box.general_profile_idc.get()),
2 => format!("B{}", b.hvcc_box.general_profile_idc.get()),
3 => format!("C{}", b.hvcc_box.general_profile_idc.get()),
v => format!("{v}"),
},
b.hvcc_box
.general_profile_compatibility_flags
.reverse_bits(),
format!(
"{}{}",
if b.hvcc_box.general_tier_flag.get() == 0 {
'L'
} else {
'H'
},
b.hvcc_box.general_level_idc
),
constraints
.into_iter()
.map(|b| format!("{:02X}", b))
.collect::<Vec<_>>()
.join(".")
),
description,
coded_width: b.visual.width,
coded_height: b.visual.height,
}
}

pub fn from_vp08_box(b: &Vp08Box) -> Self {
Self {
codec: "vp8".to_owned(),
Expand Down Expand Up @@ -163,6 +212,7 @@ impl Track {

match sample_table.stbl_box().stsd_box.entries.first() {
Some(SampleEntry::Avc1(_)) => (),
Some(SampleEntry::Hev1(_)) => (),
Some(SampleEntry::Vp08(_)) => (),
Some(SampleEntry::Vp09(_)) => (),
Some(SampleEntry::Av01(_)) => (),
Expand Down Expand Up @@ -256,6 +306,9 @@ impl Mp4 {
SampleEntry::Avc1(b) => {
video_configs.push(VideoDecoderConfig::from_avc1_box(b));
}
SampleEntry::Hev1(b) => {
video_configs.push(VideoDecoderConfig::from_hev1_box(b));
}
SampleEntry::Vp08(b) => {
video_configs.push(VideoDecoderConfig::from_vp08_box(b));
}
Expand Down
4 changes: 4 additions & 0 deletions packages/mp4-media-stream/wasm/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ impl TrackPlayer {
let config = VideoDecoderConfig::from_avc1_box(b);
WasmApi::create_video_decoder(self.player_id, config).await
}
SampleEntry::Hev1(b) => {
let config = VideoDecoderConfig::from_hev1_box(b);
WasmApi::create_video_decoder(self.player_id, config).await
}
SampleEntry::Vp08(b) => {
let config = VideoDecoderConfig::from_vp08_box(b);
WasmApi::create_video_decoder(self.player_id, config).await
Expand Down

0 comments on commit e4b840a

Please sign in to comment.