1 |
|
/***************************************************************************** |
2 |
|
* |
3 |
|
* XVID MPEG-4 VIDEO CODEC |
4 |
|
* - Adaptive quantization functions - |
5 |
|
* |
6 |
|
* Copyright(C) 2002 Peter Ross |
7 |
|
* |
8 |
|
* This program is an implementation of a part of one or more MPEG-4 |
9 |
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
10 |
|
* to use this software module in hardware or software products are |
11 |
|
* advised that its use may infringe existing patents or copyrights, and |
12 |
|
* any such use would be at such party's own risk. The original |
13 |
|
* developer of this software module and his/her company, and subsequent |
14 |
|
* editors and their companies, will have no liability for use of this |
15 |
|
* software or modifications or derivatives thereof. |
16 |
|
* |
17 |
|
* This program is free software ; you can redistribute it and/or modify |
18 |
|
* it under the terms of the GNU General Public License as published by |
19 |
|
* the Free Software Foundation ; either version 2 of the License, or |
20 |
|
* (at your option) any later version. |
21 |
|
* |
22 |
|
* This program is distributed in the hope that it will be useful, |
23 |
|
* but WITHOUT ANY WARRANTY ; without even the implied warranty of |
24 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
25 |
|
* GNU General Public License for more details. |
26 |
|
* |
27 |
|
* You should have received a copy of the GNU General Public License |
28 |
|
* along with this program ; if not, write to the Free Software |
29 |
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
30 |
|
* |
31 |
|
* $Id$ |
32 |
|
* |
33 |
|
****************************************************************************/ |
34 |
|
|
35 |
#include "../portab.h" |
#include "../portab.h" |
36 |
#include "adapt_quant.h" |
#include "adapt_quant.h" |
37 |
|
|
38 |
|
#include <stdlib.h> /* free, malloc */ |
39 |
|
|
40 |
#define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
#define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
41 |
#define RDIFF(a,b) ((int)(a+0.5)-(int)(b+0.5)) |
#define RDIFF(a,b) ((int)(a+0.5)-(int)(b+0.5)) |
42 |
|
|
43 |
int normalize_quantizer_field(float *in, int *out, int num, int min_quant, int max_quant) |
/***************************************************************************** |
44 |
|
* Functions |
45 |
|
****************************************************************************/ |
46 |
|
|
47 |
|
int |
48 |
|
normalize_quantizer_field(float *in, |
49 |
|
int *out, |
50 |
|
int num, |
51 |
|
int min_quant, |
52 |
|
int max_quant) |
53 |
{ |
{ |
54 |
int i; |
int i; |
55 |
int finished; |
int finished; |
56 |
|
|
57 |
do |
do { |
|
{ |
|
58 |
finished = 1; |
finished = 1; |
59 |
for(i = 1; i < num; i++) |
for (i = 1; i < num; i++) { |
60 |
{ |
if (RDIFF(in[i], in[i - 1]) > 2) { |
|
if(RDIFF(in[i], in[i-1]) > 2) |
|
|
{ |
|
61 |
in[i] -= (float) 0.5; |
in[i] -= (float) 0.5; |
62 |
finished = 0; |
finished = 0; |
63 |
} |
} else if (RDIFF(in[i], in[i - 1]) < -2) { |
|
else if(RDIFF(in[i], in[i-1]) < -2) |
|
|
{ |
|
64 |
in[i-1] -= (float) 0.5; |
in[i-1] -= (float) 0.5; |
65 |
finished = 0; |
finished = 0; |
66 |
} |
} |
67 |
|
|
68 |
if(in[i] > max_quant) |
if (in[i] > max_quant) { |
|
{ |
|
69 |
in[i] = (float) max_quant; |
in[i] = (float) max_quant; |
70 |
finished = 0; |
finished = 0; |
71 |
} |
} |
72 |
if(in[i] < min_quant) |
if (in[i] < min_quant) { |
|
{ |
|
73 |
in[i] = (float) min_quant; |
in[i] = (float) min_quant; |
74 |
finished = 0; |
finished = 0; |
75 |
} |
} |
76 |
if(in[i-1] > max_quant) |
if (in[i - 1] > max_quant) { |
|
{ |
|
77 |
in[i-1] = (float) max_quant; |
in[i-1] = (float) max_quant; |
78 |
finished = 0; |
finished = 0; |
79 |
} |
} |
80 |
if(in[i-1] < min_quant) |
if (in[i - 1] < min_quant) { |
|
{ |
|
81 |
in[i-1] = (float) min_quant; |
in[i-1] = (float) min_quant; |
82 |
finished = 0; |
finished = 0; |
83 |
} |
} |
91 |
return (int) (in[0] + 0.5); |
return (int) (in[0] + 0.5); |
92 |
} |
} |
93 |
|
|
94 |
int adaptive_quantization(unsigned char* buf, int stride, int* intquant, |
int |
95 |
int framequant, int min_quant, int max_quant, |
adaptive_quantization(unsigned char *buf, |
96 |
int mb_width, int mb_height) // no qstride because normalization |
int stride, |
97 |
|
int *intquant, |
98 |
|
int framequant, |
99 |
|
int min_quant, |
100 |
|
int max_quant, |
101 |
|
int mb_width, |
102 |
|
int mb_height) // no qstride because normalization |
103 |
{ |
{ |
104 |
int i,j,k,l; |
int i,j,k,l; |
105 |
|
|
106 |
static float *quant; |
float *quant; |
107 |
unsigned char *ptr; |
unsigned char *ptr; |
108 |
float *val; |
float *val; |
109 |
float global = 0.; |
float global = 0.; |
122 |
const float LowerLimit = 25; |
const float LowerLimit = 25; |
123 |
|
|
124 |
|
|
|
if(!quant) |
|
125 |
if(!(quant = (float *) malloc(mb_width*mb_height * sizeof(float)))) |
if(!(quant = (float *) malloc(mb_width*mb_height * sizeof(float)))) |
126 |
return -1; |
return(-1); |
127 |
|
|
128 |
val = (float *) malloc(mb_width*mb_height * sizeof(float)); |
if(!(val = (float *) malloc(mb_width * mb_height * sizeof(float)))) { |
129 |
|
free(quant); |
130 |
|
return(-1); |
131 |
|
} |
132 |
|
|
133 |
for(k = 0; k < mb_height; k++) |
for (k = 0; k < mb_height; k++) { |
|
{ |
|
134 |
for(l = 0;l < mb_width; l++) // do this for all macroblocks individually |
for(l = 0;l < mb_width; l++) // do this for all macroblocks individually |
135 |
{ |
{ |
136 |
quant[k*mb_width+l] = (float) framequant; |
quant[k*mb_width+l] = (float) framequant; |
146 |
val[k*mb_width+l] /= 256.; |
val[k*mb_width+l] /= 256.; |
147 |
global += val[k*mb_width+l]; |
global += val[k*mb_width+l]; |
148 |
|
|
149 |
if((val[k*mb_width+l] > LowerLimit) && (val[k*mb_width+l] < UpperLimit)) |
if ((val[k * mb_width + l] > LowerLimit) && |
150 |
|
(val[k * mb_width + l] < UpperLimit)) |
151 |
mid_range++; |
mid_range++; |
152 |
} |
} |
153 |
} |
} |
154 |
|
|
155 |
global /= mb_width*mb_height; |
global /= mb_width*mb_height; |
156 |
|
|
157 |
if((global < GlobalBrightThres) && (global > GlobalDarkThres) |
if (((global <GlobalBrightThres) &&(global >GlobalDarkThres)) |
158 |
|| (mid_range < MidRangeThres)) { |
|| (mid_range < MidRangeThres)) { |
159 |
for(k = 0; k < mb_height; k++) |
for (k = 0; k < mb_height; k++) { |
|
{ |
|
160 |
for(l = 0;l < mb_width; l++) // do this for all macroblocks individually |
for(l = 0;l < mb_width; l++) // do this for all macroblocks individually |
161 |
{ |
{ |
162 |
if(val[k*mb_width+l] < DarkThres) |
if(val[k*mb_width+l] < DarkThres) |
163 |
quant[k*mb_width+l] += DarkAmpl*(DarkThres-val[k*mb_width+l])/DarkThres; |
quant[k * mb_width + l] += |
164 |
|
DarkAmpl * (DarkThres - |
165 |
|
val[k * mb_width + l]) / DarkThres; |
166 |
else if (val[k*mb_width+l]>BrightThres) |
else if (val[k*mb_width+l]>BrightThres) |
167 |
quant[k*mb_width+l] += BrightAmpl*(val[k*mb_width+l]-BrightThres)/(255-BrightThres); |
quant[k * mb_width + l] += |
168 |
|
BrightAmpl * (val[k * mb_width + l] - |
169 |
|
BrightThres) / (255 - BrightThres); |
170 |
} |
} |
171 |
} |
} |
172 |
} |
} |
173 |
|
|
174 |
|
i = normalize_quantizer_field(quant, intquant, |
175 |
|
mb_width * mb_height, |
176 |
|
min_quant, max_quant); |
177 |
|
|
178 |
free(val); |
free(val); |
179 |
return normalize_quantizer_field(quant, intquant, mb_width*mb_height, min_quant, max_quant); |
free(quant); |
180 |
|
|
181 |
|
return(i); |
182 |
|
|
183 |
} |
} |