Skip to content

Support HEVC. #410

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

Merged
merged 5 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions mp4parse/src/boxes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ box_database!(
AVCConfigurationBox 0x6176_6343, // "avcC"
H263SampleEntry 0x7332_3633, // "s263"
H263SpecificBox 0x6432_3633, // "d263"
HEV1SampleEntry 0x6865_7631, // "hev1"
HVC1SampleEntry 0x6876_6331, // "hvc1"
HEVCConfigurationBox 0x6876_6343, // "hvcC"
MP4AudioSampleEntry 0x6d70_3461, // "mp4a"
MP4VideoSampleEntry 0x6d70_3476, // "mp4v"
#[cfg(feature = "3gpp")]
Expand Down
18 changes: 18 additions & 0 deletions mp4parse/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,7 @@ pub enum VideoCodecSpecific {
AV1Config(AV1ConfigBox),
ESDSConfig(TryVec<u8>),
H263Config(TryVec<u8>),
HEVCConfig(TryVec<u8>),
}

#[derive(Debug)]
Expand Down Expand Up @@ -2060,6 +2061,7 @@ pub enum CodecType {
LPCM, // QT
ALAC,
H263,
HEVC,
#[cfg(feature = "3gpp")]
AMRNB,
#[cfg(feature = "3gpp")]
Expand Down Expand Up @@ -5456,6 +5458,7 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
BoxType::AV1SampleEntry => CodecType::AV1,
BoxType::ProtectedVisualSampleEntry => CodecType::EncryptedVideo,
BoxType::H263SampleEntry => CodecType::H263,
BoxType::HEV1SampleEntry | BoxType::HVC1SampleEntry => CodecType::HEVC,
_ => {
debug!("Unsupported video codec, box {:?} found", name);
CodecType::Unknown
Expand Down Expand Up @@ -5567,6 +5570,21 @@ fn read_video_sample_entry<T: Read>(src: &mut BMFFBox<T>) -> Result<SampleEntry>
debug!("{:?} (sinf)", sinf);
protection_info.push(sinf)?;
}
BoxType::HEVCConfigurationBox => {
if (name != BoxType::HEV1SampleEntry && name != BoxType::HVC1SampleEntry)
|| codec_specific.is_some()
{
return Status::StsdBadVideoSampleEntry.into();
}
let hvcc_size = b
.head
.size
.checked_sub(b.head.offset)
.expect("offset invalid");
let hvcc = read_buf(&mut b.content, hvcc_size)?;
debug!("{:?} (hvcc)", hvcc);
codec_specific = Some(VideoCodecSpecific::HEVCConfig(hvcc));
}
_ => {
debug!("Unsupported video codec, box {:?} found", b.head.name);
skip_box_content(&mut b)?;
Expand Down
Binary file added mp4parse/tests/hevc_white_frame.mp4
Binary file not shown.
33 changes: 33 additions & 0 deletions mp4parse/tests/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,9 @@ static AVIF_CORRUPT_IMAGES_DIR: &str = "tests/corrupt";
// The 1 frame h263 3gp file can be generated by ffmpeg with command
// "ffmpeg -i [input file] -f 3gp -vcodec h263 -vf scale=176x144 -frames:v 1 -an output.3gp"
static VIDEO_H263_3GP: &str = "tests/bbb_sunflower_QCIF_30fps_h263_noaudio_1f.3gp";
// The 1 frame hevc mp4 file generated by ffmpeg with command
// "ffmpeg -f lavfi -i color=c=white:s=640x480 -c:v libx265 -frames:v 1 -pix_fmt yuv420p hevc_white_frame.mp4"
static VIDEO_HEVC_MP4: &str = "tests/hevc_white_frame.mp4";
// The 1 frame AMR-NB 3gp file can be generated by ffmpeg with command
// "ffmpeg -i [input file] -f 3gp -acodec amr_nb -ar 8000 -ac 1 -frames:a 1 -vn output.3gp"
#[cfg(feature = "3gpp")]
Expand Down Expand Up @@ -286,6 +289,10 @@ fn public_api() {
mp4::VideoCodecSpecific::H263Config(ref _h263) => {
"H263"
}
mp4::VideoCodecSpecific::HEVCConfig(ref hevc) => {
assert!(!hevc.is_empty());
"HEVC"
}
},
"AVC"
);
Expand Down Expand Up @@ -1435,6 +1442,32 @@ fn public_video_h263() {
}
}

#[test]
fn public_video_hevc() {
let mut fd = File::open(VIDEO_HEVC_MP4).expect("Unknown file");
let mut buf = Vec::new();
fd.read_to_end(&mut buf).expect("File error");

let mut c = Cursor::new(&buf);
let context = mp4::read_mp4(&mut c).expect("read_mp4 failed");
for track in context.tracks {
let stsd = track.stsd.expect("expected an stsd");
let v = match stsd.descriptions.first().expect("expected a SampleEntry") {
mp4::SampleEntry::Video(ref v) => v,
_ => panic!("expected a VideoSampleEntry"),
};
assert_eq!(v.codec_type, mp4::CodecType::HEVC);
assert_eq!(v.width, 640);
assert_eq!(v.height, 480);
let _codec_specific = match &v.codec_specific {
mp4::VideoCodecSpecific::HEVCConfig(_) => true,
_ => {
panic!("expected a HEVCConfig",);
}
};
}
}

#[test]
#[cfg(feature = "3gpp")]
fn public_audio_amrnb() {
Expand Down
6 changes: 5 additions & 1 deletion mp4parse_capi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ pub enum Mp4parseCodec {
Ec3,
Alac,
H263,
HEVC,
#[cfg(feature = "3gpp")]
AMRNB,
#[cfg(feature = "3gpp")]
Expand Down Expand Up @@ -962,6 +963,7 @@ fn mp4parse_get_track_video_info_safe(
VideoCodecSpecific::AV1Config(_) => Mp4parseCodec::Av1,
VideoCodecSpecific::AVCConfig(_) => Mp4parseCodec::Avc,
VideoCodecSpecific::H263Config(_) => Mp4parseCodec::H263,
VideoCodecSpecific::HEVCConfig(_) => Mp4parseCodec::HEVC,
#[cfg(feature = "mp4v")]
VideoCodecSpecific::ESDSConfig(_) => Mp4parseCodec::Mp4v,
#[cfg(not(feature = "mp4v"))]
Expand All @@ -978,7 +980,9 @@ fn mp4parse_get_track_video_info_safe(
VideoCodecSpecific::AV1Config(ref config) => {
sample_info.extra_data.set_data(&config.raw_config);
}
VideoCodecSpecific::AVCConfig(ref data) | VideoCodecSpecific::ESDSConfig(ref data) => {
VideoCodecSpecific::AVCConfig(ref data)
| VideoCodecSpecific::ESDSConfig(ref data)
| VideoCodecSpecific::HEVCConfig(ref data) => {
sample_info.extra_data.set_data(data);
}
_ => {}
Expand Down