tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

video_common.h (8794B)


      1 /*
      2 *  Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 // Common definition for video, including fourcc and VideoFormat.
     12 
     13 #ifndef MEDIA_BASE_VIDEO_COMMON_H_
     14 #define MEDIA_BASE_VIDEO_COMMON_H_
     15 
     16 #include <stdint.h>
     17 
     18 #include <string>
     19 
     20 #include "absl/base/macros.h"
     21 #include "rtc_base/system/rtc_export.h"
     22 #include "rtc_base/time_utils.h"
     23 
     24 namespace webrtc {
     25 
     26 //////////////////////////////////////////////////////////////////////////////
     27 // Definition of FourCC codes
     28 //////////////////////////////////////////////////////////////////////////////
     29 // Convert four characters to a FourCC code.
     30 // Needs to be a macro otherwise the OS X compiler complains when the kFormat*
     31 // constants are used in a switch.
     32 #define CRICKET_FOURCC(a, b, c, d)                                \
     33  ((static_cast<uint32_t>(a)) | (static_cast<uint32_t>(b) << 8) | \
     34   (static_cast<uint32_t>(c) << 16) | (static_cast<uint32_t>(d) << 24))
     35 // Some pages discussing FourCC codes:
     36 //   http://www.fourcc.org/yuv.php
     37 //   http://v4l2spec.bytesex.org/spec/book1.htm
     38 //   http://developer.apple.com/quicktime/icefloe/dispatch020.html
     39 //   http://msdn.microsoft.com/library/windows/desktop/dd206750.aspx#nv12
     40 //   http://people.xiph.org/~xiphmont/containers/nut/nut4cc.txt
     41 
     42 // FourCC codes grouped according to implementation efficiency.
     43 // Primary formats should convert in 1 efficient step.
     44 // Secondary formats are converted in 2 steps.
     45 // Auxilliary formats call primary converters.
     46 enum FourCC {
     47  // 9 Primary YUV formats: 5 planar, 2 biplanar, 2 packed.
     48  FOURCC_I420 = CRICKET_FOURCC('I', '4', '2', '0'),
     49  FOURCC_I422 = CRICKET_FOURCC('I', '4', '2', '2'),
     50  FOURCC_I444 = CRICKET_FOURCC('I', '4', '4', '4'),
     51  FOURCC_I411 = CRICKET_FOURCC('I', '4', '1', '1'),
     52  FOURCC_I400 = CRICKET_FOURCC('I', '4', '0', '0'),
     53  FOURCC_NV21 = CRICKET_FOURCC('N', 'V', '2', '1'),
     54  FOURCC_NV12 = CRICKET_FOURCC('N', 'V', '1', '2'),
     55  FOURCC_YUY2 = CRICKET_FOURCC('Y', 'U', 'Y', '2'),
     56  FOURCC_UYVY = CRICKET_FOURCC('U', 'Y', 'V', 'Y'),
     57 
     58  // 2 Secondary YUV formats: row biplanar.
     59  FOURCC_M420 = CRICKET_FOURCC('M', '4', '2', '0'),
     60 
     61  // 9 Primary RGB formats: 4 32 bpp, 2 24 bpp, 3 16 bpp.
     62  FOURCC_ARGB = CRICKET_FOURCC('A', 'R', 'G', 'B'),
     63  FOURCC_BGRA = CRICKET_FOURCC('B', 'G', 'R', 'A'),
     64  FOURCC_ABGR = CRICKET_FOURCC('A', 'B', 'G', 'R'),
     65  FOURCC_24BG = CRICKET_FOURCC('2', '4', 'B', 'G'),
     66  FOURCC_RAW = CRICKET_FOURCC('r', 'a', 'w', ' '),
     67  FOURCC_RGBA = CRICKET_FOURCC('R', 'G', 'B', 'A'),
     68  FOURCC_RGBP = CRICKET_FOURCC('R', 'G', 'B', 'P'),  // bgr565.
     69  FOURCC_RGBO = CRICKET_FOURCC('R', 'G', 'B', 'O'),  // abgr1555.
     70  FOURCC_R444 = CRICKET_FOURCC('R', '4', '4', '4'),  // argb4444.
     71 
     72  // 4 Secondary RGB formats: 4 Bayer Patterns.
     73  FOURCC_RGGB = CRICKET_FOURCC('R', 'G', 'G', 'B'),
     74  FOURCC_BGGR = CRICKET_FOURCC('B', 'G', 'G', 'R'),
     75  FOURCC_GRBG = CRICKET_FOURCC('G', 'R', 'B', 'G'),
     76  FOURCC_GBRG = CRICKET_FOURCC('G', 'B', 'R', 'G'),
     77 
     78  // 1 Primary Compressed YUV format.
     79  FOURCC_MJPG = CRICKET_FOURCC('M', 'J', 'P', 'G'),
     80 
     81  // 5 Auxiliary YUV variations: 3 with U and V planes are swapped, 1 Alias.
     82  FOURCC_YV12 = CRICKET_FOURCC('Y', 'V', '1', '2'),
     83  FOURCC_YV16 = CRICKET_FOURCC('Y', 'V', '1', '6'),
     84  FOURCC_YV24 = CRICKET_FOURCC('Y', 'V', '2', '4'),
     85  FOURCC_YU12 = CRICKET_FOURCC('Y', 'U', '1', '2'),  // Linux version of I420.
     86  FOURCC_J420 = CRICKET_FOURCC('J', '4', '2', '0'),
     87  FOURCC_J400 = CRICKET_FOURCC('J', '4', '0', '0'),
     88 
     89  // 14 Auxiliary aliases.  CanonicalFourCC() maps these to canonical FOURCC.
     90  FOURCC_IYUV = CRICKET_FOURCC('I', 'Y', 'U', 'V'),  // Alias for I420.
     91  FOURCC_YU16 = CRICKET_FOURCC('Y', 'U', '1', '6'),  // Alias for I422.
     92  FOURCC_YU24 = CRICKET_FOURCC('Y', 'U', '2', '4'),  // Alias for I444.
     93  FOURCC_YUYV = CRICKET_FOURCC('Y', 'U', 'Y', 'V'),  // Alias for YUY2.
     94  FOURCC_YUVS = CRICKET_FOURCC('y', 'u', 'v', 's'),  // Alias for YUY2 on Mac.
     95  FOURCC_HDYC = CRICKET_FOURCC('H', 'D', 'Y', 'C'),  // Alias for UYVY.
     96  FOURCC_2VUY = CRICKET_FOURCC('2', 'v', 'u', 'y'),  // Alias for UYVY on Mac.
     97  FOURCC_JPEG = CRICKET_FOURCC('J', 'P', 'E', 'G'),  // Alias for MJPG.
     98  FOURCC_DMB1 = CRICKET_FOURCC('d', 'm', 'b', '1'),  // Alias for MJPG on Mac.
     99  FOURCC_BA81 = CRICKET_FOURCC('B', 'A', '8', '1'),  // Alias for BGGR.
    100  FOURCC_RGB3 = CRICKET_FOURCC('R', 'G', 'B', '3'),  // Alias for RAW.
    101  FOURCC_BGR3 = CRICKET_FOURCC('B', 'G', 'R', '3'),  // Alias for 24BG.
    102  FOURCC_CM32 = CRICKET_FOURCC(0, 0, 0, 32),  // BGRA kCMPixelFormat_32ARGB
    103  FOURCC_CM24 = CRICKET_FOURCC(0, 0, 0, 24),  // RAW kCMPixelFormat_24RGB
    104 
    105  // 1 Auxiliary compressed YUV format set aside for capturer.
    106  FOURCC_H264 = CRICKET_FOURCC('H', '2', '6', '4'),
    107 };
    108 
    109 #undef CRICKET_FOURCC
    110 
    111 // Match any fourcc.
    112 
    113 // We move this out of the enum because using it in many places caused
    114 // the compiler to get grumpy, presumably since the above enum is
    115 // backed by an int.
    116 static const uint32_t FOURCC_ANY = 0xFFFFFFFF;
    117 
    118 // Converts fourcc aliases into canonical ones.
    119 uint32_t CanonicalFourCC(uint32_t fourcc);
    120 
    121 // Get FourCC code as a string.
    122 inline std::string GetFourccName(uint32_t fourcc) {
    123  std::string name;
    124  name.push_back(static_cast<char>(fourcc & 0xFF));
    125  name.push_back(static_cast<char>((fourcc >> 8) & 0xFF));
    126  name.push_back(static_cast<char>((fourcc >> 16) & 0xFF));
    127  name.push_back(static_cast<char>((fourcc >> 24) & 0xFF));
    128  return name;
    129 }
    130 
    131 //////////////////////////////////////////////////////////////////////////////
    132 // Definition of VideoFormat.
    133 //////////////////////////////////////////////////////////////////////////////
    134 
    135 // VideoFormat with Plain Old Data for global variables.
    136 struct VideoFormatPod {
    137  int width;         // Number of pixels.
    138  int height;        // Number of pixels.
    139  int64_t interval;  // Nanoseconds.
    140  uint32_t fourcc;  // Color space. FOURCC_ANY means that any color space is OK.
    141 };
    142 
    143 struct RTC_EXPORT VideoFormat : VideoFormatPod {
    144  static const int64_t kMinimumInterval =
    145      kNumNanosecsPerSec / 10000;  // 10k fps.
    146 
    147  VideoFormat() { Construct(0, 0, 0, 0); }
    148 
    149  VideoFormat(int w, int h, int64_t interval_ns, uint32_t cc) {
    150    Construct(w, h, interval_ns, cc);
    151  }
    152 
    153  explicit VideoFormat(const VideoFormatPod& format) {
    154    Construct(format.width, format.height, format.interval, format.fourcc);
    155  }
    156 
    157  void Construct(int w, int h, int64_t interval_ns, uint32_t cc) {
    158    width = w;
    159    height = h;
    160    interval = interval_ns;
    161    fourcc = cc;
    162  }
    163 
    164  static int64_t FpsToInterval(int fps) {
    165    return fps ? kNumNanosecsPerSec / fps : kMinimumInterval;
    166  }
    167 
    168  static int IntervalToFps(int64_t interval) {
    169    if (!interval) {
    170      return 0;
    171    }
    172    return static_cast<int>(kNumNanosecsPerSec / interval);
    173  }
    174 
    175  static float IntervalToFpsFloat(int64_t interval) {
    176    if (!interval) {
    177      return 0.f;
    178    }
    179    return static_cast<float>(kNumNanosecsPerSec) /
    180           static_cast<float>(interval);
    181  }
    182 
    183  bool operator==(const VideoFormat& format) const {
    184    return width == format.width && height == format.height &&
    185           interval == format.interval && fourcc == format.fourcc;
    186  }
    187 
    188  bool operator!=(const VideoFormat& format) const {
    189    return !(*this == format);
    190  }
    191 
    192  bool operator<(const VideoFormat& format) const {
    193    return (fourcc < format.fourcc) ||
    194           (fourcc == format.fourcc && width < format.width) ||
    195           (fourcc == format.fourcc && width == format.width &&
    196            height < format.height) ||
    197           (fourcc == format.fourcc && width == format.width &&
    198            height == format.height && interval > format.interval);
    199  }
    200 
    201  int framerate() const { return IntervalToFps(interval); }
    202 
    203  // Check if both width and height are 0.
    204  bool IsSize0x0() const { return 0 == width && 0 == height; }
    205 
    206  // Check if this format is less than another one by comparing the resolution
    207  // and frame rate.
    208  bool IsPixelRateLess(const VideoFormat& format) const {
    209    return width * height * framerate() <
    210           format.width * format.height * format.framerate();
    211  }
    212 
    213  // Get a string presentation in the form of "fourcc width x height x fps"
    214  std::string ToString() const;
    215 };
    216 
    217 // Returns the largest positive integer that divides both `a` and `b`.
    218 ABSL_DEPRECATE_AND_INLINE() int GreatestCommonDivisor(int a, int b);
    219 
    220 // Returns the smallest positive integer that is divisible by both `a` and `b`.
    221 ABSL_DEPRECATE_AND_INLINE() int LeastCommonMultiple(int a, int b);
    222 
    223 }  //  namespace webrtc
    224 
    225 
    226 #endif  // MEDIA_BASE_VIDEO_COMMON_H_