1 /*
2  *  Copyright (c) 2010 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 #include "media/base/video_common.h"
12 
13 #include "api/array_view.h"
14 #include "rtc_base/arraysize.h"
15 #include "rtc_base/checks.h"
16 #include "rtc_base/strings/string_builder.h"
17 
18 namespace cricket {
19 
20 struct FourCCAliasEntry {
21   uint32_t alias;
22   uint32_t canonical;
23 };
24 
25 static const FourCCAliasEntry kFourCCAliases[] = {
26     {FOURCC_IYUV, FOURCC_I420},
27     {FOURCC_YU16, FOURCC_I422},
28     {FOURCC_YU24, FOURCC_I444},
29     {FOURCC_YUYV, FOURCC_YUY2},
30     {FOURCC_YUVS, FOURCC_YUY2},
31     {FOURCC_HDYC, FOURCC_UYVY},
32     {FOURCC_2VUY, FOURCC_UYVY},
33     {FOURCC_JPEG, FOURCC_MJPG},  // Note: JPEG has DHT while MJPG does not.
34     {FOURCC_DMB1, FOURCC_MJPG},
35     {FOURCC_BA81, FOURCC_BGGR},
36     {FOURCC_RGB3, FOURCC_RAW},
37     {FOURCC_BGR3, FOURCC_24BG},
38     {FOURCC_CM32, FOURCC_BGRA},
39     {FOURCC_CM24, FOURCC_RAW},
40 };
41 
CanonicalFourCC(uint32_t fourcc)42 uint32_t CanonicalFourCC(uint32_t fourcc) {
43   for (uint32_t i = 0; i < arraysize(kFourCCAliases); ++i) {
44     if (kFourCCAliases[i].alias == fourcc) {
45       return kFourCCAliases[i].canonical;
46     }
47   }
48   // Not an alias, so return it as-is.
49   return fourcc;
50 }
51 
52 // The C++ standard requires a namespace-scope definition of static const
53 // integral types even when they are initialized in the declaration (see
54 // [class.static.data]/4), but MSVC with /Ze is non-conforming and treats that
55 // as a multiply defined symbol error. See Also:
56 // http://msdn.microsoft.com/en-us/library/34h23df8.aspx
57 #ifndef _MSC_EXTENSIONS
58 const int64_t VideoFormat::kMinimumInterval;  // Initialized in header.
59 #endif
60 
ToString() const61 std::string VideoFormat::ToString() const {
62   std::string fourcc_name = GetFourccName(fourcc) + " ";
63   for (std::string::const_iterator i = fourcc_name.begin();
64        i < fourcc_name.end(); ++i) {
65     // Test character is printable; Avoid isprint() which asserts on negatives.
66     if (*i < 32 || *i >= 127) {
67       fourcc_name = "";
68       break;
69     }
70   }
71 
72   char buf[256];
73   rtc::SimpleStringBuilder sb(buf);
74   sb << fourcc_name << width << "x" << height << "x"
75      << IntervalToFpsFloat(interval);
76   return sb.str();
77 }
78 
GreatestCommonDivisor(int a,int b)79 int GreatestCommonDivisor(int a, int b) {
80   RTC_DCHECK_GE(a, 0);
81   RTC_DCHECK_GT(b, 0);
82   int c = a % b;
83   while (c != 0) {
84     a = b;
85     b = c;
86     c = a % b;
87   }
88   return b;
89 }
90 
LeastCommonMultiple(int a,int b)91 int LeastCommonMultiple(int a, int b) {
92   RTC_DCHECK_GT(a, 0);
93   RTC_DCHECK_GT(b, 0);
94   return a * (b / GreatestCommonDivisor(a, b));
95 }
96 
97 }  // namespace cricket
98