Parent Directory | Revision Log
Revision 1.8 - (view) (download)
1 : | edgomez | 1.7 | /***************************************************************************** |
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 : | edgomez | 1.8 | * $Id: adapt_quant.c,v 1.7 2002/09/07 12:51:38 edgomez Exp $ |
32 : | edgomez | 1.7 | * |
33 : | ****************************************************************************/ | ||
34 : | |||
35 : | Isibaar | 1.1 | #include "../portab.h" |
36 : | #include "adapt_quant.h" | ||
37 : | |||
38 : | edgomez | 1.3 | #include <stdlib.h> /* free, malloc */ |
39 : | edgomez | 1.2 | |
40 : | Isibaar | 1.1 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
41 : | #define RDIFF(a,b) ((int)(a+0.5)-(int)(b+0.5)) | ||
42 : | edgomez | 1.7 | |
43 : | /***************************************************************************** | ||
44 : | * Functions | ||
45 : | ****************************************************************************/ | ||
46 : | Isibaar | 1.1 | |
47 : | edgomez | 1.3 | int |
48 : | normalize_quantizer_field(float *in, | ||
49 : | int *out, | ||
50 : | int num, | ||
51 : | int min_quant, | ||
52 : | int max_quant) | ||
53 : | Isibaar | 1.1 | { |
54 : | int i; | ||
55 : | int finished; | ||
56 : | edgomez | 1.3 | |
57 : | do { | ||
58 : | Isibaar | 1.1 | finished = 1; |
59 : | edgomez | 1.3 | for (i = 1; i < num; i++) { |
60 : | if (RDIFF(in[i], in[i - 1]) > 2) { | ||
61 : | Isibaar | 1.1 | in[i] -= (float) 0.5; |
62 : | finished = 0; | ||
63 : | edgomez | 1.3 | } else if (RDIFF(in[i], in[i - 1]) < -2) { |
64 : | in[i - 1] -= (float) 0.5; | ||
65 : | Isibaar | 1.1 | finished = 0; |
66 : | } | ||
67 : | edgomez | 1.3 | |
68 : | if (in[i] > max_quant) { | ||
69 : | edgomez | 1.2 | in[i] = (float) max_quant; |
70 : | finished = 0; | ||
71 : | } | ||
72 : | edgomez | 1.3 | if (in[i] < min_quant) { |
73 : | edgomez | 1.2 | in[i] = (float) min_quant; |
74 : | finished = 0; | ||
75 : | } | ||
76 : | edgomez | 1.3 | if (in[i - 1] > max_quant) { |
77 : | in[i - 1] = (float) max_quant; | ||
78 : | edgomez | 1.2 | finished = 0; |
79 : | } | ||
80 : | edgomez | 1.3 | if (in[i - 1] < min_quant) { |
81 : | in[i - 1] = (float) min_quant; | ||
82 : | edgomez | 1.2 | finished = 0; |
83 : | } | ||
84 : | Isibaar | 1.1 | } |
85 : | edgomez | 1.3 | } while (!finished); |
86 : | |||
87 : | Isibaar | 1.1 | out[0] = 0; |
88 : | for (i = 1; i < num; i++) | ||
89 : | edgomez | 1.3 | out[i] = RDIFF(in[i], in[i - 1]); |
90 : | |||
91 : | Isibaar | 1.1 | return (int) (in[0] + 0.5); |
92 : | } | ||
93 : | |||
94 : | edgomez | 1.3 | 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 | 1.1 | { |
104 : | edgomez | 1.3 | int i, j, k, l; |
105 : | |||
106 : | edgomez | 1.6 | float *quant; |
107 : | Isibaar | 1.1 | unsigned char *ptr; |
108 : | float *val; | ||
109 : | Isibaar | 1.5 | float global = 0.; |
110 : | uint32_t mid_range = 0; | ||
111 : | |||
112 : | const float DarkAmpl = 14 / 2; | ||
113 : | const float BrightAmpl = 10 / 2; | ||
114 : | const float DarkThres = 70; | ||
115 : | const float BrightThres = 200; | ||
116 : | |||
117 : | const float GlobalDarkThres = 60; | ||
118 : | const float GlobalBrightThres = 170; | ||
119 : | |||
120 : | const float MidRangeThres = 20; | ||
121 : | const float UpperLimit = 200; | ||
122 : | const float LowerLimit = 25; | ||
123 : | Isibaar | 1.1 | |
124 : | edgomez | 1.3 | |
125 : | edgomez | 1.6 | if (!(quant = (float *) malloc(mb_width * mb_height * sizeof(float)))) |
126 : | return(-1); | ||
127 : | Isibaar | 1.1 | |
128 : | edgomez | 1.6 | if(!(val = (float *) malloc(mb_width * mb_height * sizeof(float)))) |
129 : | return(-1); | ||
130 : | Isibaar | 1.1 | |
131 : | edgomez | 1.3 | for (k = 0; k < mb_height; k++) { |
132 : | for (l = 0; l < mb_width; l++) // do this for all macroblocks individually | ||
133 : | Isibaar | 1.1 | { |
134 : | edgomez | 1.3 | quant[k * mb_width + l] = (float) framequant; |
135 : | |||
136 : | Isibaar | 1.1 | // calculate luminance-masking |
137 : | edgomez | 1.3 | ptr = &buf[16 * k * stride + 16 * l]; // address of MB |
138 : | Isibaar | 1.1 | |
139 : | edgomez | 1.3 | 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 | 1.5 | 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 | 1.1 | } |
151 : | } | ||
152 : | |||
153 : | Isibaar | 1.5 | global /=mb_width * mb_height; |
154 : | Isibaar | 1.1 | |
155 : | Isibaar | 1.5 | 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 | 1.1 | } |
170 : | } | ||
171 : | edgomez | 1.8 | |
172 : | i = normalize_quantizer_field(quant, intquant, | ||
173 : | mb_width * mb_height, | ||
174 : | min_quant, max_quant); | ||
175 : | |||
176 : | Isibaar | 1.1 | free(val); |
177 : | edgomez | 1.6 | free(quant); |
178 : | edgomez | 1.8 | |
179 : | return(i); | ||
180 : | |||
181 : | edgomez | 1.2 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |