Debian Codegraph @

libmmxnow/src/generic/video/rgb_yuv.c

raw symbols SCIP
1 #include "create.h"
2
3 #include <stdlib.h>
4
5 static inline void rgb_yuv(mmxnow_yuv_t* yuv, const mmxnow_rgb_t* rgb)
6 {
7 int Y, Cb, Cr;
8 uint8_t r = rgb->r;
9 uint8_t g = rgb->g;
10 uint8_t b = rgb->b;
11
12 Y = 66 * r + 129 * g + 25 * b + 0x1000;
13 Cb = -38 * r - 74 * g + 112 * b + 0x8000;
14 Cr = 112 * r - 94 * g - 18 * b + 0x8000;
15 if (Y < 0x1000) Y = 0x1000;
16 else if ( Y > 0xef00) Y = 0xef00;
17 if (Cb < 0x1000) Cb = 0x1000;
18 else if(Cb > 0xef00) Cb = 0xef00;
19 if (Cr < 0x1000) Cr = 0x1000;
20 else if (Cr > 0xef00) Cr = 0xef00;
21
22 yuv->y = (uint8_t) (Y >> 8);
23 yuv->cb = (uint8_t) (Cb >> 8);
24 yuv->cr = (uint8_t) (Cr >> 8);
25 }
26
27 static inline int rgb_y(const mmxnow_rgb_t* rgb)
28 {
29 return (16853l * rgb->r) + (33055l * rgb->g) + (6392l * rgb->b) + 0x8000;
30 }
31
32 // add stride calculation
33
34 static int rgb24_to_yuv(struct mmxnow_context_t* ctx)
35 {
36 mmxnow_image_data_t* data = (mmxnow_image_data_t*) ctx->data;
37
38 int width = data->in.width;
39 int height = data->in.height;
40 int flip = data->flip;
41 mmxnow_yuv_t* dest = (mmxnow_yuv_t*) data->out.plane[0] + width * height - 1;
42 const mmxnow_rgb_t* src;
43 int i;
44 if (!flip)
45 src = (const mmxnow_rgb_t*) data->in.plane[0] + width * height - 1;
46 else
47 src = (const mmxnow_rgb_t*) data->in.plane[0] + width - 1;
48
49 for (i = height-1; i >= 0; i--)
50 {
51 int j;
52 for (j = width-1; j >= 0; j--)
53 {
54 rgb_yuv(dest, src);
55 src--;
56 dest--;
57 }
58 if (flip)
59 src += 2 * width;
60 }
61
62 return 0;
63 }
64
65 static int rgb24_to_yuy2(struct mmxnow_context_t* ctx)
66 {
67 mmxnow_image_data_t* data = (mmxnow_image_data_t*) ctx->data;
68
69 int width = data->in.width;
70 int height = data->in.height;
71 int flip = data->flip;
72 // yuv2 - 16bit
73 uint8_t* dest = (uint8_t*)data->out.plane[0] + 2 * width * height - 1;
74 const mmxnow_rgb_t* src;
75 int i;
76 if (!flip)
77 src = (const mmxnow_rgb_t*)data->in.plane[0] + width * height - 1;
78 else
79 src = (const mmxnow_rgb_t*)data->in.plane[0] + width - 1;
80
81 for(i = height - 1; i >= 0; i--)
82 {
83 int j;
84 for(j = width / 2- 1; j >= 0; j--)
85 {
86 mmxnow_yuv_t yuvt;
87 int y;
88 rgb_yuv(&yuvt, src);
89 *src--;
90 *dest--= yuvt.cr;
91 *dest--= yuvt.y;
92 *dest--= yuvt.cb;
93 y = rgb_y(src);
94 *dest-- = (y >> 16) + 16;
95 src--;
96 }
97 if (flip)
98 src += 2 * width;
99 }
100 return 0;
101 }
102
103 static int rgb24_to_yv12(struct mmxnow_context_t* ctx)
104 {
105 mmxnow_image_data_t* data = (mmxnow_image_data_t*) ctx->data;
106
107 int width = data->in.width;
108 int height = data->in.height;
109 int flip = data->flip;
110 int i;
111 const mmxnow_rgb_t* src1, *src2;
112
113 //from+=2*width*height-1;
114 uint8_t* dest_y1 = data->out.plane[0];
115 uint8_t* dest_y2 = data->out.plane[0] + data->out.stride[0];
116 //uint8_t* dest_cb = data->out.plane[0] + width * height;
117 //uint8_t* dest_cr = data->out.plane[0] + 5 * width * height / 4;
118 uint8_t* dest_cb = data->out.plane[1];
119 uint8_t* dest_cr = data->out.plane[2];
120
121 if (flip)
122 {
123 src1 = (const mmxnow_rgb_t*) data->in.plane[0] + width * (height - 1);
124 src2 = (const mmxnow_rgb_t*) data->in.plane[0] + width * (height - 2);
125 }
126 else
127 {
128 src1 = (const mmxnow_rgb_t*) data->in.plane[0];
129 src2 = (const mmxnow_rgb_t*) data->in.plane[0] + width;
130 }
131
132
133 // uint8_t* dest;
134 // const col* src;
135 // dest=to+2*(width*height-1);
136 // if(!flip_dir)
137 // src=(const col*)from+width*height-1;
138 // else
139 // src=(const col*)from+width-1;
140
141 for(i = height / 2 - 1; i >= 0; i--)
142 {
143 int j;
144 for(j = width / 2 - 1; j >= 0; j--)
145 {
146 mmxnow_yuv_t yuvt;
147 rgb_yuv(&yuvt, src1);
148 *src1++;
149 *dest_cb++ = yuvt.cb;
150 *dest_cr++ = yuvt.cr;
151 *dest_y1++ = yuvt.y;
152 *dest_y1++ = rgb_y(src1);
153 src1++;
154 *dest_y2++ = rgb_y(src2);
155 src2++;
156 *dest_y2++ = rgb_y(src2);
157 src2++;
158 }
159 dest_y1 += width;
160 dest_y2 += width;
161 if (flip)
162 {
163 src1 -= 3*width;
164 src2 -= 3*width;
165 }
166 else
167 {
168 src1 += width;
169 src2 += width;
170 }
171 }
172
173 return 0;
174 }
175
176 void mmxnow_create_generic_video_rgb_yuv(mmxnow_context_t* ctx, int method)
177 {
178 if (ctx->method == 0)
179 {
180 switch (method)
181 {
182 case MMXNOW_BGR24_TO_YUV:
183 ctx->method = rgb24_to_yuv;
184 ctx->name = "rgb24_to_yuv";
185 break;
186 case MMXNOW_BGR24_TO_YUY2:
187 ctx->method = rgb24_to_yuy2;
188 ctx->name = "rgb24_to_yuv2";
189 break;
190 case MMXNOW_BGR24_TO_YV12:
191 ctx->method = rgb24_to_yv12;
192 ctx->name = "rgb24_to_yv12";
193 break;
194 }
195 if (ctx->method)
196 ctx->data = calloc(1, sizeof(mmxnow_image_data_t));
197 }
198 }