Parent Directory | Revision Log
Revision 357 - (view) (download)
1 : | Isibaar | 3 | #include "../portab.h" |
2 : | #include "adapt_quant.h" | ||
3 : | |||
4 : | edgomez | 195 | #include <stdlib.h> /* free, malloc */ |
5 : | edgomez | 18 | |
6 : | Isibaar | 3 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
7 : | #define RDIFF(a,b) ((int)(a+0.5)-(int)(b+0.5)) | ||
8 : | |||
9 : | edgomez | 195 | int |
10 : | normalize_quantizer_field(float *in, | ||
11 : | int *out, | ||
12 : | int num, | ||
13 : | int min_quant, | ||
14 : | int max_quant) | ||
15 : | Isibaar | 3 | { |
16 : | int i; | ||
17 : | int finished; | ||
18 : | edgomez | 195 | |
19 : | do { | ||
20 : | Isibaar | 3 | finished = 1; |
21 : | edgomez | 195 | for (i = 1; i < num; i++) { |
22 : | if (RDIFF(in[i], in[i - 1]) > 2) { | ||
23 : | Isibaar | 3 | in[i] -= (float) 0.5; |
24 : | finished = 0; | ||
25 : | edgomez | 195 | } else if (RDIFF(in[i], in[i - 1]) < -2) { |
26 : | in[i - 1] -= (float) 0.5; | ||
27 : | Isibaar | 3 | finished = 0; |
28 : | } | ||
29 : | edgomez | 195 | |
30 : | if (in[i] > max_quant) { | ||
31 : | edgomez | 18 | in[i] = (float) max_quant; |
32 : | finished = 0; | ||
33 : | } | ||
34 : | edgomez | 195 | if (in[i] < min_quant) { |
35 : | edgomez | 18 | in[i] = (float) min_quant; |
36 : | finished = 0; | ||
37 : | } | ||
38 : | edgomez | 195 | if (in[i - 1] > max_quant) { |
39 : | in[i - 1] = (float) max_quant; | ||
40 : | edgomez | 18 | finished = 0; |
41 : | } | ||
42 : | edgomez | 195 | if (in[i - 1] < min_quant) { |
43 : | in[i - 1] = (float) min_quant; | ||
44 : | edgomez | 18 | finished = 0; |
45 : | } | ||
46 : | Isibaar | 3 | } |
47 : | edgomez | 195 | } while (!finished); |
48 : | |||
49 : | Isibaar | 3 | out[0] = 0; |
50 : | for (i = 1; i < num; i++) | ||
51 : | edgomez | 195 | out[i] = RDIFF(in[i], in[i - 1]); |
52 : | |||
53 : | Isibaar | 3 | return (int) (in[0] + 0.5); |
54 : | } | ||
55 : | |||
56 : | edgomez | 195 | int |
57 : | adaptive_quantization(unsigned char *buf, | ||
58 : | int stride, | ||
59 : | int *intquant, | ||
60 : | int framequant, | ||
61 : | int min_quant, | ||
62 : | int max_quant, | ||
63 : | int mb_width, | ||
64 : | int mb_height) // no qstride because normalization | ||
65 : | Isibaar | 3 | { |
66 : | edgomez | 195 | int i, j, k, l; |
67 : | |||
68 : | Isibaar | 3 | static float *quant; |
69 : | unsigned char *ptr; | ||
70 : | float *val; | ||
71 : | Isibaar | 357 | float global = 0.; |
72 : | uint32_t mid_range = 0; | ||
73 : | Isibaar | 3 | |
74 : | Isibaar | 357 | const float DarkAmpl = 14 / 2; |
75 : | const float BrightAmpl = 10 / 2; | ||
76 : | const float DarkThres = 70; | ||
77 : | const float BrightThres = 200; | ||
78 : | Isibaar | 3 | |
79 : | Isibaar | 357 | const float GlobalDarkThres = 60; |
80 : | const float GlobalBrightThres = 170; | ||
81 : | Isibaar | 3 | |
82 : | Isibaar | 357 | const float MidRangeThres = 20; |
83 : | const float UpperLimit = 200; | ||
84 : | const float LowerLimit = 25; | ||
85 : | Isibaar | 3 | |
86 : | Isibaar | 357 | |
87 : | edgomez | 195 | if (!quant) |
88 : | if (!(quant = (float *) malloc(mb_width * mb_height * sizeof(float)))) | ||
89 : | Isibaar | 3 | return -1; |
90 : | |||
91 : | edgomez | 195 | val = (float *) malloc(mb_width * mb_height * sizeof(float)); |
92 : | Isibaar | 3 | |
93 : | edgomez | 195 | for (k = 0; k < mb_height; k++) { |
94 : | for (l = 0; l < mb_width; l++) // do this for all macroblocks individually | ||
95 : | Isibaar | 3 | { |
96 : | edgomez | 195 | quant[k * mb_width + l] = (float) framequant; |
97 : | |||
98 : | Isibaar | 3 | // calculate luminance-masking |
99 : | edgomez | 195 | ptr = &buf[16 * k * stride + 16 * l]; // address of MB |
100 : | Isibaar | 3 | |
101 : | edgomez | 195 | val[k * mb_width + l] = 0.; |
102 : | |||
103 : | for (i = 0; i < 16; i++) | ||
104 : | for (j = 0; j < 16; j++) | ||
105 : | val[k * mb_width + l] += ptr[i * stride + j]; | ||
106 : | val[k * mb_width + l] /= 256.; | ||
107 : | Isibaar | 357 | global +=val[k * mb_width + l]; |
108 : | |||
109 : | if ((val[k * mb_width + l] > LowerLimit) && | ||
110 : | (val[k * mb_width + l] < UpperLimit)) | ||
111 : | mid_range++; | ||
112 : | Isibaar | 3 | } |
113 : | } | ||
114 : | |||
115 : | Isibaar | 357 | global /=mb_width * mb_height; |
116 : | Isibaar | 3 | |
117 : | Isibaar | 357 | if (((global <GlobalBrightThres) &&(global >GlobalDarkThres)) |
118 : | || (mid_range < MidRangeThres)) { | ||
119 : | for (k = 0; k < mb_height; k++) { | ||
120 : | for (l = 0; l < mb_width; l++) // do this for all macroblocks individually | ||
121 : | { | ||
122 : | if (val[k * mb_width + l] < DarkThres) | ||
123 : | quant[k * mb_width + l] += | ||
124 : | DarkAmpl * (DarkThres - | ||
125 : | val[k * mb_width + l]) / DarkThres; | ||
126 : | else if (val[k * mb_width + l] > BrightThres) | ||
127 : | quant[k * mb_width + l] += | ||
128 : | BrightAmpl * (val[k * mb_width + l] - | ||
129 : | BrightThres) / (255 - BrightThres); | ||
130 : | } | ||
131 : | Isibaar | 3 | } |
132 : | } | ||
133 : | free(val); | ||
134 : | edgomez | 195 | return normalize_quantizer_field(quant, intquant, mb_width * mb_height, |
135 : | min_quant, max_quant); | ||
136 : | edgomez | 18 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |