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