Home | History | Annotate | Download | only in base
      1 /*
      2  * libjingle
      3  * Copyright 2008 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "talk/media/base/videocommon.h"
     29 #include "webrtc/base/gunit.h"
     30 
     31 namespace cricket {
     32 
     33 TEST(VideoCommonTest, TestCanonicalFourCC) {
     34   // Canonical fourccs are not changed.
     35   EXPECT_EQ(FOURCC_I420, CanonicalFourCC(FOURCC_I420));
     36   // The special FOURCC_ANY value is not changed.
     37   EXPECT_EQ(FOURCC_ANY, CanonicalFourCC(FOURCC_ANY));
     38   // Aliases are translated to the canonical equivalent.
     39   EXPECT_EQ(FOURCC_I420, CanonicalFourCC(FOURCC_IYUV));
     40   EXPECT_EQ(FOURCC_I422, CanonicalFourCC(FOURCC_YU16));
     41   EXPECT_EQ(FOURCC_I444, CanonicalFourCC(FOURCC_YU24));
     42   EXPECT_EQ(FOURCC_YUY2, CanonicalFourCC(FOURCC_YUYV));
     43   EXPECT_EQ(FOURCC_YUY2, CanonicalFourCC(FOURCC_YUVS));
     44   EXPECT_EQ(FOURCC_UYVY, CanonicalFourCC(FOURCC_HDYC));
     45   EXPECT_EQ(FOURCC_UYVY, CanonicalFourCC(FOURCC_2VUY));
     46   EXPECT_EQ(FOURCC_MJPG, CanonicalFourCC(FOURCC_JPEG));
     47   EXPECT_EQ(FOURCC_MJPG, CanonicalFourCC(FOURCC_DMB1));
     48   EXPECT_EQ(FOURCC_BGGR, CanonicalFourCC(FOURCC_BA81));
     49   EXPECT_EQ(FOURCC_RAW, CanonicalFourCC(FOURCC_RGB3));
     50   EXPECT_EQ(FOURCC_24BG, CanonicalFourCC(FOURCC_BGR3));
     51   EXPECT_EQ(FOURCC_BGRA, CanonicalFourCC(FOURCC_CM32));
     52   EXPECT_EQ(FOURCC_RAW, CanonicalFourCC(FOURCC_CM24));
     53 }
     54 
     55 // Test conversion between interval and fps
     56 TEST(VideoCommonTest, TestVideoFormatFps) {
     57   EXPECT_EQ(VideoFormat::kMinimumInterval, VideoFormat::FpsToInterval(0));
     58   EXPECT_EQ(rtc::kNumNanosecsPerSec / 20, VideoFormat::FpsToInterval(20));
     59   EXPECT_EQ(20, VideoFormat::IntervalToFps(rtc::kNumNanosecsPerSec / 20));
     60   EXPECT_EQ(0, VideoFormat::IntervalToFps(0));
     61 }
     62 
     63 // Test IsSize0x0
     64 TEST(VideoCommonTest, TestVideoFormatIsSize0x0) {
     65   VideoFormat format;
     66   EXPECT_TRUE(format.IsSize0x0());
     67   format.width = 320;
     68   EXPECT_FALSE(format.IsSize0x0());
     69 }
     70 
     71 // Test ToString: print fourcc when it is printable.
     72 TEST(VideoCommonTest, TestVideoFormatToString) {
     73   VideoFormat format;
     74   EXPECT_EQ("0x0x0", format.ToString());
     75 
     76   format.fourcc = FOURCC_I420;
     77   format.width = 640;
     78   format.height = 480;
     79   format.interval = VideoFormat::FpsToInterval(20);
     80   EXPECT_EQ("I420 640x480x20", format.ToString());
     81 
     82   format.fourcc = FOURCC_ANY;
     83   format.width = 640;
     84   format.height = 480;
     85   format.interval = VideoFormat::FpsToInterval(20);
     86   EXPECT_EQ("640x480x20", format.ToString());
     87 }
     88 
     89 // Test comparison.
     90 TEST(VideoCommonTest, TestVideoFormatCompare) {
     91   VideoFormat format(640, 480, VideoFormat::FpsToInterval(20), FOURCC_I420);
     92   VideoFormat format2;
     93   EXPECT_NE(format, format2);
     94 
     95   // Same pixelrate, different fourcc.
     96   format2 = format;
     97   format2.fourcc = FOURCC_YUY2;
     98   EXPECT_NE(format, format2);
     99   EXPECT_FALSE(format.IsPixelRateLess(format2) ||
    100                format2.IsPixelRateLess(format2));
    101 
    102   format2 = format;
    103   format2.interval /= 2;
    104   EXPECT_TRUE(format.IsPixelRateLess(format2));
    105 
    106   format2 = format;
    107   format2.width *= 2;
    108   EXPECT_TRUE(format.IsPixelRateLess(format2));
    109 }
    110 
    111 TEST(VideoCommonTest, TestComputeScaleWithLowFps) {
    112   int scaled_width, scaled_height;
    113 
    114   // Request small enough. Expect no change.
    115   ComputeScale(2560, 1600, 5, &scaled_width, &scaled_height);
    116   EXPECT_EQ(2560, scaled_width);
    117   EXPECT_EQ(1600, scaled_height);
    118 
    119   // Request too many pixels. Expect 1/2 size.
    120   ComputeScale(4096, 2560, 5, &scaled_width, &scaled_height);
    121   EXPECT_EQ(2048, scaled_width);
    122   EXPECT_EQ(1280, scaled_height);
    123 
    124   // Request too many pixels and too wide and tall. Expect 1/4 size.
    125   ComputeScale(16000, 10000, 5, &scaled_width, &scaled_height);
    126   EXPECT_EQ(2000, scaled_width);
    127   EXPECT_EQ(1250, scaled_height);
    128 
    129   // Request too wide. (two 30 inch monitors). Expect 1/2 size.
    130   ComputeScale(5120, 1600, 5, &scaled_width, &scaled_height);
    131   EXPECT_EQ(2560, scaled_width);
    132   EXPECT_EQ(800, scaled_height);
    133 
    134   // Request too wide but not too many pixels. Expect 1/2 size.
    135   ComputeScale(8192, 1024, 5, &scaled_width, &scaled_height);
    136   EXPECT_EQ(4096, scaled_width);
    137   EXPECT_EQ(512, scaled_height);
    138 
    139   // Request too tall. Expect 1/4 size.
    140   ComputeScale(1024, 8192, 5, &scaled_width, &scaled_height);
    141   EXPECT_EQ(256, scaled_width);
    142   EXPECT_EQ(2048, scaled_height);
    143 }
    144 
    145 // Same as TestComputeScale but with 15 fps instead of 5 fps.
    146 TEST(VideoCommonTest, TestComputeScaleWithHighFps) {
    147   int scaled_width, scaled_height;
    148 
    149   // Request small enough but high fps. Expect 1/2 size.
    150   ComputeScale(2560, 1600, 15, &scaled_width, &scaled_height);
    151   EXPECT_EQ(1280, scaled_width);
    152   EXPECT_EQ(800, scaled_height);
    153 
    154   // Request too many pixels. Expect 1/2 size.
    155   ComputeScale(4096, 2560, 15, &scaled_width, &scaled_height);
    156   EXPECT_EQ(2048, scaled_width);
    157   EXPECT_EQ(1280, scaled_height);
    158 
    159   // Request too many pixels and too wide and tall. Expect 1/16 size.
    160   ComputeScale(64000, 40000, 15, &scaled_width, &scaled_height);
    161   EXPECT_EQ(4000, scaled_width);
    162   EXPECT_EQ(2500, scaled_height);
    163 
    164   // Request too wide. (two 30 inch monitors). Expect 1/2 size.
    165   ComputeScale(5120, 1600, 15, &scaled_width, &scaled_height);
    166   EXPECT_EQ(2560, scaled_width);
    167   EXPECT_EQ(800, scaled_height);
    168 
    169   // Request too wide but not too many pixels. Expect 1/2 size.
    170   ComputeScale(8192, 1024, 15, &scaled_width, &scaled_height);
    171   EXPECT_EQ(4096, scaled_width);
    172   EXPECT_EQ(512, scaled_height);
    173 
    174   // Request too tall. Expect 1/4 size.
    175   ComputeScale(1024, 8192, 15, &scaled_width, &scaled_height);
    176   EXPECT_EQ(256, scaled_width);
    177   EXPECT_EQ(2048, scaled_height);
    178 }
    179 
    180 TEST(VideoCommonTest, TestComputeCrop) {
    181   int cropped_width, cropped_height;
    182 
    183   // Request 16:9 to 16:9.  Expect no cropping.
    184   ComputeCrop(1280, 720,  // Crop size 16:9
    185               640, 360,  // Frame is 4:3
    186               1, 1,  // Normal 1:1 pixels
    187               0,
    188               &cropped_width, &cropped_height);
    189   EXPECT_EQ(640, cropped_width);
    190   EXPECT_EQ(360, cropped_height);
    191 
    192   // Request 4:3 to 16:9.  Expect vertical.
    193   ComputeCrop(640, 360,  // Crop size 16:9
    194               640, 480,  // Frame is 4:3
    195               1, 1,  // Normal 1:1 pixels
    196               0,
    197               &cropped_width, &cropped_height);
    198   EXPECT_EQ(640, cropped_width);
    199   EXPECT_EQ(360, cropped_height);
    200 
    201   // Request 16:9 to 4:3.  Expect horizontal crop.
    202   ComputeCrop(640, 480,  // Crop size 4:3
    203               640, 360,  // Frame is 16:9
    204               1, 1,  // Normal 1:1 pixels
    205               0,
    206               &cropped_width, &cropped_height);
    207   EXPECT_EQ(480, cropped_width);
    208   EXPECT_EQ(360, cropped_height);
    209 
    210   // Request 16:9 but VGA has 3:8 pixel aspect ratio. Expect no crop.
    211   // This occurs on HP4110 on OSX 10.5/10.6/10.7
    212   ComputeCrop(640, 360,  // Crop size 16:9
    213               640, 480,  // Frame is 4:3
    214               3, 8,  // Pixel aspect ratio is tall
    215               0,
    216               &cropped_width, &cropped_height);
    217   EXPECT_EQ(640, cropped_width);
    218   EXPECT_EQ(480, cropped_height);
    219 
    220   // Request 16:9 but QVGA has 15:11 pixel aspect ratio. Expect horizontal crop.
    221   // This occurs on Logitech B910 on OSX 10.5/10.6/10.7 in Hangouts.
    222   ComputeCrop(640, 360,  // Crop size 16:9
    223               320, 240,  // Frame is 4:3
    224               15, 11,  // Pixel aspect ratio is wide
    225               0,
    226               &cropped_width, &cropped_height);
    227   EXPECT_EQ(312, cropped_width);
    228   EXPECT_EQ(240, cropped_height);
    229 
    230   // Request 16:10 but QVGA has 15:11 pixel aspect ratio.
    231   // Expect horizontal crop.
    232   // This occurs on Logitech B910 on OSX 10.5/10.6/10.7 in gmail.
    233   ComputeCrop(640, 400,  // Crop size 16:10
    234               320, 240,  // Frame is 4:3
    235               15, 11,  // Pixel aspect ratio is wide
    236               0,
    237               &cropped_width, &cropped_height);
    238   EXPECT_EQ(280, cropped_width);
    239   EXPECT_EQ(240, cropped_height);
    240 
    241   // Request 16:9 but VGA has 6:5 pixel aspect ratio. Expect vertical crop.
    242   // This occurs on Logitech QuickCam Pro C9000 on OSX
    243   ComputeCrop(640, 360,  // Crop size 16:9
    244               640, 480,  // Frame is 4:3
    245               6, 5,  // Pixel aspect ratio is wide
    246               0,
    247               &cropped_width, &cropped_height);
    248   EXPECT_EQ(640, cropped_width);
    249   EXPECT_EQ(432, cropped_height);
    250 
    251   // Request 16:10 but HD is 16:9. Expect horizontal crop.
    252   // This occurs in settings and local preview with HD experiment.
    253   ComputeCrop(1280, 800,  // Crop size 16:10
    254               1280, 720,  // Frame is 4:3
    255               1, 1,  // Pixel aspect ratio is wide
    256               0,
    257               &cropped_width, &cropped_height);
    258   EXPECT_EQ(1152, cropped_width);
    259   EXPECT_EQ(720, cropped_height);
    260 
    261   // Request 16:9 but HD has 3:4 pixel aspect ratio. Expect vertical crop.
    262   // This occurs on Logitech B910 on OSX 10.5/10.6.7 but not OSX 10.6.8 or 10.7
    263   ComputeCrop(1280, 720,  // Crop size 16:9
    264               1280, 720,  // Frame is 4:3
    265               3, 4,  // Pixel aspect ratio is wide
    266               0,
    267               &cropped_width, &cropped_height);
    268   EXPECT_EQ(1280, cropped_width);
    269   EXPECT_EQ(540, cropped_height);
    270 
    271   // Request 16:9 to 3:4 (portrait).  Expect no cropping.
    272   ComputeCrop(640, 360,  // Crop size 16:9
    273               640, 480,  // Frame is 3:4 portrait
    274               1, 1,  // Normal 1:1 pixels
    275               90,
    276               &cropped_width, &cropped_height);
    277   EXPECT_EQ(640, cropped_width);
    278   EXPECT_EQ(480, cropped_height);
    279 
    280   // Request 9:16 from VGA rotated (portrait).  Expect crop.
    281   ComputeCrop(360, 640,  // Crop size 9:16
    282               640, 480,  // Frame is 3:4 portrait
    283               1, 1,  // Normal 1:1 pixels
    284               90,
    285               &cropped_width, &cropped_height);
    286   EXPECT_EQ(640, cropped_width);
    287   EXPECT_EQ(360, cropped_height);
    288 
    289   // Cropped size 0x0.  Expect no cropping.
    290   // This is used when adding multiple capturers
    291   ComputeCrop(0, 0,  // Crop size 0x0
    292               1024, 768,  // Frame is 3:4 portrait
    293               1, 1,  // Normal 1:1 pixels
    294               0,
    295               &cropped_width, &cropped_height);
    296   EXPECT_EQ(1024, cropped_width);
    297   EXPECT_EQ(768, cropped_height);
    298 }
    299 
    300 TEST(VideoCommonTest, TestComputeScaleToSquarePixels) {
    301   int scaled_width, scaled_height;
    302 
    303   // Pixel aspect ratio is 4:3.  Logical aspect ratio is 16:9.  Expect scale
    304   // to square pixels with physical aspect ratio of 16:9.
    305   ComputeScaleToSquarePixels(640, 480,
    306                              4, 3,  // 4 x 3 pixel aspect ratio
    307                              &scaled_width, &scaled_height);
    308   EXPECT_EQ(640, scaled_width);
    309   EXPECT_EQ(360, scaled_height);
    310 
    311   // Pixel aspect ratio is 3:8.  Physical aspect ratio is 4:3.  Expect scale
    312   // to square pixels with logical aspect ratio of 1:2.
    313   // Note that 640x1280 will be scaled down by video adapter to view request
    314   // of 640*360 and will end up using 320x640.
    315   ComputeScaleToSquarePixels(640, 480,
    316                              3, 8,  // 4 x 3 pixel aspect ratio
    317                              &scaled_width, &scaled_height);
    318   EXPECT_EQ(640, scaled_width);
    319   EXPECT_EQ(1280, scaled_height);
    320 }
    321 
    322 }  // namespace cricket
    323