• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2015 The WebM project authors. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS.  All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 #ifndef LIBWEBM_COMMON_LIBWEBM_UTIL_H_
9 #define LIBWEBM_COMMON_LIBWEBM_UTIL_H_
10 
11 #include <cstddef>
12 #include <cstdint>
13 #include <cstdio>
14 #include <memory>
15 #include <vector>
16 
17 namespace libwebm {
18 
19 const double kNanosecondsPerSecond = 1000000000.0;
20 
21 // fclose functor for wrapping FILE in std::unique_ptr.
22 // TODO(tomfinegan): Move this to file_util once c++11 restrictions are
23 //                   relaxed.
24 struct FILEDeleter {
operatorFILEDeleter25   int operator()(std::FILE* f) {
26     if (f != nullptr)
27       return fclose(f);
28     return 0;
29   }
30 };
31 typedef std::unique_ptr<std::FILE, FILEDeleter> FilePtr;
32 
33 struct Range {
RangeRange34   Range(std::size_t off, std::size_t len) : offset(off), length(len) {}
35   Range() = delete;
36   Range(const Range&) = default;
37   Range(Range&&) = default;
38   ~Range() = default;
39   const std::size_t offset;
40   const std::size_t length;
41 };
42 typedef std::vector<Range> Ranges;
43 
44 // Converts |nanoseconds| to 90000 Hz clock ticks and vice versa. Each return
45 // the converted value.
46 std::int64_t NanosecondsTo90KhzTicks(std::int64_t nanoseconds);
47 std::int64_t Khz90TicksToNanoseconds(std::int64_t khz90_ticks);
48 
49 // Returns true and stores frame offsets and lengths in |frame_ranges| when
50 // |frame| has a valid VP9 super frame index. Sets |error| to true when parsing
51 // fails for any reason.
52 bool ParseVP9SuperFrameIndex(const std::uint8_t* frame,
53                              std::size_t frame_length, Ranges* frame_ranges,
54                              bool* error);
55 
56 // Writes |val| to |fileptr| and returns true upon success.
57 bool WriteUint8(std::uint8_t val, std::FILE* fileptr);
58 
59 // Reads 2 bytes from |buf| and returns them as a uint16_t. Returns 0 when |buf|
60 // is a nullptr.
61 std::uint16_t ReadUint16(const std::uint8_t* buf);
62 
63 }  // namespace libwebm
64 
65 #endif  // LIBWEBM_COMMON_LIBWEBM_UTIL_H_
66