expanded gamma lut, with floor
[sparrow.git] / play_core.h
blobf3565f4100ab6cbb679713c76e87fa7dca60c5da
1 #include "sparrow.h"
2 #include "gstsparrow.h"
3 #include <string.h>
4 #include <math.h>
6 #define DEBUG_PLAY 0
7 #define OLD_FRAMES 4
9 static const double GAMMA = 2.0;
10 static const double INV_GAMMA = 1.0 / 2.0;
11 #define GAMMA_UNIT_LIMIT 1024
12 #define GAMMA_INVERSE_LIMIT 1024
13 #define GAMMA_TABLE_TOP 2048
14 #define GAMMA_TABLE_BASEMENT 1024
15 #define GAMMA_FLOOR -64
17 typedef struct sparrow_play_s{
18 guint16 lut_f[256];
19 guint8 lut_b_basement[GAMMA_TABLE_BASEMENT]; /*rather than if x < 0 return 0 */
20 guint8 lut_b[GAMMA_TABLE_TOP];
21 guint8 *image_row;
22 guint jpeg_index;
23 GstBuffer *old_frames[OLD_FRAMES];
24 int old_frames_head;
25 int old_frames_tail;
26 } sparrow_play_t;
29 #define SUBPIXEL(x) static inline guint8 one_subpixel_##x \
30 (sparrow_play_t *player, guint8 inpix, guint8 jpegpix, guint8 oldpix)
33 SUBPIXEL(gamma_clamp){
34 /*clamp in pseudo gamma space*/
35 int gj = player->lut_f[jpegpix];
36 int gi = player->lut_f[inpix];
37 int diff = gj - gi;
38 if (diff < 0)
39 return 0;
40 return player->lut_b[diff];
44 SUBPIXEL(clamp){
45 /*clamp */
46 int diff = jpegpix - inpix;
47 if (diff < 0)
48 return 0;
49 return diff;
52 SUBPIXEL(full_mirror){
53 /*full mirror -SEGFAULTS */
54 int diff = jpegpix - inpix;
55 if (diff < 0)
56 return -diff; /*or -diff /2 */
57 return diff;
60 SUBPIXEL(sum){
61 guint sum = jpegpix + inpix;
62 return sum >> 1;
65 SUBPIXEL(gamma_avg){
66 int sum = player->lut_f[jpegpix] + player->lut_f[255 - inpix];
67 return player->lut_b[sum >> 1];
70 SUBPIXEL(simple){
71 int sum = jpegpix + ((oldpix - inpix) >> 1);
72 if (sum < 0)
73 return 0;
74 if (sum > 200)
75 return 200 + sum / 2;
76 return sum;
79 SUBPIXEL(gentle_clamp){
80 /* gentle clamp */
81 int error = MAX(inpix - oldpix, 0) >> 1;
82 int diff = jpegpix - error;
83 if (diff < 0)
84 return 0;
85 return diff;
88 SUBPIXEL(zebra){
89 int error = MAX(inpix - oldpix, 0) >> 1;
90 int diff = jpegpix - error - (inpix >> 1);
91 if (diff < 0)
92 return 0;
93 if (diff > 255)
94 return 255;
95 return diff;
98 SUBPIXEL(inverse_clamp){
99 /* gentle clamp */
100 int error = MAX(oldpix - inpix, 0);
101 int diff = jpegpix + error;
102 if (diff < 0)
103 return 0;
104 if (diff > 255)
105 return 255;
106 return diff;
109 SUBPIXEL(gamma_oldpix){
110 /*clamp in pseudo gamma space*/
111 int jpeg_gamma = player->lut_b[jpegpix];
112 int in_gamma = player->lut_b[inpix];
113 int old_gamma = player->lut_b[oldpix];
114 int error = (in_gamma - old_gamma) >> 1;
115 int diff = jpeg_gamma - error;
116 if (diff < 0)
117 return 0;
118 return player->lut_f[diff];
122 SUBPIXEL(gamma_clamp_oldpix_gentle){
123 /*clamp in pseudo gamma space*/
124 int jpeg_gamma = player->lut_f[jpegpix];
125 int in_gamma = player->lut_f[inpix];
126 int old_gamma = player->lut_f[oldpix];
127 int error = MAX(in_gamma - old_gamma, 0) >> 1;
128 int diff = jpeg_gamma - error;
129 if (diff < 0)
130 return 0;
131 return player->lut_b[diff];
134 SUBPIXEL(gamma_clamp_oldpix){
135 /*clamp in pseudo gamma space*/
136 int jpeg_gamma = player->lut_f[jpegpix];
137 int in_gamma = player->lut_f[inpix];
138 int old_gamma = player->lut_f[oldpix];
139 int error = MAX(in_gamma - old_gamma, 0);
140 int diff = jpeg_gamma - error;
141 return player->lut_b[diff]; /*diff range: -1023 to 1023*/
146 SUBPIXEL(mess){
147 /*something */
148 int target = 2 * jpegpix - oldpix;
149 int diff = (target - inpix -inpix) >> 1;
150 if (diff < 0)
151 return 0;
152 if (diff > 255)
153 return 255;
154 return diff;