Master commit of OpenFace.

This commit is contained in:
unknown
2016-04-28 15:40:36 -04:00
parent 5346d303ab
commit 57e58a6949
4406 changed files with 1441342 additions and 0 deletions

View File

@@ -0,0 +1,236 @@
// Copyright (C) 2005 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_LZP_BUFFER_KERNEl_1_
#define DLIB_LZP_BUFFER_KERNEl_1_
#include "../algs.h"
#include "lzp_buffer_kernel_abstract.h"
namespace dlib
{
template <
typename sbuf
>
class lzp_buffer_kernel_1
{
/*!
REQUIREMENTS ON sbuf
sbuf is an implementation of sliding_buffer/sliding_buffer_kernel_abstract.h
T == unsigned char
INITIAL VALUE
- buffer.size() == the size as defined by the constructor
- table_size == the number of elements in the table array
- for all i: buffer[i] == 0
- for all i: table[i] == buffer.size()
CONVENTION
- table_size == the number of elements in the table array
- size() == buffer.size()
- operator[](i) == buffer[i]
- if (table[hash()] != buffer.size()) then
- buffer.get_element_index(table[hash()]) == the index we will
predict for the current context
- else
- there is no prediction for the current context
- last_element == buffer.size()-1
This is LZP with just an order-3 model without context confirmation.
!*/
public:
explicit lzp_buffer_kernel_1 (
unsigned long buffer_size
);
virtual ~lzp_buffer_kernel_1 (
);
void clear(
);
inline void add (
unsigned char symbol
);
inline unsigned long predict_match (
unsigned long& index
);
inline unsigned long size (
) const;
inline unsigned char operator[] (
unsigned long index
) const;
private:
inline unsigned long hash (
) const
/*!
ensures
- returns a hash computed from the current context. This hash
is always in the range for table.
!*/
{
unsigned long temp = buffer[0];
temp <<= 16;
unsigned long temp2 = buffer[1];
temp2 <<= 8;
unsigned long temp3 = buffer[2];
temp = temp|temp2|temp3;
temp = ((temp>>11)^temp)&0xFFFF;
return temp;
}
sbuf buffer;
const unsigned long table_size;
unsigned long* const table;
unsigned long last_element;
// restricted functions
lzp_buffer_kernel_1(const lzp_buffer_kernel_1<sbuf>&); // copy constructor
lzp_buffer_kernel_1<sbuf>& operator=(const lzp_buffer_kernel_1<sbuf>&); // assignment operator
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
lzp_buffer_kernel_1<sbuf>::
lzp_buffer_kernel_1 (
unsigned long buffer_size
) :
table_size(65536),
table(new unsigned long[table_size])
{
buffer.set_size(buffer_size);
for (unsigned long i = 0; i < buffer.size(); ++i)
buffer[i] = 0;
for (unsigned long i = 0; i < table_size; ++i)
table[i] = buffer.size();
last_element = buffer.size()-1;
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
lzp_buffer_kernel_1<sbuf>::
~lzp_buffer_kernel_1 (
)
{
delete [] table;
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
void lzp_buffer_kernel_1<sbuf>::
clear(
)
{
for (unsigned long i = 0; i < buffer.size(); ++i)
buffer[i] = 0;
for (unsigned long i = 0; i < table_size; ++i)
table[i] = buffer.size();
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
void lzp_buffer_kernel_1<sbuf>::
add (
unsigned char symbol
)
{
buffer.rotate_left(1);
buffer[0] = symbol;
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
unsigned long lzp_buffer_kernel_1<sbuf>::
predict_match (
unsigned long& index
)
{
const unsigned long i = hash();
if (table[i] != buffer.size())
{
index = buffer.get_element_index(table[i]);
if (index > 20)
{
// update the prediction for this context
table[i] = buffer.get_element_id(last_element);
}
return 3;
}
else
{
// update the prediction for this context
table[i] = buffer.get_element_id(last_element);
return 0;
}
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
unsigned long lzp_buffer_kernel_1<sbuf>::
size (
) const
{
return buffer.size();
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
unsigned char lzp_buffer_kernel_1<sbuf>::
operator[] (
unsigned long index
) const
{
return buffer[index];
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LZP_BUFFER_KERNEl_1_

View File

@@ -0,0 +1,319 @@
// Copyright (C) 2005 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_LZP_BUFFER_KERNEl_2_
#define DLIB_LZP_BUFFER_KERNEl_2_
#include "../algs.h"
#include "lzp_buffer_kernel_abstract.h"
#include <new>
namespace dlib
{
template <
typename sbuf
>
class lzp_buffer_kernel_2
{
/*!
REQUIREMENTS ON sbuf
sbuf is an implementation of sliding_buffer/sliding_buffer_kernel_abstract.h
T == unsigned char
INITIAL VALUE
- buffer.size() == the size as defined by the constructor
- table_size == the number of elements in the table3 and table4 arrays
- for all i: buffer[i] == 0
- for all i: table3[i] == buffer.size()
- for all i: table4[i] == buffer.size()
CONVENTION
- table_size == the number of elements in the table3 and table4 arrays
- size() == buffer.size()
- operator[](i) == buffer[i]
- last_element == buffer.size()-1
This is LZP with an order-5-4-3 model with context confirmation.
To save memory the order5 and order3 predictions exist in the same
table, that is, table3.
!*/
public:
explicit lzp_buffer_kernel_2 (
unsigned long buffer_size
);
virtual ~lzp_buffer_kernel_2 (
);
void clear(
);
inline void add (
unsigned char symbol
);
inline unsigned long predict_match (
unsigned long& index
);
inline unsigned long size (
) const;
inline unsigned char operator[] (
unsigned long index
) const;
private:
inline bool verify (
unsigned long index
) const
/*!
ensures
- returns true if buffer[index]'s context matches the current context
!*/
{
if (index+3 < buffer.size())
{
if (buffer[0] != buffer[index+1])
return false;
if (buffer[1] != buffer[index+2])
return false;
if (buffer[2] != buffer[index+3])
return false;
return true;
}
else
{
// just call this a match
return true;
}
}
sbuf buffer;
unsigned long* table3;
unsigned long* table4;
unsigned long last_element;
const unsigned long table_size;
// restricted functions
lzp_buffer_kernel_2(const lzp_buffer_kernel_2<sbuf>&); // copy constructor
lzp_buffer_kernel_2<sbuf>& operator=(const lzp_buffer_kernel_2<sbuf>&); // assignment operator
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
lzp_buffer_kernel_2<sbuf>::
lzp_buffer_kernel_2 (
unsigned long buffer_size
) :
table3(0),
table4(0),
table_size(65536)
{
buffer.set_size(buffer_size);
table3 = new (std::nothrow) unsigned long[table_size];
table4 = new (std::nothrow) unsigned long[table_size];
if (!table3 || !table4)
{
if (!table3)
delete [] table3;
if (!table4)
delete [] table4;
throw std::bad_alloc();
}
for (unsigned long i = 0; i < buffer.size(); ++i)
buffer[i] = 0;
for (unsigned long i = 0; i < table_size; ++i)
{
table3[i] = buffer.size();
table4[i] = buffer.size();
}
last_element = buffer.size()-1;
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
lzp_buffer_kernel_2<sbuf>::
~lzp_buffer_kernel_2 (
)
{
delete [] table3;
delete [] table4;
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
void lzp_buffer_kernel_2<sbuf>::
clear(
)
{
for (unsigned long i = 0; i < buffer.size(); ++i)
buffer[i] = 0;
for (unsigned long i = 0; i < table_size; ++i)
{
table3[i] = buffer.size();
table4[i] = buffer.size();
}
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
void lzp_buffer_kernel_2<sbuf>::
add (
unsigned char symbol
)
{
buffer.rotate_left(1);
buffer[0] = symbol;
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
unsigned long lzp_buffer_kernel_2<sbuf>::
predict_match (
unsigned long& index
)
{
unsigned long temp1 = buffer[0];
unsigned long temp2 = buffer[1];
temp2 <<= 8;
unsigned long temp3 = buffer[2];
temp3 <<= 16;
unsigned long temp4 = buffer[3];
temp4 <<= 24;
unsigned long temp5 = buffer[4];
temp5 <<= 12;
unsigned long context1 = temp1|temp2|temp3;
unsigned long context2 = context1|temp4;
const unsigned long i5 = ((temp5|(context2>>20))^context2)&0xFFFF;
const unsigned long i4 = ((context2>>15)^context2)&0xFFFF;
const unsigned long i3 = ((context1>>11)^context1)&0xFFFF;
// check the 5-order context's prediction
if (table3[i5] != buffer.size() &&
verify(buffer.get_element_index(table3[i5])) )
{
index = buffer.get_element_index(table3[i5]);
if (index > 20)
{
// update the prediction for this context
table3[i3] = buffer.get_element_id(last_element);
table4[i4] = table3[i3];
table3[i5] = table3[i3];
}
return 5;
}
// check the 4-order context's prediction
else if (table4[i4] != buffer.size() &&
verify(buffer.get_element_index(table4[i4])) )
{
index = buffer.get_element_index(table4[i4]);
if (index > 20)
{
// update the prediction for this context
table3[i3] = buffer.get_element_id(last_element);
table4[i4] = table3[i3];
table3[i5] = table3[i3];
}
return 4;
}
// check the 3-order context's prediction
else if (table3[i3] != buffer.size() &&
verify(buffer.get_element_index(table3[i3])))
{
index = buffer.get_element_index(table3[i3]);
if (index > 20)
{
// update the prediction for this context
table3[i3] = buffer.get_element_id(last_element);
table4[i4] = table3[i3];
table3[i5] = table3[i3];
}
return 3;
}
else
{
// update the prediction for this context
table3[i3] = buffer.get_element_id(last_element);
table4[i4] = table3[i3];
table3[i5] = table3[i3];
return 0;
}
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
unsigned long lzp_buffer_kernel_2<sbuf>::
size (
) const
{
return buffer.size();
}
// ----------------------------------------------------------------------------------------
template <
typename sbuf
>
unsigned char lzp_buffer_kernel_2<sbuf>::
operator[] (
unsigned long index
) const
{
return buffer[index];
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LZP_BUFFER_KERNEl_2_

View File

@@ -0,0 +1,130 @@
// Copyright (C) 2005 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_LZP_BUFFER_KERNEl_ABSTRACT_
#ifdef DLIB_LZP_BUFFER_KERNEl_ABSTRACT_
#include "../algs.h"
namespace dlib
{
class lzp_buffer
{
/*!
INITIAL VALUE
size() == some value defined by the constructor argument
Initially this object is at some predefined empty or ground state.
for all i: (*this)[i] == 0
WHAT THIS OBJECT REPRESENTS
This object represents some varation on the LZP algorithm
described by Charles Bloom in his paper "LZP: a new data
compression algorithm"
The LZP algorithm is a lot like lz77 except there is no need to pass
the location of matches in the history buffer to the decoder because
LZP uses the data it has already seen to predict the location of the
next match.
NOTE
The add() and predict_match() functions must be called in the same
order by the coder and decoder. If they aren't the state of the
lzp_buffer objects in the coder and decoder may differ and the decoder
won't be able to correctly decode the data stream.
!*/
public:
explicit lzp_buffer (
unsigned long buffer_size
);
/*!
requires
- 10 < buffer_size < 32
ensures
- #*this is properly initialized
- #size() == 2^buffer_size
throws
- std::bad_alloc
!*/
virtual ~lzp_buffer (
);
/*!
ensures
- any resources associated with *this have been released
!*/
void clear(
);
/*!
ensures
- #*this has its initial value
throws
- std::bad_alloc
if this exception is thrown then #*this is unusable
until clear() is called and succeeds
!*/
void add (
unsigned char symbol
);
/*!
ensures
- shifts everything in the history buffer left 1.
(i.e. #(*this)[i+1] == (*this)[i])
- #(*this)[0] == symbol
throws
- std::bad_alloc
if this exception is thrown then #*this is unusable
until clear() is called and succeeds
!*/
unsigned long predict_match (
unsigned long& index
);
/*!
ensures
- updates the prediction for the current context.
(the current context is the last few symbols seen. i.e. (*this)[0],
(*this)[1], etc.)
- if (*this can generate a prediction) then
- #index == the predicted location of a match in the history buffer.
(i.e. (*this)[#index] is the first symbol of the predicted match)
- returns the order this prediction came from
- else
- returns 0
throws
- std::bad_alloc
if this exception is thrown then #*this is unusable
until clear() is called and succeeds
!*/
unsigned long size (
) const;
/*!
ensures
- returns the size of the history buffer
!*/
unsigned char operator[] (
unsigned long index
) const;
/*!
requires
- index < size()
ensures
- returns the symbol at the given index in the history buffer
!*/
private:
// restricted functions
lzp_buffer(const lzp_buffer&); // copy constructor
lzp_buffer& operator=(const lzp_buffer&); // assignment operator
};
}
#endif // DLIB_LZP_BUFFER_KERNEl_ABSTRACT_

View File

@@ -0,0 +1,101 @@
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_LZP_BUFFER_KERNEl_C_
#define DLIB_LZP_BUFFER_KERNEl_C_
#include "lzp_buffer_kernel_abstract.h"
#include "../algs.h"
#include "../assert.h"
#include <iostream>
namespace dlib
{
template <
typename lzp_base
>
class lzp_buffer_kernel_c : public lzp_base
{
public:
lzp_buffer_kernel_c (
unsigned long buffer_size
);
unsigned char operator[] (
unsigned long index
) const;
unsigned long make_safe (
unsigned long buffer_size
)
/*!
ensures
- if ( 10 < buffer_size < 32) then
- returns buffer_size
- else
- throws due to failed CASSERT
!*/
{
// make sure requires clause is not broken
DLIB_CASSERT( 10 < buffer_size && buffer_size < 32,
"\tlzp_buffer::lzp_buffer(unsigned long)"
<< "\n\tbuffer_size must be in the range 11 to 31."
<< "\n\tthis: " << this
<< "\n\tbuffer_size: " << buffer_size
);
return buffer_size;
}
};
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template <
typename lzp_base
>
unsigned char lzp_buffer_kernel_c<lzp_base>::
operator[] (
unsigned long index
) const
{
// make sure requires clause is not broken
DLIB_CASSERT( index < this->size(),
"\tunsigned char lzp_buffer::operator[](unsigned long) const"
<< "\n\tindex must be in the range 0 to size()()-1"
<< "\n\tthis: " << this
<< "\n\tsize(): " << this->size()
<< "\n\tindex: " << index
);
// call the real function
return lzp_base::operator[](index);
}
// ----------------------------------------------------------------------------------------
template <
typename lzp_base
>
lzp_buffer_kernel_c<lzp_base>::
lzp_buffer_kernel_c (
unsigned long buffer_size
) :
lzp_base(make_safe(buffer_size))
{
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LZP_BUFFER_KERNEl_C_