video_common.h 8.5 KB

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