Skip to content

Commit bbb3464

Browse files
committed
Implement support for encoding HDR10+ from JSON metadata file
1 parent 90fdd3d commit bbb3464

File tree

8 files changed

+131
-10
lines changed

8 files changed

+131
-10
lines changed

Cargo.lock

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ new_debug_unreachable = "1.0.4"
109109
once_cell = "1.13.0"
110110
av1-grain = { version = "0.2.0", features = ["serialize"] }
111111
serde-big-array = { version = "0.4.1", optional = true }
112+
hdr10plus = { version = "2.0.0", features = ["json"] }
112113

113114
[dependencies.image]
114115
version = "0.24.3"

src/api/config/encoder.rs

+7
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::api::{Rational, SpeedSettings};
1515
use crate::encoder::Tune;
1616
use crate::serialize::{Deserialize, Serialize};
1717

18+
use std::collections::BTreeMap;
1819
use std::fmt;
1920

2021
// We add 1 to rdo_lookahead_frames in a bunch of places.
@@ -91,6 +92,11 @@ pub struct EncoderConfig {
9192
pub tune: Tune,
9293
/// Parameters for grain synthesis.
9394
pub film_grain_params: Option<Vec<GrainTableSegment>>,
95+
/// HDR10+, ST2094-40 T.35 metadata payload map, by input frame index.
96+
///
97+
/// The payloads are expected to follow the specification
98+
/// defined at https://aomediacodec.github.io/av1-hdr10plus.
99+
pub hdr10plus_payloads: Option<BTreeMap<u64, Vec<u8>>>,
94100
/// Number of tiles horizontally. Must be a power of two.
95101
///
96102
/// Overridden by [`tiles`], if present.
@@ -167,6 +173,7 @@ impl EncoderConfig {
167173
bitrate: 0,
168174
tune: Tune::default(),
169175
film_grain_params: None,
176+
hdr10plus_payloads: None,
170177
tile_cols: 0,
171178
tile_rows: 0,
172179
tiles: 0,

src/api/test.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2129,6 +2129,7 @@ fn log_q_exp_overflow() {
21292129
bitrate: 1,
21302130
tune: Tune::Psychovisual,
21312131
film_grain_params: None,
2132+
hdr10plus_payloads: None,
21322133
tile_cols: 0,
21332134
tile_rows: 0,
21342135
tiles: 0,
@@ -2206,6 +2207,7 @@ fn guess_frame_subtypes_assert() {
22062207
bitrate: 16384,
22072208
tune: Tune::Psychovisual,
22082209
film_grain_params: None,
2210+
hdr10plus_payloads: None,
22092211
tile_cols: 0,
22102212
tile_rows: 0,
22112213
tiles: 0,

src/api/util.rs

+16
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ impl fmt::Display for FrameType {
137137
}
138138
}
139139

140+
/// ST2094-40 T.35 metadata payload expected prefix.
141+
pub const ST2094_40_PREFIX: &[u8] = &[
142+
0x00, 0x03C, // Samsung Electronics America
143+
0x00, 0x01, // ST-2094-40
144+
0x04, // application_identifier = 4
145+
0x01, // application_mode = 1
146+
];
147+
140148
/// A single T.35 metadata packet.
141149
#[derive(Clone, Debug, Default)]
142150
pub struct T35 {
@@ -299,3 +307,11 @@ impl<T: Pixel> IntoFrame<T> for (Frame<T>, Option<FrameParameters>) {
299307
(Some(Arc::new(self.0)), self.1)
300308
}
301309
}
310+
311+
impl T35 {
312+
/// Whether the T.35 metadata is HDR10+ Metadata
313+
/// According to the specification at https://aomediacodec.github.io/av1-hdr10plus
314+
pub fn is_hdr10plus_metadata(&self) -> bool {
315+
self.country_code == 0xB5 && self.data.starts_with(ST2094_40_PREFIX)
316+
}
317+
}

src/bin/common.rs

+37
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use once_cell::sync::Lazy;
1717
use rav1e::prelude::*;
1818
use scan_fmt::scan_fmt;
1919

20+
use std::collections::BTreeMap;
2021
use std::fs::File;
2122
use std::io;
2223
use std::io::prelude::*;
@@ -194,6 +195,14 @@ pub struct CliOptions {
194195
help_heading = "ENCODE SETTINGS"
195196
)]
196197
pub film_grain_table: Option<PathBuf>,
198+
/// Uses a HDR10+ metadata JSON file to add as T.35 metadata to the encode.
199+
#[clap(
200+
long,
201+
alias = "dhdr10-info",
202+
value_parser,
203+
help_heading = "ENCODE SETTINGS"
204+
)]
205+
pub hdr10plus_json: Option<PathBuf>,
197206

198207
/// Pixel range
199208
#[clap(long, value_parser, help_heading = "VIDEO METADATA")]
@@ -678,6 +687,34 @@ fn parse_config(matches: &CliOptions) -> Result<EncoderConfig, CliError> {
678687
}
679688
}
680689

690+
if let Some(json_file) = matches.hdr10plus_json.as_ref() {
691+
let contents = std::fs::read_to_string(json_file)
692+
.expect("Failed to read HDR10+ metadata file");
693+
let metadata_root =
694+
hdr10plus::metadata_json::MetadataJsonRoot::parse(&contents)
695+
.expect("Failed to parse HDR10+ metadata");
696+
697+
let hdr10plus_enc_opts = hdr10plus::metadata::Hdr10PlusMetadataEncOpts {
698+
with_country_code: false,
699+
..Default::default()
700+
};
701+
let payloads: BTreeMap<u64, Vec<u8>> = metadata_root
702+
.scene_info
703+
.iter()
704+
.filter_map(|meta| {
705+
hdr10plus::metadata::Hdr10PlusMetadata::try_from(meta)
706+
.and_then(|meta| meta.encode_with_opts(&hdr10plus_enc_opts))
707+
.ok()
708+
})
709+
.zip(0u64..)
710+
.map(|(payload, frame_no)| (frame_no, payload))
711+
.collect();
712+
713+
if !payloads.is_empty() {
714+
cfg.hdr10plus_payloads = Some(payloads);
715+
}
716+
}
717+
681718
if let Some(frame_rate) = matches.frame_rate {
682719
cfg.time_base = Rational::new(matches.time_scale, frame_rate);
683720
}

src/encoder.rs

+44-10
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,26 @@ impl<T: Pixel> FrameInvariants<T> {
12861286
self.input_frameno * TIMESTAMP_BASE_UNIT * self.sequence.time_base.num
12871287
/ self.sequence.time_base.den
12881288
}
1289+
1290+
/// HDR10+ Metadata as T.35 metadata from [`EncoderConfig`]
1291+
pub fn hdr10plus_metadata(&self) -> Option<T35> {
1292+
if !(self.show_frame || self.is_show_existing_frame()) {
1293+
return None;
1294+
}
1295+
1296+
self
1297+
.config
1298+
.hdr10plus_payloads
1299+
.as_ref()
1300+
.and_then(|payloads| payloads.get(&self.input_frameno))
1301+
.and_then(|payload| {
1302+
Some(T35 {
1303+
country_code: 0xB5,
1304+
country_code_extension_byte: 0x00,
1305+
data: payload.clone().into_boxed_slice(),
1306+
})
1307+
})
1308+
}
12891309
}
12901310

12911311
impl<T: Pixel> fmt::Display for FrameInvariants<T> {
@@ -3681,11 +3701,14 @@ pub fn encode_show_existing_frame<T: Pixel>(
36813701
}
36823702

36833703
for t35 in fi.t35_metadata.iter() {
3684-
let mut t35_buf = Vec::new();
3685-
let mut t35_bw = BitWriter::endian(&mut t35_buf, BigEndian);
3686-
t35_bw.write_t35_metadata_obu(t35).unwrap();
3687-
packet.write_all(&t35_buf).unwrap();
3688-
t35_buf.clear();
3704+
write_t35_metadata_packet(&mut packet, t35);
3705+
}
3706+
3707+
// HDR10+ Metadata OBU from config
3708+
if let Some(t35) = fi.hdr10plus_metadata() {
3709+
if !fi.t35_metadata.iter().any(|t35| t35.is_hdr10plus_metadata()) {
3710+
write_t35_metadata_packet(&mut packet, &t35);
3711+
}
36893712
}
36903713

36913714
let mut buf1 = Vec::new();
@@ -3761,11 +3784,14 @@ pub fn encode_frame<T: Pixel>(
37613784
}
37623785

37633786
for t35 in fi.t35_metadata.iter() {
3764-
let mut t35_buf = Vec::new();
3765-
let mut t35_bw = BitWriter::endian(&mut t35_buf, BigEndian);
3766-
t35_bw.write_t35_metadata_obu(t35).unwrap();
3767-
packet.write_all(&t35_buf).unwrap();
3768-
t35_buf.clear();
3787+
write_t35_metadata_packet(&mut packet, t35);
3788+
}
3789+
3790+
// HDR10+ Metadata OBU from config
3791+
if let Some(t35) = fi.hdr10plus_metadata() {
3792+
if !fi.t35_metadata.iter().any(|t35| t35.is_hdr10plus_metadata()) {
3793+
write_t35_metadata_packet(&mut packet, &t35);
3794+
}
37693795
}
37703796

37713797
let mut buf1 = Vec::new();
@@ -3821,6 +3847,14 @@ pub fn update_rec_buffer<T: Pixel>(
38213847
}
38223848
}
38233849

3850+
fn write_t35_metadata_packet(packet: &mut Vec<u8>, t35: &T35) {
3851+
let mut t35_buf = Vec::new();
3852+
let mut t35_bw = BitWriter::endian(&mut t35_buf, BigEndian);
3853+
t35_bw.write_t35_metadata_obu(t35).unwrap();
3854+
packet.write_all(&t35_buf).unwrap();
3855+
t35_buf.clear();
3856+
}
3857+
38243858
#[cfg(test)]
38253859
mod test {
38263860
use super::*;

src/fuzzing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ impl Arbitrary for ArbitraryEncoder {
258258
switch_frame_interval: u.int_in_range(0..=3)?,
259259
tune: *u.choose(&[Tune::Psnr, Tune::Psychovisual])?,
260260
film_grain_params: None,
261+
hdr10plus_payloads: None,
261262
};
262263

263264
let frame_count =

0 commit comments

Comments
 (0)