Converted old jpeg_test tool to gtest unit test.

Restructured paths to new directory layout.

Stefan: common_video/*
Magnus: video_engine/*
Niklas: Android.mk

BUG=
TEST=jpeg_unittests on Debug+Release on Linux, Mac, Windows. Valgrind on Linux passes without warnings.

Review URL: https://webrtc-codereview.appspot.com/388007

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1691 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
kjellander@webrtc.org 2012-02-15 09:47:55 +00:00
parent 38a0d28d35
commit 0a57aae75b
15 changed files with 152 additions and 419 deletions

View File

@ -1,4 +1,4 @@
# Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
@ -37,7 +37,7 @@ include $(MY_WEBRTC_ROOT_PATH)/src/system_wrappers/source/Android.mk
include $(MY_WEBRTC_ROOT_PATH)/src/voice_engine/main/source/Android.mk
# video
include $(MY_WEBRTC_ROOT_PATH)/src/common_video/jpeg/main/source/Android.mk
include $(MY_WEBRTC_ROOT_PATH)/src/common_video/jpeg/Android.mk
include $(MY_WEBRTC_ROOT_PATH)/src/common_video/libyuv/Android.mk
include $(MY_WEBRTC_ROOT_PATH)/src/modules/video_capture/main/source/Android.mk
include $(MY_WEBRTC_ROOT_PATH)/src/modules/video_coding/codecs/i420/main/source/Android.mk

View File

@ -10,6 +10,6 @@
'includes': [
'../common_settings.gypi', # Common settings
'libyuv/libyuv.gypi',
'jpeg/main/source/jpeg.gypi',
'jpeg/jpeg.gypi',
],
}

View File

@ -1,4 +1,4 @@
# Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
@ -10,7 +10,7 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include $(LOCAL_PATH)/../../../../../android-webrtc.mk
include $(LOCAL_PATH)/../../../android-webrtc.mk
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_MODULE := libwebrtc_jpeg
@ -25,11 +25,9 @@ LOCAL_CFLAGS := \
$(MY_WEBRTC_COMMON_DEFS)
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/include \
$(LOCAL_PATH)/../interface \
$(LOCAL_PATH)/../../../.. \
$(LOCAL_PATH)/../../../vplib/main/interface \
$(LOCAL_PATH)/../../../interface \
$(LOCAL_PATH)/../../../../../../ \
$(LOCAL_PATH)/../../../../ \
external/jpeg
LOCAL_SHARED_LIBRARIES := \

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
@ -8,11 +8,7 @@
* be found in the AUTHORS file in the root of the source tree.
*/
/*
* data_manager.cc
*/
#include "data_manager.h"
#include "common_video/jpeg/data_manager.h"
namespace webrtc
{

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
@ -8,10 +8,6 @@
* be found in the AUTHORS file in the root of the source tree.
*/
/*
* JPEG wrapper
*/
#ifndef WEBRTC_COMMON_VIDEO_JPEG
#define WEBRTC_COMMON_VIDEO_JPEG

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
@ -8,11 +8,6 @@
* be found in the AUTHORS file in the root of the source tree.
*/
/*
* jpeg.cc
*/
#if defined(WIN32)
#include <basetsd.h>
#endif
@ -20,8 +15,9 @@
#include <stdio.h>
#include <string.h>
#include "jpeg.h"
#include "data_manager.h"
#include "common_video/jpeg/include/jpeg.h"
#include "common_video/jpeg/data_manager.h"
extern "C" {
#if defined(USE_SYSTEM_LIBJPEG)
#include <jpeglib.h>

View File

@ -1,4 +1,4 @@
# Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
# Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
#
# Use of this source code is governed by a BSD-style license
# that can be found in the LICENSE file in the root of the source
@ -18,14 +18,14 @@
'webrtc_libyuv',
],
'include_dirs': [
'../../../interface',
'../interface',
'../../../../../../',
'include',
'<(webrtc_root)',
'<(webrtc_root)/common_video/interface',
],
'direct_dependent_settings': {
'include_dirs': [
'../interface',
'../../../interface',
'include',
'<(webrtc_root)/common_video/interface',
],
},
'conditions': [
@ -55,7 +55,7 @@
}],
],
'sources': [
'../interface/jpeg.h',
'include/jpeg.h',
'data_manager.cc',
'data_manager.h',
'jpeg.cc',
@ -67,17 +67,15 @@
['build_with_chromium==0', {
'targets': [
{
'target_name': 'jpeg_test',
'target_name': 'jpeg_unittests',
'type': 'executable',
'dependencies': [
'webrtc_jpeg',
],
'include_dirs': [
'../interface',
'../source',
'<(webrtc_root)/../testing/gtest.gyp:gtest',
'<(webrtc_root)/../test/test.gyp:test_support_main',
],
'sources': [
'../test/test_jpeg.cc',
'jpeg_unittest.cc',
],
},
] # targets

View File

@ -0,0 +1,123 @@
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include <cstdio>
#include <string>
#include "common_video/jpeg/include/jpeg.h"
#include "common_video/interface/video_image.h"
#include "gtest/gtest.h"
#include "testsupport/fileutils.h"
namespace webrtc {
const unsigned int kImageWidth = 640;
const unsigned int kImageHeight = 480;
class JpegTest: public testing::Test {
protected:
JpegTest()
: input_filename_(webrtc::test::ProjectRootPath() +
"test/data/common_video/jpeg/webrtc_logo.jpg"),
decoded_filename_(webrtc::test::OutputPath() + "TestJpegDec.yuv"),
encoded_filename_(webrtc::test::OutputPath() + "TestJpegEnc.jpg"),
encoded_buffer_(NULL) {}
virtual ~JpegTest() {}
void SetUp() {
encoder_ = new JpegEncoder();
decoder_ = new JpegDecoder();
}
void TearDown() {
if (encoded_buffer_ != NULL) {
if (encoded_buffer_->_buffer != NULL) {
delete [] encoded_buffer_->_buffer;
}
delete encoded_buffer_;
}
delete encoder_;
delete decoder_;
}
// Reads an encoded image. Caller will have to deallocate the memory of this
// object and it's _buffer byte array.
EncodedImage* ReadEncodedImage(std::string input_filename) {
FILE* open_file = fopen(input_filename.c_str(), "rb");
assert(open_file != NULL);
size_t length = webrtc::test::GetFileSize(input_filename);
EncodedImage* encoded_buffer = new EncodedImage();
encoded_buffer->_buffer = new WebRtc_UWord8[length];
encoded_buffer->_size = length;
encoded_buffer->_length = length;
if (fread(encoded_buffer->_buffer, 1, length, open_file) != length) {
ADD_FAILURE() << "Error reading file:" << input_filename;
}
fclose(open_file);
return encoded_buffer;
}
std::string input_filename_;
std::string decoded_filename_;
std::string encoded_filename_;
EncodedImage* encoded_buffer_;
JpegEncoder* encoder_;
JpegDecoder* decoder_;
};
TEST_F(JpegTest, Decode) {
encoded_buffer_ = ReadEncodedImage(input_filename_);
RawImage image_buffer;
EXPECT_EQ(0, decoder_->Decode(*encoded_buffer_, image_buffer));
EXPECT_GT(image_buffer._length, 0u);
EXPECT_EQ(kImageWidth, image_buffer._width);
EXPECT_EQ(kImageHeight, image_buffer._height);
delete [] image_buffer._buffer;
}
TEST_F(JpegTest, EncodeInvalidInputs) {
RawImage empty;
empty._width = 164;
empty._height = 164;
EXPECT_EQ(-1, encoder_->SetFileName(0));
EXPECT_EQ(-1, encoder_->Encode(empty));
empty._buffer = new WebRtc_UWord8[10];
empty._size = 0;
EXPECT_EQ(-1, encoder_->Encode(empty));
empty._size = 10;
empty._height = 0;
EXPECT_EQ(-1, encoder_->Encode(empty));
empty._height = 164;
empty._width = 0;
EXPECT_EQ(-1, encoder_->Encode(empty));
delete[] empty._buffer;
}
TEST_F(JpegTest, Encode) {
// Decode our input image then encode it again to a new file:
encoded_buffer_ = ReadEncodedImage(input_filename_);
RawImage image_buffer;
EXPECT_EQ(0, decoder_->Decode(*encoded_buffer_, image_buffer));
EXPECT_EQ(0, encoder_->SetFileName(encoded_filename_.c_str()));
EXPECT_EQ(0, encoder_->Encode(image_buffer));
// Save decoded image to file.
FILE* save_file = fopen(decoded_filename_.c_str(), "wb");
fwrite(image_buffer._buffer, 1, image_buffer._length, save_file);
fclose(save_file);
delete[] image_buffer._buffer;
}
} // namespace webrtc

View File

@ -1,161 +0,0 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
// system includes
#include <assert.h>
#include <string.h> // memcpy
#include "test_buffer.h"
#include "common_video/libyuv/include/libyuv.h"
TestBuffer::TestBuffer():
_buffer(0),
_bufferSize(0),
_bufferLength(0),
_width(0),
_height(0)
{
//
}
TestBuffer::~TestBuffer()
{
_bufferLength = 0;
_bufferSize = 0;
if(_buffer)
{
delete [] _buffer;
_buffer = 0;
}
}
TestBuffer::TestBuffer(const TestBuffer& rhs)
:
_bufferLength(rhs._bufferLength),
_bufferSize(rhs._bufferSize),
_height(rhs._height),
_width(rhs._width),
_buffer(0)
{
// make sure that our buffer is big enough
_buffer = new WebRtc_UWord8[_bufferSize];
// only copy required length
memcpy(_buffer, rhs._buffer, _bufferLength);
}
WebRtc_UWord32
TestBuffer::GetWidth() const
{
return _width;
}
WebRtc_UWord32
TestBuffer::GetHeight() const
{
return _height;
}
void
TestBuffer::SetWidth(WebRtc_UWord32 width)
{
_width = width;
}
void
TestBuffer::SetHeight(WebRtc_UWord32 height)
{
_height = height;
}
void
TestBuffer::Free()
{
_bufferLength = 0;
_bufferSize = 0;
_height = 0;
_width = 0;
if(_buffer)
{
delete [] _buffer;
_buffer = 0;
}
}
void
TestBuffer::VerifyAndAllocate(WebRtc_UWord32 minimumSize)
{
if(minimumSize > _bufferSize)
{
// make sure that our buffer is big enough
WebRtc_UWord8 * newBufferBuffer = new WebRtc_UWord8[minimumSize];
if(_buffer)
{
// copy the old data
memcpy(newBufferBuffer, _buffer, _bufferSize);
delete [] _buffer;
}
_buffer = newBufferBuffer;
_bufferSize = minimumSize;
}
}
void
TestBuffer::UpdateLength(WebRtc_UWord32 newLength)
{
assert(newLength <= _bufferSize);
_bufferLength = newLength;
}
void
TestBuffer::CopyBuffer(WebRtc_UWord32 length, const WebRtc_UWord8* buffer)
{
assert(length <= _bufferSize);
memcpy(_buffer, buffer, length);
_bufferLength = length;
}
void
TestBuffer::CopyBuffer(TestBuffer& fromVideoBuffer)
{
assert(fromVideoBuffer.GetLength() <= _bufferSize);
assert(fromVideoBuffer.GetSize() <= _bufferSize);
_bufferLength = fromVideoBuffer.GetLength();
_height = fromVideoBuffer.GetHeight();
_width = fromVideoBuffer.GetWidth();
memcpy(_buffer, fromVideoBuffer.GetBuffer(), fromVideoBuffer.GetLength());
}
void
TestBuffer::Set(WebRtc_UWord8* tempBuffer,WebRtc_UWord32 tempSize, WebRtc_UWord32 tempLength)
{
_buffer = tempBuffer;
_bufferSize = tempSize;
_bufferLength = tempLength;
}
WebRtc_UWord8*
TestBuffer::GetBuffer() const
{
return _buffer;
}
WebRtc_UWord32
TestBuffer::GetSize() const
{
return _bufferSize;
}
WebRtc_UWord32
TestBuffer::GetLength() const
{
return _bufferLength;
}

View File

@ -1,77 +0,0 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#ifndef WEBRTC_COMMON_VIDEO_JPEG_TEST_BUFFER_H
#define WEBRTC_COMMON_VIDEO_JPEG_TEST_BUFFER_H
#include "typedefs.h"
class TestBuffer
{
public:
TestBuffer();
virtual ~TestBuffer();
TestBuffer(const TestBuffer& rhs);
/**
* Verifies that current allocated buffer size is larger than or equal to the input size.
* If the current buffer size is smaller, a new allocation is made and the old buffer data is copied to the new buffer.
*/
void VerifyAndAllocate(WebRtc_UWord32 minimumSize);
void UpdateLength(WebRtc_UWord32 newLength);
void CopyBuffer(WebRtc_UWord32 length, const WebRtc_UWord8* fromBuffer);
void CopyBuffer(TestBuffer& fromBuffer);
void Free(); // Deletes frame buffer and resets members to zero
/**
* Gets pointer to frame buffer
*/
WebRtc_UWord8* GetBuffer() const;
/**
* Gets allocated buffer size
*/
WebRtc_UWord32 GetSize() const;
/**
* Gets length of frame
*/
WebRtc_UWord32 GetLength() const;
WebRtc_UWord32 GetWidth() const;
WebRtc_UWord32 GetHeight() const;
void SetWidth(WebRtc_UWord32 width);
void SetHeight(WebRtc_UWord32 height);
private:
// TestBuffer& operator=(const TestBuffer& inBuffer);
private:
void Set(WebRtc_UWord8* buffer,WebRtc_UWord32 size,WebRtc_UWord32 length);
WebRtc_UWord8* _buffer; // Pointer to frame buffer
WebRtc_UWord32 _bufferSize; // Allocated buffer size
WebRtc_UWord32 _bufferLength; // Length (in bytes) of frame
WebRtc_UWord32 _width;
WebRtc_UWord32 _height;
};
#endif

View File

@ -1,136 +0,0 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
/*
* test_jpeg.cc
*/
#include <cassert>
#include <iostream>
#include <cmath>
#include <string>
#include <stdio.h>
#include "video_image.h"
#include "jpeg.h"
using namespace webrtc;
int
main(int argc, char **argv)
{
if (argc < 1)
{
return -1;
}
std::string fileName = argv[1];
const char* fileNameDec = "TestJpegDec.yuv";
const char* fileNameEnc = "TestJpegEnc.jpg";
std::string str;
std::cout << "---------------------" << std::endl;
std::cout << "----- Test JPEG -----" << std::endl;
std::cout << "---------------------" << std::endl;
std::cout << " " << std::endl;
JpegDecoder* JpgDecPtr = new JpegDecoder( );
// Open input file
FILE* openFile = fopen(fileName.c_str(), "rb");
assert(openFile != NULL);
// Get file length
fseek(openFile, 0, SEEK_END);
size_t length = ftell(openFile);
fseek(openFile, 0, SEEK_SET);
// Read input file to buffer
EncodedImage encodedBuffer;
encodedBuffer._buffer = new WebRtc_UWord8[length];
encodedBuffer._size = length;
encodedBuffer._length = length;
if (fread(encodedBuffer._buffer, 1, length, openFile) != length)
{
printf("Error reading file %s\n", fileName.c_str());
return 1;
}
fclose(openFile);
// ------------------
// Decode
// ------------------
RawImage imageBuffer;
int error = JpgDecPtr->Decode(encodedBuffer, imageBuffer);
std::cout << error << " = Decode(" << fileName.c_str() << ", "
"(" << imageBuffer._width <<
"x" << imageBuffer._height << "))" << std::endl;
if (error == 0)
{
// Save decoded image to file
FILE* saveFile = fopen(fileNameDec, "wb");
fwrite(imageBuffer._buffer, 1, imageBuffer._length, saveFile);
fclose(saveFile);
// ------------------
// Encode
// ------------------
JpegEncoder* JpegEncoderPtr = new JpegEncoder();
// Test invalid inputs
RawImage empty;
empty._width = 164;
empty._height = 164;
int error = JpegEncoderPtr->SetFileName(0);
assert(error == -1);
error = JpegEncoderPtr->Encode(empty);
assert(error == -1);
empty._buffer = new WebRtc_UWord8[10];
empty._size = 0;
error = JpegEncoderPtr->Encode(empty);
assert(error == -1);
empty._size = 10;
empty._height = 0;
error = JpegEncoderPtr->Encode(empty);
assert(error == -1);
empty._height = 164;
empty._width = 0;
error = JpegEncoderPtr->Encode(empty);
assert(error == -1);
error = JpegEncoderPtr->SetFileName(fileNameEnc);
assert(error == 0);
delete [] empty._buffer;
// Actual Encode
error = JpegEncoderPtr->Encode(imageBuffer);
assert(error == 0);
std::cout << error << " = Encode(" << fileNameDec << ")" << std::endl;
delete JpegEncoderPtr;
}
delete [] imageBuffer._buffer;
delete [] encodedBuffer._buffer;
delete JpgDecPtr;
std::cout << "Verify that the encoded and decoded images look correct."
<< std::endl;
std::cout << "Press enter to quit test...";
std::getline(std::cin, str);
return 0;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
@ -18,7 +18,7 @@
#include <stdio.h>
#include "common_video/interface/video_image.h"
#include "common_video/jpeg/main/interface/jpeg.h"
#include "common_video/jpeg/include/jpeg.h"
#include "system_wrappers/interface/trace.h"
namespace webrtc {

View File

@ -13,7 +13,7 @@
#include "video_engine/vie_file_impl.h"
#ifdef WEBRTC_VIDEO_ENGINE_FILE_API
#include "common_video/jpeg/main/interface/jpeg.h"
#include "common_video/jpeg/include/jpeg.h"
#include "system_wrappers/interface/condition_variable_wrapper.h"
#include "system_wrappers/interface/critical_section_wrapper.h"
#include "system_wrappers/interface/trace.h"

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.9 KiB