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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 443 - (view) (download)

1 : edgomez 443 /*****************************************************************************
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: adapt_quant.c,v 1.7 2002-09-07 12:51:38 edgomez Exp $
32 :     *
33 :     ****************************************************************************/
34 :    
35 : Isibaar 3 #include "../portab.h"
36 :     #include "adapt_quant.h"
37 :    
38 : edgomez 195 #include <stdlib.h> /* free, malloc */
39 : edgomez 18
40 : Isibaar 3 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
41 :     #define RDIFF(a,b) ((int)(a+0.5)-(int)(b+0.5))
42 :    
43 : edgomez 443 /*****************************************************************************
44 :     * Functions
45 :     ****************************************************************************/
46 :    
47 : edgomez 195 int
48 :     normalize_quantizer_field(float *in,
49 :     int *out,
50 :     int num,
51 :     int min_quant,
52 :     int max_quant)
53 : Isibaar 3 {
54 :     int i;
55 :     int finished;
56 : edgomez 195
57 :     do {
58 : Isibaar 3 finished = 1;
59 : edgomez 195 for (i = 1; i < num; i++) {
60 :     if (RDIFF(in[i], in[i - 1]) > 2) {
61 : Isibaar 3 in[i] -= (float) 0.5;
62 :     finished = 0;
63 : edgomez 195 } else if (RDIFF(in[i], in[i - 1]) < -2) {
64 :     in[i - 1] -= (float) 0.5;
65 : Isibaar 3 finished = 0;
66 :     }
67 : edgomez 195
68 :     if (in[i] > max_quant) {
69 : edgomez 18 in[i] = (float) max_quant;
70 :     finished = 0;
71 :     }
72 : edgomez 195 if (in[i] < min_quant) {
73 : edgomez 18 in[i] = (float) min_quant;
74 :     finished = 0;
75 :     }
76 : edgomez 195 if (in[i - 1] > max_quant) {
77 :     in[i - 1] = (float) max_quant;
78 : edgomez 18 finished = 0;
79 :     }
80 : edgomez 195 if (in[i - 1] < min_quant) {
81 :     in[i - 1] = (float) min_quant;
82 : edgomez 18 finished = 0;
83 :     }
84 : Isibaar 3 }
85 : edgomez 195 } while (!finished);
86 :    
87 : Isibaar 3 out[0] = 0;
88 :     for (i = 1; i < num; i++)
89 : edgomez 195 out[i] = RDIFF(in[i], in[i - 1]);
90 :    
91 : Isibaar 3 return (int) (in[0] + 0.5);
92 :     }
93 :    
94 : edgomez 195 int
95 :     adaptive_quantization(unsigned char *buf,
96 :     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 : Isibaar 3 {
104 : edgomez 195 int i, j, k, l;
105 :    
106 : edgomez 391 float *quant;
107 : Isibaar 3 unsigned char *ptr;
108 :     float *val;
109 : Isibaar 357 float global = 0.;
110 :     uint32_t mid_range = 0;
111 : Isibaar 3
112 : Isibaar 357 const float DarkAmpl = 14 / 2;
113 :     const float BrightAmpl = 10 / 2;
114 :     const float DarkThres = 70;
115 :     const float BrightThres = 200;
116 : Isibaar 3
117 : Isibaar 357 const float GlobalDarkThres = 60;
118 :     const float GlobalBrightThres = 170;
119 : Isibaar 3
120 : Isibaar 357 const float MidRangeThres = 20;
121 :     const float UpperLimit = 200;
122 :     const float LowerLimit = 25;
123 : Isibaar 3
124 : Isibaar 357
125 : edgomez 391 if (!(quant = (float *) malloc(mb_width * mb_height * sizeof(float))))
126 :     return(-1);
127 : Isibaar 3
128 : edgomez 391 if(!(val = (float *) malloc(mb_width * mb_height * sizeof(float))))
129 :     return(-1);
130 : Isibaar 3
131 : edgomez 195 for (k = 0; k < mb_height; k++) {
132 :     for (l = 0; l < mb_width; l++) // do this for all macroblocks individually
133 : Isibaar 3 {
134 : edgomez 195 quant[k * mb_width + l] = (float) framequant;
135 :    
136 : Isibaar 3 // calculate luminance-masking
137 : edgomez 195 ptr = &buf[16 * k * stride + 16 * l]; // address of MB
138 : Isibaar 3
139 : edgomez 195 val[k * mb_width + l] = 0.;
140 :    
141 :     for (i = 0; i < 16; i++)
142 :     for (j = 0; j < 16; j++)
143 :     val[k * mb_width + l] += ptr[i * stride + j];
144 :     val[k * mb_width + l] /= 256.;
145 : Isibaar 357 global +=val[k * mb_width + l];
146 :    
147 :     if ((val[k * mb_width + l] > LowerLimit) &&
148 :     (val[k * mb_width + l] < UpperLimit))
149 :     mid_range++;
150 : Isibaar 3 }
151 :     }
152 :    
153 : Isibaar 357 global /=mb_width * mb_height;
154 : Isibaar 3
155 : Isibaar 357 if (((global <GlobalBrightThres) &&(global >GlobalDarkThres))
156 :     || (mid_range < MidRangeThres)) {
157 :     for (k = 0; k < mb_height; k++) {
158 :     for (l = 0; l < mb_width; l++) // do this for all macroblocks individually
159 :     {
160 :     if (val[k * mb_width + l] < DarkThres)
161 :     quant[k * mb_width + l] +=
162 :     DarkAmpl * (DarkThres -
163 :     val[k * mb_width + l]) / DarkThres;
164 :     else if (val[k * mb_width + l] > BrightThres)
165 :     quant[k * mb_width + l] +=
166 :     BrightAmpl * (val[k * mb_width + l] -
167 :     BrightThres) / (255 - BrightThres);
168 :     }
169 : Isibaar 3 }
170 :     }
171 :     free(val);
172 : edgomez 391 free(quant);
173 : edgomez 195 return normalize_quantizer_field(quant, intquant, mb_width * mb_height,
174 :     min_quant, max_quant);
175 : edgomez 18 }

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