[cvs] / xvidcore / src / quant / adapt_quant.c Repository:
ViewVC logotype

Annotation of /xvidcore/src/quant/adapt_quant.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.11 - (view) (download)

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

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4