LibVideo: Parse the duration of Matroska files

This commit is contained in:
Zaggy1024 2022-10-29 17:01:01 -05:00 committed by Andreas Kling
parent 2b4b6c5613
commit 3a2f6c700d
Notes: sideshowbarker 2024-07-17 04:58:24 +09:00
3 changed files with 34 additions and 0 deletions

View file

@ -30,11 +30,14 @@ public:
void set_muxing_app(String muxing_app) { m_muxing_app = move(muxing_app); } void set_muxing_app(String muxing_app) { m_muxing_app = move(muxing_app); }
Utf8View writing_app() const { return Utf8View(m_writing_app); } Utf8View writing_app() const { return Utf8View(m_writing_app); }
void set_writing_app(String writing_app) { m_writing_app = move(writing_app); } void set_writing_app(String writing_app) { m_writing_app = move(writing_app); }
Optional<double> duration() const { return m_duration; }
void set_duration(double duration) { m_duration.emplace(duration); }
private: private:
u64 m_timestamp_scale { 1'000'000 }; u64 m_timestamp_scale { 1'000'000 };
String m_muxing_app; String m_muxing_app;
String m_writing_app; String m_writing_app;
Optional<double> m_duration;
}; };
class TrackEntry { class TrackEntry {

View file

@ -26,6 +26,7 @@ constexpr u32 CLUSTER_ELEMENT_ID = 0x1F43B675;
constexpr u32 TIMESTAMP_SCALE_ID = 0x2AD7B1; constexpr u32 TIMESTAMP_SCALE_ID = 0x2AD7B1;
constexpr u32 MUXING_APP_ID = 0x4D80; constexpr u32 MUXING_APP_ID = 0x4D80;
constexpr u32 WRITING_APP_ID = 0x5741; constexpr u32 WRITING_APP_ID = 0x5741;
constexpr u32 DURATION_ID = 0x4489;
// Tracks // Tracks
constexpr u32 TRACK_ENTRY_ID = 0xAE; constexpr u32 TRACK_ENTRY_ID = 0xAE;
@ -195,6 +196,11 @@ OwnPtr<SegmentInformation> MatroskaReader::parse_information()
CHECK_HAS_VALUE(writing_app); CHECK_HAS_VALUE(writing_app);
segment_information->set_writing_app(writing_app.value()); segment_information->set_writing_app(writing_app.value());
dbgln_if(MATROSKA_DEBUG, "Read WritingApp attribute: {}", writing_app.value()); dbgln_if(MATROSKA_DEBUG, "Read WritingApp attribute: {}", writing_app.value());
} else if (element_id == DURATION_ID) {
auto duration = read_float_element();
CHECK_HAS_VALUE(duration);
segment_information->set_duration(duration.value());
dbgln_if(MATROSKA_DEBUG, "Read Duration attribute: {}", duration.value());
} else { } else {
return read_unknown_element(); return read_unknown_element();
} }
@ -516,6 +522,30 @@ Optional<u64> MatroskaReader::read_u64_element()
return result; return result;
} }
Optional<double> MatroskaReader::read_float_element()
{
auto length = m_streamer.read_variable_size_integer();
if (!length.has_value() || m_streamer.remaining() < length.value())
return {};
if (length != 4u && length != 8u)
return {};
union {
u64 value;
float float_value;
double double_value;
} read_data;
read_data.value = 0;
for (size_t i = 0; i < length.value(); i++) {
if (!m_streamer.has_octet())
return {};
read_data.value = (read_data.value << 8u) + m_streamer.read_octet();
}
if (length == 4u)
return read_data.float_value;
return read_data.double_value;
}
bool MatroskaReader::read_unknown_element() bool MatroskaReader::read_unknown_element()
{ {
auto element_length = m_streamer.read_variable_size_integer(); auto element_length = m_streamer.read_variable_size_integer();

View file

@ -166,6 +166,7 @@ private:
Optional<String> read_string_element(); Optional<String> read_string_element();
Optional<u64> read_u64_element(); Optional<u64> read_u64_element();
Optional<double> read_float_element();
bool read_unknown_element(); bool read_unknown_element();
Streamer m_streamer; Streamer m_streamer;