Browse Subversion Repository
Contents of /Quantizer.cpp
Parent Directory
| Revision Log
Revision 18 -
( show annotations)
( download)
( as text)
Mon Oct 11 13:26:23 2010 UTC
(13 years, 7 months ago)
by berupon
File MIME type: text/x-c++src
File size: 2816 byte(s)
GUIで、圧縮設定を調整して結果を確認出来るアプリケーションの作成開始。
| 1 |
#include "stdafx.h" |
| 2 |
#include "Quantizer.h" |
| 3 |
|
| 4 |
static const int QUANT8_SCAN[16] = { |
| 5 |
0,3,4,3, |
| 6 |
3,1,5,1, |
| 7 |
4,5,2,5, |
| 8 |
3,1,5,1 |
| 9 |
}; |
| 10 |
|
| 11 |
static const int DEQUANT8_SCALE[6][6] = { |
| 12 |
{ 20, 18, 32, 19, 25, 24 }, |
| 13 |
{ 22, 19, 35, 21, 28, 26 }, |
| 14 |
{ 26, 23, 42, 24, 33, 31 }, |
| 15 |
{ 28, 25, 45, 26, 35, 33 }, |
| 16 |
{ 32, 28, 51, 30, 40, 38 }, |
| 17 |
{ 36, 32, 58, 34, 46, 43 }, |
| 18 |
}; |
| 19 |
static const int QUANT8_SCALE[6][6] = { |
| 20 |
{ 13107, 11428, 20972, 12222, 16777, 15481 }, |
| 21 |
{ 11916, 10826, 19174, 11058, 14980, 14290 }, |
| 22 |
{ 10082, 8943, 15978, 9675, 12710, 11985 }, |
| 23 |
{ 9362, 8228, 14913, 8931, 11984, 11259 }, |
| 24 |
{ 8192, 7346, 13159, 7740, 10486, 9777 }, |
| 25 |
{ 7282, 6428, 11570, 6830, 9118, 8640 } |
| 26 |
}; |
| 27 |
|
| 28 |
static const unsigned char QUANT8_INTRA_MATRIX[8][8] = { |
| 29 |
6, 10, 13, 16, 18, 23, 25, 27, |
| 30 |
10, 11, 16, 18, 23, 25, 27, 29, |
| 31 |
13, 16, 18, 23, 25, 27, 29, 31, |
| 32 |
16, 18, 23, 25, 27, 29, 31, 33, |
| 33 |
18, 23, 25, 27, 29, 31, 33, 36, |
| 34 |
23, 25, 27, 29, 31, 33, 36, 38, |
| 35 |
25, 27, 29, 31, 33, 36, 38, 40, |
| 36 |
27, 29, 31, 33, 36, 38, 40, 42, |
| 37 |
}; |
| 38 |
|
| 39 |
namespace impl { |
| 40 |
|
| 41 |
int div(int n, int d) |
| 42 |
{ |
| 43 |
return (n + (d>>1)) / d; |
| 44 |
} |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
void Quantizer::init(size_t qp, size_t hBlockness, size_t vBlockness, bool useQuantMatrix) |
| 49 |
{ |
| 50 |
QP = qp; |
| 51 |
QP_shift = QP / 6; |
| 52 |
QP_remain = QP % 6; |
| 53 |
|
| 54 |
for (size_t i=0; i<64; ++i) { |
| 55 |
int j = QUANT8_SCAN[((i>>1)&12) | (i&3)]; |
| 56 |
// printf("%d,", j); |
| 57 |
quant8_table[i] = QUANT8_SCALE[QP_remain][j]; |
| 58 |
dequant8_table[i] = DEQUANT8_SCALE[QP_remain][j] << QP_shift; |
| 59 |
} |
| 60 |
|
| 61 |
// ���q���e�[�u������ |
| 62 |
for (size_t i=0; i<8; ++i) { |
| 63 |
if (i>=(8-hBlockness)) { |
| 64 |
for (size_t j=0; j<8; ++j) { |
| 65 |
quant8_table[j*8+i] = 0; |
| 66 |
} |
| 67 |
} |
| 68 |
if (i>=(8-vBlockness)) { |
| 69 |
for (size_t j=0; j<8; ++j) { |
| 70 |
quant8_table[i*8+j] = 0; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// �����g���r���������q���}�g���N�X���K�p |
| 76 |
if (useQuantMatrix) { |
| 77 |
for (size_t i=0; i<8; ++i) { |
| 78 |
for (size_t j=0; j<8; ++j) { |
| 79 |
quant8_table[i*8+j] = impl::div(quant8_table[i*8+j] << 4, QUANT8_INTRA_MATRIX[i][j]); |
| 80 |
dequant8_table[i*8+j] *= QUANT8_INTRA_MATRIX[i][j]; |
| 81 |
} |
| 82 |
} |
| 83 |
}else { |
| 84 |
for (size_t i=0; i<8*8; ++i) { |
| 85 |
quant8_table[i] = quant8_table[i]; |
| 86 |
dequant8_table[i] <<= 4; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
} |
| 91 |
|
| 92 |
void Quantizer::quantize(int values[8][8]) |
| 93 |
{ |
| 94 |
for (size_t i=0; i<8; ++i) { |
| 95 |
for (size_t j=0; j<8; ++j) { |
| 96 |
int value = values[i][j]; |
| 97 |
if (value == 0) { |
| 98 |
continue; |
| 99 |
}else if (value > 0) { |
| 100 |
value += (1 << 5); |
| 101 |
}else { |
| 102 |
value -= (1 << 5); |
| 103 |
} |
| 104 |
value >>= 6; |
| 105 |
value *= quant8_table[i*8+j]; |
| 106 |
value += (1 << (11+QP_shift)); |
| 107 |
value >>= (12+QP_shift); |
| 108 |
// assert(value >= -32768 && value <= 32767); |
| 109 |
values[i][j] = value; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void Quantizer::dequantize(int values[8][8]) |
| 115 |
{ |
| 116 |
for (size_t i=0; i<8; ++i) { |
| 117 |
for (size_t j=0; j<8; ++j) { |
| 118 |
values[i][j] *= dequant8_table[i*8+j]; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| |