Home | History | Annotate | Download | only in test_framework
      1 /*
      2  *  Copyright (c) 2012 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 "webrtc/modules/video_coding/codecs/test_framework/test.h"
     12 
     13 #include <string.h>
     14 
     15 #include <iostream>
     16 
     17 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
     18 #include "webrtc/test/testsupport/metrics/video_metrics.h"
     19 
     20 using namespace webrtc;
     21 
     22 long filesize(const char *filename); // local function defined at end of file
     23 
     24 CodecTest::CodecTest(std::string name, std::string description)
     25 :
     26 _bitRate(0),
     27 _inname(""),
     28 _outname(""),
     29 _encodedName(""),
     30 _name(name),
     31 _description(description)
     32 {
     33     memset(&_inst, 0, sizeof(_inst));
     34     unsigned int seed = static_cast<unsigned int>(0);
     35     srand(seed);
     36 }
     37 
     38 CodecTest::CodecTest(std::string name, std::string description,
     39                      uint32_t bitRate)
     40 :
     41 _bitRate(bitRate),
     42 _inname(""),
     43 _outname(""),
     44 _encodedName(""),
     45 _name(name),
     46 _description(description)
     47 {
     48     memset(&_inst, 0, sizeof(_inst));
     49     unsigned int seed = static_cast<unsigned int>(0);
     50     srand(seed);
     51 }
     52 
     53 void
     54 CodecTest::Print()
     55 {
     56     std::cout << _name << " completed!" << std::endl;
     57     (*_log) << _name << std::endl;
     58     (*_log) << _description << std::endl;
     59     (*_log) << "Input file: " << _inname << std::endl;
     60     (*_log) << "Output file: " << _outname << std::endl;
     61     webrtc::test::QualityMetricsResult psnr;
     62     webrtc::test::QualityMetricsResult ssim;
     63     I420PSNRFromFiles(_inname.c_str(), _outname.c_str(), _inst.width,
     64                       _inst.height, &psnr);
     65     I420SSIMFromFiles(_inname.c_str(), _outname.c_str(), _inst.width,
     66                       _inst.height, &ssim);
     67 
     68     (*_log) << "PSNR: " << psnr.average << std::endl;
     69     std::cout << "PSNR: " << psnr.average << std::endl << std::endl;
     70     (*_log) << "SSIM: " << ssim.average << std::endl;
     71     std::cout << "SSIM: " << ssim.average << std::endl << std::endl;
     72     (*_log) << std::endl;
     73 }
     74 
     75 void
     76 CodecTest::Setup()
     77 {
     78     _width = _inst.width;
     79     _halfWidth = (_width + 1) / 2;
     80     _height = _inst.height;
     81     _halfHeight = (_height + 1) / 2;;
     82     _sizeY = _width * _height;
     83     _sizeUv = _halfWidth * _halfHeight;
     84     _lengthSourceFrame  = webrtc::CalcBufferSize(webrtc::kI420,
     85                                                  _inst.width,
     86                                                  _inst.height);
     87     _sourceBuffer       = new unsigned char[_lengthSourceFrame];
     88 }
     89 
     90 void
     91 CodecTest::CodecSettings(int width, int height,
     92                          uint32_t frameRate /*=30*/,
     93                          uint32_t bitRate /*=0*/)
     94 {
     95     if (bitRate > 0)
     96     {
     97         _bitRate = bitRate;
     98     }
     99     else if (_bitRate == 0)
    100     {
    101         _bitRate = 600;
    102     }
    103     _inst.codecType = kVideoCodecVP8;
    104     _inst.codecSpecific.VP8.feedbackModeOn = true;
    105     _inst.maxFramerate = (unsigned char)frameRate;
    106     _inst.startBitrate = (int)_bitRate;
    107     _inst.maxBitrate = 8000;
    108     _inst.width = width;
    109     _inst.height = height;
    110 }
    111 
    112 void
    113 CodecTest::Teardown()
    114 {
    115     delete [] _sourceBuffer;
    116 }
    117 
    118 void
    119 CodecTest::SetEncoder(webrtc::VideoEncoder*encoder)
    120 {
    121     _encoder = encoder;
    122 }
    123 
    124 void
    125 CodecTest::SetDecoder(VideoDecoder*decoder)
    126 {
    127     _decoder = decoder;
    128 }
    129 
    130 void
    131 CodecTest::SetLog(std::fstream* log)
    132 {
    133     _log = log;
    134 }
    135 
    136 double CodecTest::ActualBitRate(int nFrames)
    137 {
    138     return 8.0 * _sumEncBytes / (nFrames / _inst.maxFramerate);
    139 }
    140 
    141 bool CodecTest::PacketLoss(double lossRate, int /*thrown*/)
    142 {
    143     return RandUniform() < lossRate;
    144 }
    145 
    146 void
    147 CodecTest::VideoEncodedBufferToEncodedImage(VideoFrame& videoBuffer,
    148                                             EncodedImage &image)
    149 {
    150     image._buffer = videoBuffer.Buffer();
    151     image._length = videoBuffer.Length();
    152     image._size = videoBuffer.Size();
    153     // image._frameType = static_cast<VideoFrameType>
    154     //     (videoBuffer.GetFrameType());
    155     image._timeStamp = videoBuffer.TimeStamp();
    156     image._encodedWidth = videoBuffer.Width();
    157     image._encodedHeight = videoBuffer.Height();
    158     image._completeFrame = true;
    159 }
    160 
    161 long filesize(const char *filename)
    162 {
    163     FILE *f = fopen(filename,"rb");  /* open the file in read only */
    164     long size = 0;
    165     if (fseek(f,0,SEEK_END)==0) /* seek was successful */
    166         size = ftell(f);
    167     fclose(f);
    168     return size;
    169 }
    170