allow ffmpeg video to resample curr_pos, add bluray format
[goodguy/history.git] / cinelerra-5.0 / quicktime / cmodel_permutation.h
1 /*
2  * This library is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU Lesser General Public License as published
4  * by the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  * 
7  * This library is distributed in the hope that it will be useful, but
8  * WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  * 
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 
15  * USA
16  */
17 #include "colormodels.h"
18 #include <stdint.h>
19
20 // All variables are unsigned
21 // y -> 24 bits u, v, -> 8 bits r, g, b -> 8 bits
22 #define YUV_TO_RGB(y, u, v, r, g, b) \
23 { \
24         (r) = ((y + yuv_table->vtor_tab[v]) >> 16); \
25         (g) = ((y + yuv_table->utog_tab[u] + yuv_table->vtog_tab[v]) >> 16); \
26         (b) = ((y + yuv_table->utob_tab[u]) >> 16); \
27         CLAMP(r, 0, 0xff); \
28         CLAMP(g, 0, 0xff); \
29         CLAMP(b, 0, 0xff); \
30 }
31
32 // y -> 0 - 1 float
33 // u, v, -> 8 bits
34 // r, g, b -> float
35 #define YUV_TO_FLOAT(y, u, v, r, g, b) \
36 { \
37         (r) = y + yuv_table->vtor_float_tab[v]; \
38         (g) = y + yuv_table->utog_float_tab[u] + yuv_table->vtog_float_tab[v]; \
39         (b) = y + yuv_table->utob_float_tab[u]; \
40 }
41
42 // y -> 0 - 1 float
43 // u, v, -> 16 bits
44 // r, g, b -> float
45 #define YUV16_TO_RGB_FLOAT(y, u, v, r, g, b) \
46 { \
47         (r) = y + yuv_table->v16tor_float_tab[v]; \
48         (g) = y + yuv_table->u16tog_float_tab[u] + yuv_table->v16tog_float_tab[v]; \
49         (b) = y + yuv_table->u16tob_float_tab[u]; \
50 }
51
52 // y -> 24 bits   u, v-> 16 bits
53 #define YUV_TO_RGB16(y, u, v, r, g, b) \
54 { \
55         (r) = ((y + yuv_table->vtor_tab16[v]) >> 8); \
56         (g) = ((y + yuv_table->utog_tab16[u] + yuv_table->vtog_tab16[v]) >> 8); \
57         (b) = ((y + yuv_table->utob_tab16[u]) >> 8); \
58         CLAMP(r, 0, 0xffff); \
59         CLAMP(g, 0, 0xffff); \
60         CLAMP(b, 0, 0xffff); \
61 }
62
63
64
65
66 #define RGB_TO_YUV(y, u, v, r, g, b) \
67 { \
68         y = ((yuv_table->rtoy_tab[r] + yuv_table->gtoy_tab[g] + yuv_table->btoy_tab[b]) >> 16); \
69         u = ((yuv_table->rtou_tab[r] + yuv_table->gtou_tab[g] + yuv_table->btou_tab[b]) >> 16); \
70         v = ((yuv_table->rtov_tab[r] + yuv_table->gtov_tab[g] + yuv_table->btov_tab[b]) >> 16); \
71         CLAMP(y, 0, 0xff); \
72         CLAMP(u, 0, 0xff); \
73         CLAMP(v, 0, 0xff); \
74 }
75
76 // r, g, b -> 16 bits
77 #define RGB_TO_YUV16(y, u, v, r, g, b) \
78 { \
79         y = ((yuv_table->rtoy_tab16[r] + yuv_table->gtoy_tab16[g] + yuv_table->btoy_tab16[b]) >> 8); \
80         u = ((yuv_table->rtou_tab16[r] + yuv_table->gtou_tab16[g] + yuv_table->btou_tab16[b]) >> 8); \
81         v = ((yuv_table->rtov_tab16[r] + yuv_table->gtov_tab16[g] + yuv_table->btov_tab16[b]) >> 8); \
82         CLAMP(y, 0, 0xffff); \
83         CLAMP(u, 0, 0xffff); \
84         CLAMP(v, 0, 0xffff); \
85 }
86
87 #define WRITE_YUV101010(y, u, v) \
88 { \
89         uint32_t output_i = ((y & 0xffc0) << 16) | \
90                 ((u & 0xffc0) << 6) | \
91                 ((v & 0xffc0) >> 4); \
92         *(*output)++ = output_i; \
93         *(*output)++ = output_i >> 8; \
94         *(*output)++ = output_i >> 16; \
95         *(*output)++ = output_i >> 24; \
96 }
97
98
99 // ****************************** Pixel transfers *****************************
100
101
102
103
104
105
106 // ****************************** ARGB8888 -> *********************************
107
108 static inline void transfer_ARGB8888_to_ARGB8888(unsigned char *(*output), unsigned char *input)
109 {
110         (*output)[0] = input[0];
111         (*output)[1] = input[1];
112         (*output)[2] = input[2];
113         (*output)[3] = input[3];
114         (*output) += 4;
115 }
116
117 static inline void transfer_ARGB8888_to_RGBA8888(unsigned char *(*output), unsigned char *input)
118 {
119         (*output)[0] = input[1];
120         (*output)[1] = input[2];
121         (*output)[2] = input[3];
122         (*output)[3] = input[0];
123         (*output) += 4;
124 }
125
126
127 static inline void transfer_ARGB8888_to_RGB888(unsigned char *(*output), unsigned char *input)
128 {
129         int a = input[0];
130         (*output)[0] = input[1] * a / 0xff;
131         (*output)[1] = input[2] * a / 0xff;
132         (*output)[2] = input[3] * a / 0xff;
133         (*output) += 3;
134 }
135
136 static inline void transfer_ARGB8888_to_BGR8888(unsigned char *(*output), unsigned char *input)
137 {
138         int a = input[0];
139         (*output)[0] = input[3] * a / 0xff;
140         (*output)[1] = input[2] * a / 0xff;
141         (*output)[2] = input[1] * a / 0xff;
142         (*output) += 3;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 // ******************************** RGB888 -> *********************************
160
161 static inline void transfer_RGB888_to_RGB8(unsigned char *(*output), unsigned char *input)
162 {
163         *(*output) = (unsigned char)((input[0] & 0xc0) +
164                                                  ((input[1] & 0xe0) >> 2) +
165                                                  ((input[2] & 0xe0) >> 5));
166         (*output)++;
167 }
168
169 static inline void transfer_RGB888_to_BGR565(unsigned char *(*output), unsigned char *input)
170 {
171         uint16_t r, g, b;
172         uint16_t r_s, g_s, b_s;
173         r = *input++;
174         g = *input++;
175         b = *input;
176         
177         r_s  = (r & 0x01) << 7;
178         r_s |= (r & 0x02) << 5;
179         r_s |= (r & 0x04) << 3;
180         r_s |= (r & 0x08) << 1;
181         r_s |= (r & 0x10) >> 1;
182         r_s |= (r & 0x20) >> 3;
183         r_s |= (r & 0x40) >> 5;
184         r_s |= (r & 0x80) >> 7;
185
186         g_s  = (g & 0x01) << 7;
187         g_s |= (g & 0x02) << 5;
188         g_s |= (g & 0x04) << 3;
189         g_s |= (g & 0x08) << 1;
190         g_s |= (g & 0x10) >> 1;
191         g_s |= (g & 0x20) >> 3;
192         g_s |= (g & 0x40) >> 5;
193         g_s |= (g & 0x80) >> 7;
194
195         b_s  = (b & 0x01) << 7;
196         b_s |= (b & 0x02) << 5;
197         b_s |= (b & 0x04) << 3;
198         b_s |= (b & 0x08) << 1;
199         b_s |= (b & 0x10) >> 1;
200         b_s |= (b & 0x20) >> 3;
201         b_s |= (b & 0x40) >> 5;
202         b_s |= (b & 0x80) >> 7;
203
204         *(uint16_t*)(*output) = ((b_s & 0xf8) << 8)
205                          + ((g_s & 0xfc) << 3)
206                          + ((r_s & 0xf8) >> 3);
207         (*output) += 2;
208 }
209
210 static inline void transfer_RGB888_to_RGB565(unsigned char *(*output), unsigned char *input)
211 {
212         uint16_t r, g, b;
213         r = *input++;
214         g = *input++;
215         b = *input;
216         *(uint16_t*)(*output) = ((r & 0xf8) << 8)
217                          + ((g & 0xfc) << 3)
218                          + ((b & 0xf8) >> 3);
219         (*output) += 2;
220 }
221
222 static inline void transfer_RGB888_to_BGR888(unsigned char *(*output), unsigned char *input)
223 {
224         *(*output)++ = input[2];
225         *(*output)++ = input[1];
226         *(*output)++ = input[0];
227 }
228
229 static inline void transfer_RGB888_to_RGB888(unsigned char *(*output), unsigned char *input)
230 {
231         *(*output)++ = *input++;
232         *(*output)++ = *input++;
233         *(*output)++ = *input;
234 }
235
236 static inline void transfer_RGB888_to_RGBA8888(unsigned char *(*output), unsigned char *input)
237 {
238         *(*output)++ = *input++;
239         *(*output)++ = *input++;
240         *(*output)++ = *input;
241         *(*output)++ = 0xff;
242 }
243
244 static inline void transfer_RGB888_to_ARGB8888(unsigned char *(*output), unsigned char *input)
245 {
246         *(*output)++ = 0xff;
247         *(*output)++ = *input++;
248         *(*output)++ = *input++;
249         *(*output)++ = *input;
250 }
251
252 static inline void transfer_RGB888_to_RGB161616(uint16_t *(*output), unsigned char *input)
253 {
254         (*output)[0] = (input[0] << 8) | input[0];
255         (*output)[1] = (input[1] << 8) | input[1];
256         (*output)[2] = (input[2] << 8) | input[2];
257         (*output) += 3;
258 }
259
260 static inline void transfer_RGB888_to_RGBA16161616(uint16_t *(*output), unsigned char *input)
261 {
262         (*output)[0] = (input[0] << 8) | input[0];
263         (*output)[1] = (input[1] << 8) | input[1];
264         (*output)[2] = (input[2] << 8) | input[2];
265         (*output)[3] = 0xffff;
266         (*output) += 4;
267 }
268
269 static inline void transfer_RGB888_to_RGB_FLOAT(float *(*output), unsigned char *input)
270 {
271         *(*output)++ = (float)*input++ / 0xff;
272         *(*output)++ = (float)*input++ / 0xff;
273         *(*output)++ = (float)*input / 0xff;
274 }
275
276 static inline void transfer_RGB888_to_RGBA_FLOAT(float *(*output), unsigned char *input)
277 {
278         *(*output)++ = (float)*input++ / 0xff;
279         *(*output)++ = (float)*input++ / 0xff;
280         *(*output)++ = (float)*input / 0xff;
281         *(*output)++ = 1.0;
282 }
283
284 static inline void transfer_RGB888_to_ABGR8888(unsigned char *(*output), unsigned char *input)
285 {
286         *(*output)++ = 0xff;
287         *(*output)++ = input[2];
288         *(*output)++ = input[1];
289         *(*output)++ = input[0];
290 }
291
292 static inline void transfer_RGB888_to_BGR8888(unsigned char *(*output), unsigned char *input)
293 {
294         *(*output)++ = input[2];
295         *(*output)++ = input[1];
296         *(*output)++ = input[0];
297         (*output)++;
298 }
299
300 static inline void transfer_RGB888_to_YUV888(unsigned char *(*output), unsigned char *input)
301 {
302         int y, u, v;
303
304         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
305
306         *(*output)++ = y;
307         *(*output)++ = u;
308         *(*output)++ = v;
309 }
310
311
312 static inline void transfer_RGB888_to_YUV101010(unsigned char *(*output), unsigned char *input)
313 {
314         int r, g, b;
315         int y, u, v;
316
317         r = ((uint16_t)input[0]) << 8;
318         g = ((uint16_t)input[1]) << 8;
319         b = ((uint16_t)input[2]) << 8;
320         RGB_TO_YUV16(y, u, v, r, g, b);
321         WRITE_YUV101010(y, u, v);
322 }
323
324 static inline void transfer_RGB888_to_VYU888(unsigned char *(*output), unsigned char *input)
325 {
326         int y, u, v;
327
328         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
329
330         *(*output)++ = v;
331         *(*output)++ = y;
332         *(*output)++ = u;
333 }
334
335 static inline void transfer_RGB888_to_UYVA8888(unsigned char *(*output), unsigned char *input)
336 {
337         int y, u, v;
338
339         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
340
341         *(*output)++ = u;
342         *(*output)++ = y;
343         *(*output)++ = v;
344         *(*output)++ = 0xff;
345 }
346
347
348
349 static inline void transfer_RGB888_to_YUVA8888(unsigned char *(*output), unsigned char *input)
350 {
351         int y, u, v;
352
353         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
354
355         *(*output)++ = y;
356         *(*output)++ = u;
357         *(*output)++ = v;
358         *(*output)++ = 255;
359 }
360
361 static inline void transfer_RGB888_to_YUV161616(uint16_t *(*output), unsigned char *input)
362 {
363         int y, u, v, r, g, b;
364         
365         r = ((int)input[0] << 8) | input[0];
366         g = ((int)input[1] << 8) | input[1];
367         b = ((int)input[2] << 8) | input[2];
368
369         RGB_TO_YUV16(y, u, v, r, g, b);
370
371         *(*output)++ = y;
372         *(*output)++ = u;
373         *(*output)++ = v;
374 }
375
376 static inline void transfer_RGB888_to_YUVA16161616(uint16_t *(*output), unsigned char *input)
377 {
378         int y, u, v, r, g, b;
379
380         r = (((int)input[0]) << 8) | input[0];
381         g = (((int)input[1]) << 8) | input[1];
382         b = (((int)input[2]) << 8) | input[2];
383         RGB_TO_YUV16(y, u, v, r, g, b);
384
385         *(*output)++ = y;
386         *(*output)++ = u;
387         *(*output)++ = v;
388         *(*output)++ = 0xffff;
389 }
390
391 static inline void transfer_RGB888_to_YUV420P_YUV422P(unsigned char *output_y, 
392         unsigned char *output_u, 
393         unsigned char *output_v, 
394         unsigned char *input,
395         int output_column)
396 {
397         int y, u, v;
398
399         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
400
401         output_y[output_column] = y;
402         output_u[output_column / 2] = u;
403         output_v[output_column / 2] = v;
404 }
405
406 static inline void transfer_RGB888_to_YUV444P(unsigned char *output_y, 
407         unsigned char *output_u, 
408         unsigned char *output_v, 
409         unsigned char *input,
410         int output_column)
411 {
412         int y, u, v;
413
414         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
415
416         output_y[output_column] = y;
417         output_u[output_column] = u;
418         output_v[output_column] = v;
419 }
420
421 static inline void transfer_RGB888_to_YUV422(unsigned char *(*output), 
422         unsigned char *input,
423         int j)
424 {
425         int y, u, v;
426
427         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
428
429         if(!(j & 1))
430         { 
431 // Store U and V for even pixels only
432                  (*output)[1] = u;
433                  (*output)[3] = v;
434                  (*output)[0] = y;
435         }
436         else
437         { 
438 // Store Y and advance output for odd pixels only
439                  (*output)[2] = y;
440                  (*output) += 4;
441         }
442
443 }
444
445
446
447
448
449
450
451 // *************************** RGBA8888 -> ************************************
452
453 static inline void transfer_RGBA8888_to_TRANSPARENCY(unsigned char *(*output), unsigned char *input, int (*bit_counter))
454 {
455         if((*bit_counter) == 7) *(*output) = 0;
456
457         if(input[3] < 127) 
458         {
459                 *(*output) |= (unsigned char)1 << (7 - (*bit_counter));
460         }
461
462         if((*bit_counter) == 0)
463         {
464                 (*output)++;
465                 (*bit_counter) = 7;
466         }
467         else
468                 (*bit_counter)--;
469 }
470
471 // These routines blend in a background color since they should be
472 // exclusively used for widgets.
473
474 static inline void transfer_RGBA8888_to_RGB8bg(unsigned char *(*output), unsigned char *input, int bg_r, int bg_g, int bg_b)
475 {
476         unsigned int r, g, b, a, anti_a;
477         a = input[3];
478         anti_a = 255 - a;
479         r = ((unsigned int)input[0] * a + bg_r * anti_a) / 0xff;
480         g = ((unsigned int)input[1] * a + bg_g * anti_a) / 0xff;
481         b = ((unsigned int)input[2] * a + bg_b * anti_a) / 0xff;
482         *(*output) = (unsigned char)((r & 0xc0) + 
483                                 ((g & 0xe0) >> 2) + 
484                                 ((b & 0xe0) >> 5));
485         (*output)++;
486 }
487
488 static inline void transfer_RGBA8888_to_BGR565bg(unsigned char *(*output), unsigned char *input, int bg_r, int bg_g, int bg_b)
489 {
490         unsigned int r, g, b, a, anti_a;
491         a = input[3];
492         anti_a = 255 - a;
493         r = ((unsigned int)input[0] * a + bg_r * anti_a) / 0xff;
494         g = ((unsigned int)input[1] * a + bg_g * anti_a) / 0xff;
495         b = ((unsigned int)input[2] * a + bg_b * anti_a) / 0xff;
496         *(uint16_t*)(*output) = (uint16_t)(((b & 0xf8) << 8) + 
497                                 ((g & 0xfc) << 3) + 
498                                 ((r & 0xf8) >> 3));
499         (*output) += 2;
500 }
501
502 static inline void transfer_RGBA8888_to_RGB565bg(unsigned char *(*output), unsigned char *input, int bg_r, int bg_g, int bg_b)
503 {
504         unsigned int r, g, b, a, anti_a;
505         a = input[3];
506         anti_a = 255 - a;
507         r = ((unsigned int)input[0] * a + bg_r * anti_a) / 0xff;
508         g = ((unsigned int)input[1] * a + bg_g * anti_a) / 0xff;
509         b = ((unsigned int)input[2] * a + bg_b * anti_a) / 0xff;
510         *(uint16_t*)(*output) = (uint16_t)(((r & 0xf8) << 8)+ 
511                                 ((g & 0xfc) << 3) + 
512                                 ((b & 0xf8) >> 3));
513         (*output) += 2;
514 }
515
516 static inline void transfer_RGBA8888_to_BGR888bg(unsigned char *(*output), unsigned char *input, int bg_r, int bg_g, int bg_b)
517 {
518         unsigned int r, g, b, a, anti_a;
519         a = input[3];
520         anti_a = 255 - a;
521         r = ((unsigned int)input[0] * a + bg_r * anti_a) / 0xff;
522         g = ((unsigned int)input[1] * a + bg_g * anti_a) / 0xff;
523         b = ((unsigned int)input[2] * a + bg_b * anti_a) / 0xff;
524         *(*output)++ = b;
525         *(*output)++ = g;
526         *(*output)++ = r;
527 }
528
529 static inline void transfer_RGBA8888_to_RGB888bg(unsigned char *(*output), unsigned char *input, int bg_r, int bg_g, int bg_b)
530 {
531         unsigned int r, g, b, a, anti_a;
532         a = input[3];
533         anti_a = 255 - a;
534         r = ((unsigned int)input[0] * a + bg_r * anti_a) / 0xff;
535         g = ((unsigned int)input[1] * a + bg_g * anti_a) / 0xff;
536         b = ((unsigned int)input[2] * a + bg_b * anti_a) / 0xff;
537         *(*output)++ = r;
538         *(*output)++ = g;
539         *(*output)++ = b;
540 }
541
542 static inline void transfer_RGBA8888_to_BGR8888bg(unsigned char *(*output), unsigned char *input, int bg_r, int bg_g, int bg_b)
543 {
544         unsigned int r, g, b, a, anti_a;
545         a = input[3];
546         anti_a = 255 - a;
547
548         r = ((unsigned int)input[0] * a + bg_r * anti_a) / 0xff;
549         g = ((unsigned int)input[1] * a + bg_g * anti_a) / 0xff;
550         b = ((unsigned int)input[2] * a + bg_b * anti_a) / 0xff;
551
552         *(*output)++ = b;
553         *(*output)++ = g;
554         *(*output)++ = r;
555         (*output)++;
556 }
557
558
559
560
561
562
563
564 // These routines blend in a black background
565
566 static inline void transfer_RGBA8888_to_RGB8(unsigned char *(*output), unsigned char *input)
567 {
568         unsigned int r, g, b, a;
569         a = input[3];
570         r = (unsigned int)input[0] * a;
571         g = (unsigned int)input[1] * a;
572         b = (unsigned int)input[2] * a;
573         *(*output) = (unsigned char)(((r & 0xc000) >> 8) + 
574                                 ((g & 0xe000) >> 10) + 
575                                 ((b & 0xe000) >> 13));
576         (*output)++;
577 }
578
579 static inline void transfer_RGBA8888_to_BGR565(unsigned char *(*output), unsigned char *input)
580 {
581         unsigned int r, g, b, a;
582         a = input[3];
583         r = ((unsigned int)input[0] * a) / 0xff;
584         g = ((unsigned int)input[1] * a) / 0xff;
585         b = ((unsigned int)input[2] * a) / 0xff;
586         *(uint16_t*)(*output) = (uint16_t)(((b & 0xf8) << 8) + 
587                                 ((g & 0xfc) << 3) + 
588                                 ((r & 0xf8) >> 3));
589         (*output) += 2;
590 }
591
592 static inline void transfer_RGBA8888_to_RGB565(unsigned char *(*output), unsigned char *input)
593 {
594         unsigned int r, g, b, a;
595         a = input[3];
596         r = ((unsigned int)input[0] * a) / 0xff;
597         g = ((unsigned int)input[1] * a) / 0xff;
598         b = ((unsigned int)input[2] * a) / 0xff;
599
600
601         *(uint16_t*)(*output) = (uint16_t)(((r & 0xf8) << 8) + 
602                                 ((g & 0xfc) << 3) + 
603                                 ((b & 0xf8) >> 3));
604         (*output) += 2;
605 }
606
607 static inline void transfer_RGBA8888_to_BGR888(unsigned char *(*output), unsigned char *input)
608 {
609         unsigned int r, g, b, a;
610         a = input[3];
611         r = ((unsigned int)input[0] * a) / 0xff;
612         g = ((unsigned int)input[1] * a) / 0xff;
613         b = ((unsigned int)input[2] * a) / 0xff;
614         *(*output)++ = b;
615         *(*output)++ = g;
616         *(*output)++ = r;
617 }
618
619 static inline void transfer_RGBA8888_to_RGB888(unsigned char *(*output), unsigned char *input)
620 {
621         unsigned int r, g, b, a;
622         a = input[3];
623         r = ((unsigned int)input[0] * a) / 0xff;
624         g = ((unsigned int)input[1] * a) / 0xff;
625         b = ((unsigned int)input[2] * a) / 0xff;
626         *(*output)++ = r;
627         *(*output)++ = g;
628         *(*output)++ = b;
629 }
630
631 static inline void transfer_RGBA8888_to_RGBA8888(unsigned char *(*output), unsigned char *input)
632 {
633         (*output)[0] = input[0];
634         (*output)[1] = input[1];
635         (*output)[2] = input[2];
636         (*output)[3] = input[3];
637         (*output) += 4;
638 }
639
640 static inline void transfer_RGBA8888_to_ARGB8888(unsigned char *(*output), unsigned char *input)
641 {
642         (*output)[0] = input[3];
643         (*output)[1] = input[0];
644         (*output)[2] = input[1];
645         (*output)[3] = input[2];
646         (*output) += 4;
647 }
648
649 static inline void transfer_RGBA8888_to_RGB161616(uint16_t *(*output), unsigned char *input)
650 {
651         int opacity = input[3];
652         (*output)[0] = (((int)input[0] << 8) | input[0]) * opacity / 0xff;
653         (*output)[1] = (((int)input[1] << 8) | input[1]) * opacity / 0xff;
654         (*output)[2] = (((int)input[2] << 8) | input[2]) * opacity / 0xff;
655         (*output) += 3;
656 }
657
658 static inline void transfer_RGBA8888_to_RGBA16161616(uint16_t *(*output), unsigned char *input)
659 {
660         (*output)[0] = (((int)input[0]) << 8) | input[0];
661         (*output)[1] = (((int)input[1]) << 8) | input[1];
662         (*output)[2] = (((int)input[2]) << 8) | input[2];
663         (*output)[3] = (((int)input[3]) << 8) | input[3];
664         (*output) += 4;
665 }
666
667 static inline void transfer_RGBA8888_to_RGB_FLOAT(float *(*output), unsigned char *input)
668 {
669         float opacity = (float)input[3];
670         *(*output)++ = (float)*input++ * opacity / 0xff / 0xff;
671         *(*output)++ = (float)*input++ * opacity / 0xff / 0xff;
672         *(*output)++ = (float)*input * opacity / 0xff / 0xff;
673 }
674
675 static inline void transfer_RGBA8888_to_RGBA_FLOAT(float *(*output), unsigned char *input)
676 {
677         *(*output)++ = (float)*input++ / 0xff;
678         *(*output)++ = (float)*input++ / 0xff;
679         *(*output)++ = (float)*input++ / 0xff;
680         *(*output)++ = (float)*input / 0xff;
681 }
682
683 static inline void transfer_RGBA8888_to_BGR8888(unsigned char *(*output), unsigned char *input)
684 {
685         unsigned int r, g, b, a;
686         a = input[3];
687         r = ((unsigned int)input[0] * a) / 0xff;
688         g = ((unsigned int)input[1] * a) / 0xff;
689         b = ((unsigned int)input[2] * a) / 0xff;
690         *(*output)++ = b;
691         *(*output)++ = g;
692         *(*output)++ = r;
693         (*output)++;
694 }
695
696 static inline void transfer_RGBA8888_to_YUV888(unsigned char *(*output), unsigned char *input)
697 {
698         int y, u, v, a, r, g, b;
699         
700         a = input[3];
701         r = (input[0] * a) / 0xff;
702         g = (input[1] * a) / 0xff;
703         b = (input[2] * a) / 0xff;
704
705         RGB_TO_YUV(y, u, v, r, g, b);
706
707         *(*output)++ = y;
708         *(*output)++ = u;
709         *(*output)++ = v;
710 }
711
712 static inline void transfer_RGBA8888_to_YUVA8888(unsigned char *(*output), unsigned char *input)
713 {
714         int y, u, v;
715
716         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
717
718         *(*output)++ = y;
719         *(*output)++ = u;
720         *(*output)++ = v;
721         *(*output)++ = input[3];
722 }
723
724 static inline void transfer_RGBA8888_to_YUV161616(uint16_t *(*output), unsigned char *input)
725 {
726         int y, u, v, opacity, r, g, b;
727         
728         opacity = input[3];
729         r = (((int)input[0] << 8) | input[0]) * opacity / 0xff;
730         g = (((int)input[1] << 8) | input[1]) * opacity / 0xff;
731         b = (((int)input[2] << 8) | input[2]) * opacity / 0xff;
732
733         RGB_TO_YUV16(y, u, v, r, g, b);
734
735         *(*output)++ = y;
736         *(*output)++ = u;
737         *(*output)++ = v;
738 }
739
740 static inline void transfer_RGBA8888_to_YUVA16161616(uint16_t *(*output), unsigned char *input)
741 {
742         int y, u, v, r, g, b;
743
744         r = (((int)input[0]) << 8) | input[0];
745         g = (((int)input[1]) << 8) | input[1];
746         b = (((int)input[2]) << 8) | input[2];
747         RGB_TO_YUV16(y, u, v, r, g, b);
748
749         *(*output)++ = y;
750         *(*output)++ = u;
751         *(*output)++ = v;
752         *(*output)++ = (((int)input[3]) << 8) | input[3];
753 }
754
755 static inline void transfer_RGBA8888_to_YUV101010(unsigned char *(*output), unsigned char *input)
756 {
757         int r, g, b;
758         int y, u, v;
759
760         r = ((uint16_t)input[0] * input[3]) + 0x1fe;
761         g = ((uint16_t)input[1] * input[3]) + 0x1fe;
762         b = ((uint16_t)input[2] * input[3]) + 0x1fe;
763         RGB_TO_YUV16(y, u, v, r, g, b);
764         WRITE_YUV101010(y, u, v);
765 }
766
767 static inline void transfer_RGBA8888_to_VYU888(unsigned char *(*output), unsigned char *input)
768 {
769         int y, u, v, a, r, g, b;
770         
771         a = input[3];
772         r = ((input[0] * a) >> 8) + 1;
773         g = ((input[1] * a) >> 8) + 1;
774         b = ((input[2] * a) >> 8) + 1;
775
776         RGB_TO_YUV(y, u, v, r, g, b);
777
778         *(*output)++ = v;
779         *(*output)++ = y;
780         *(*output)++ = u;
781 }
782
783 static inline void transfer_RGBA8888_to_UYVA8888(unsigned char *(*output), unsigned char *input)
784 {
785         int y, u, v;
786
787         RGB_TO_YUV(y, u, v, input[0], input[1], input[2]);
788
789         *(*output)++ = u;
790         *(*output)++ = y;
791         *(*output)++ = v;
792         *(*output)++ = input[3];
793 }
794
795 static inline void transfer_RGBA888_to_YUV420P_YUV422P(unsigned char *output_y, 
796         unsigned char *output_u, 
797         unsigned char *output_v, 
798         unsigned char *input,
799         int output_column)
800 {
801         int y, u, v, a, r, g, b;
802         
803         a = input[3];
804         r = (input[0] * a) / 0xff;
805         g = (input[1] * a) / 0xff;
806         b = (input[2] * a) / 0xff;
807
808         RGB_TO_YUV(y, u, v, r, g, b);
809
810         output_y[output_column] = y;
811         output_u[output_column / 2] = u;
812         output_v[output_column / 2] = v;
813 }
814
815 static inline void transfer_RGBA888_to_YUV444P(unsigned char *output_y, 
816         unsigned char *output_u, 
817         unsigned char *output_v, 
818         unsigned char *input,
819         int output_column)
820 {
821         int y, u, v, a, r, g, b;
822         
823         a = input[3];
824         r = (input[0] * a) / 0xff;
825         g = (input[1] * a) / 0xff;
826         b = (input[2] * a) / 0xff;
827
828         RGB_TO_YUV(y, u, v, r, g, b);
829
830         output_y[output_column] = y;
831         output_u[output_column] = u;
832         output_v[output_column] = v;
833 }
834
835 static inline void transfer_RGBA888_to_YUV422(unsigned char *(*output), 
836         unsigned char *input,
837         int j)
838 {
839         int y, u, v, a, r, g, b;
840         
841         a = input[3];
842         r = (input[0] * a) / 0xff;
843         g = (input[1] * a) / 0xff;
844         b = (input[2] * a) / 0xff;
845
846         RGB_TO_YUV(y, u, v, r, g, b);
847
848         if(!(j & 1))
849         { 
850 // Store U and V for even pixels only
851                  (*output)[1] = u;
852                  (*output)[3] = v;
853                  (*output)[0] = y;
854         }
855         else
856         { 
857 // Store Y and advance output for odd pixels only
858                  (*output)[2] = y;
859                  (*output) += 4;
860         }
861
862 }
863
864
865
866
867
868
869
870
871
872
873
874
875
876 // ******************************** RGB161616 -> *********************************
877
878 static inline void transfer_RGB161616_to_RGB8(unsigned char *(*output), uint16_t *input)
879 {
880         *(*output) = (unsigned char)(((input[0] & 0xc000) >> 8) +
881                                                  ((input[1] & 0xe000) >> 10) +
882                                                  ((input[2] & 0xe000) >> 13));
883         (*output)++;
884 }
885
886 static inline void transfer_RGB161616_to_BGR565(unsigned char *(*output), uint16_t *input)
887 {
888         uint16_t r, g, b;
889         r = *input++;
890         g = *input++;
891         b = *input;
892         *(uint16_t*)(*output) = (b & 0xf800) |
893                          ((g & 0xfc00) >> 5) |
894                          ((r & 0xf800) >> 11);
895         (*output) += 2;
896 }
897
898 static inline void transfer_RGB161616_to_RGB565(unsigned char *(*output), uint16_t *input)
899 {
900         uint16_t r, g, b;
901         r = *input++;
902         g = *input++;
903         b = *input;
904         *(uint16_t*)(*output) = (r & 0xf800) |
905                          ((g & 0xfc00) >> 5) |
906                          ((b & 0xf800) >> 11);
907         (*output) += 2;
908 }
909
910 static inline void transfer_RGB161616_to_BGR888(unsigned char *(*output), uint16_t *input)
911 {
912         (*output)[0] = input[2] >> 8;
913         (*output)[1] = input[1] >> 8;
914         (*output)[2] = input[0] >> 8;
915         (*output) += 3;
916 }
917
918 static inline void transfer_RGB161616_to_RGB888(unsigned char *(*output), uint16_t *input)
919 {
920         (*output)[0] = input[0] >> 8;
921         (*output)[1] = input[1] >> 8;
922         (*output)[2] = input[2] >> 8;
923         (*output) += 3;
924 }
925
926 static inline void transfer_RGB161616_to_RGBA8888(unsigned char *(*output), uint16_t *input)
927 {
928         (*output)[0] = input[0] >> 8;
929         (*output)[1] = input[1] >> 8;
930         (*output)[2] = input[2] >> 8;
931         (*output)[3] = 0xff;
932         (*output) += 4;
933 }
934
935 static inline void transfer_RGB161616_to_BGR8888(unsigned char *(*output), uint16_t *input)
936 {
937         (*output)[0] = input[2] >> 8;
938         (*output)[1] = input[1] >> 8;
939         (*output)[2] = input[0] >> 8;
940         (*output) += 4;
941 }
942
943 static inline void transfer_RGB161616_to_RGB_FLOAT(float *(*output), uint16_t *input)
944 {
945         *(*output)++ = (float)*input++ / 0xffff;
946         *(*output)++ = (float)*input++ / 0xffff;
947         *(*output)++ = (float)*input / 0xffff;
948 }
949
950 static inline void transfer_RGB161616_to_RGBA_FLOAT(float *(*output), uint16_t *input)
951 {
952         *(*output)++ = (float)*input++ / 0xffff;
953         *(*output)++ = (float)*input++ / 0xffff;
954         *(*output)++ = (float)*input / 0xffff;
955         *(*output)++ = 1.0;
956 }
957
958 static inline void transfer_RGB161616_to_YUV888(unsigned char *(*output), uint16_t *input)
959 {
960         int y, u, v, r, g, b;
961         r = input[0] >> 8;
962         g = input[1] >> 8;
963         b = input[2] >> 8;
964
965         RGB_TO_YUV(y, u, v, r, g, b);
966
967         (*output)[0] = y;
968         (*output)[1] = u;
969         (*output)[2] = v;
970         (*output) += 3;
971 }
972
973 static inline void transfer_RGB161616_to_YUVA8888(unsigned char *(*output), uint16_t *input)
974 {
975         int y, u, v, r, g, b;
976
977         r = input[0] >> 8;
978         g = input[1] >> 8;
979         b = input[2] >> 8;
980
981         RGB_TO_YUV(y, u, v, r, g, b);
982
983         (*output)[0] = y;
984         (*output)[1] = u;
985         (*output)[2] = v;
986         (*output)[3] = 255;
987         (*output) += 4;
988 }
989
990 static inline void transfer_RGB161616_to_YUV161616(uint16_t *(*output), uint16_t *input)
991 {
992         uint32_t y, u, v, r, g, b;
993         
994         r = (uint32_t)input[0];
995         g = (uint32_t)input[1];
996         b = (uint32_t)input[2];
997         
998         RGB_TO_YUV16(y, u, v, r, g, b);
999         
1000         (*output)[0] = y;
1001         (*output)[1] = u;
1002         (*output)[2] = v;
1003         (*output) += 3;
1004 }
1005
1006 static inline void transfer_RGB161616_to_YUVA16161616(uint16_t *(*output), uint16_t *input)
1007 {
1008         uint32_t y, u, v, r, g, b;
1009         
1010         r = (uint32_t)input[0];
1011         g = (uint32_t)input[1];
1012         b = (uint32_t)input[2];
1013         
1014         RGB_TO_YUV16(y, u, v, r, g, b);
1015
1016         (*output)[0] = y;
1017         (*output)[1] = u;
1018         (*output)[2] = v;
1019         (*output)[3] = 0xffff;
1020         (*output) += 4;
1021 }
1022
1023 static inline void transfer_RGB161616_to_YUV101010(unsigned char *(*output), uint16_t *input)
1024 {
1025         int r, g, b;
1026         int y, u, v;
1027
1028         r = input[0];
1029         g = input[1];
1030         b = input[2];
1031         RGB_TO_YUV16(y, u, v, r, g, b);
1032         WRITE_YUV101010(y, u, v);
1033 }
1034
1035 static inline void transfer_RGB161616_to_VYU888(unsigned char *(*output), uint16_t *input)
1036 {
1037         int y, u, v, r, g, b;
1038         r = input[0] >> 8;
1039         g = input[1] >> 8;
1040         b = input[2] >> 8;
1041
1042         RGB_TO_YUV(y, u, v, r, g, b);
1043
1044         (*output)[0] = v;
1045         (*output)[1] = y;
1046         (*output)[2] = u;
1047         (*output) += 3;
1048 }
1049
1050 static inline void transfer_RGB161616_to_UYVA8888(unsigned char *(*output), uint16_t *input)
1051 {
1052         int y, u, v, r, g, b;
1053
1054         r = input[0] >> 8;
1055         g = input[1] >> 8;
1056         b = input[2] >> 8;
1057
1058         RGB_TO_YUV(y, u, v, r, g, b);
1059
1060         (*output)[0] = u;
1061         (*output)[1] = y;
1062         (*output)[2] = v;
1063         (*output)[3] = 0xff;
1064         (*output) += 4;
1065 }
1066
1067
1068 static inline void transfer_RGB161616_to_YUV420P_YUV422P(unsigned char *output_y, 
1069         unsigned char *output_u, 
1070         unsigned char *output_v, 
1071         uint16_t *input,
1072         int output_column)
1073 {
1074         int y, u, v, r, g, b;
1075         r = input[0] >> 8;
1076         g = input[1] >> 8;
1077         b = input[2] >> 8;
1078
1079         RGB_TO_YUV(y, u, v, r, g, b);
1080
1081         output_y[output_column] = y;
1082         output_u[output_column / 2] = u;
1083         output_v[output_column / 2] = v;
1084 }
1085
1086 static inline void transfer_RGB161616_to_YUV444P(unsigned char *output_y, 
1087         unsigned char *output_u, 
1088         unsigned char *output_v, 
1089         uint16_t *input,
1090         int output_column)
1091 {
1092         int y, u, v, r, g, b;
1093         r = input[0] >> 8;
1094         g = input[1] >> 8;
1095         b = input[2] >> 8;
1096
1097         RGB_TO_YUV(y, u, v, r, g, b);
1098
1099         output_y[output_column] = y;
1100         output_u[output_column] = u;
1101         output_v[output_column] = v;
1102 }
1103
1104
1105 // ****************************** RGBA16161616 -> *****************************
1106
1107 static inline void transfer_RGBA16161616_to_RGB8(unsigned char *(*output), uint16_t *input)
1108 {
1109         unsigned int r, g, b, a;
1110         a = (input)[3] >> 8;
1111         r = (unsigned int)(input)[0] * a;
1112         g = (unsigned int)(input)[1] * a;
1113         b = (unsigned int)(input)[2] * a;
1114
1115         *(*output) = (unsigned char)(((r & 0xc00000) >> 16) + 
1116                                 ((g & 0xe00000) >> 18) + 
1117                                 ((b & 0xe00000) >> 21));
1118         (*output)++;
1119 }
1120
1121 static inline void transfer_RGBA16161616_to_BGR565(unsigned char *(*output), uint16_t *input)
1122 {
1123         unsigned int r, g, b, a;
1124         a = (input)[3] >> 8;
1125         r = (unsigned int)(input)[0] * a;
1126         g = (unsigned int)(input)[1] * a;
1127         b = (unsigned int)(input)[2] * a;
1128
1129         *(uint16_t*)(*output) = (uint16_t)(((b & 0xf80000) >> 8) + 
1130                                 ((g & 0xfc0000) >> 13) + 
1131                                 ((r & 0xf80000) >> 19));
1132         (*output) += 2;
1133 }
1134
1135 static inline void transfer_RGBA16161616_to_RGB565(unsigned char *(*output), uint16_t *input)
1136 {
1137         unsigned int r, g, b, a;
1138         a = (input)[3] >> 8;
1139         r = (unsigned int)(input)[0] * a;
1140         g = (unsigned int)(input)[1] * a;
1141         b = (unsigned int)(input)[2] * a;
1142
1143         *(uint16_t*)(*output) = (uint16_t)(((r & 0xf80000) >> 8) + 
1144                                 ((g & 0xfc0000) >> 13) + 
1145                                 ((b & 0xf80000) >> 19));
1146         (*output) += 2;
1147 }
1148
1149 static inline void transfer_RGBA16161616_to_BGR888(unsigned char *(*output), uint16_t *input)
1150 {
1151         uint32_t r, g, b, a;
1152         a = input[3];
1153         r = (uint32_t)(input)[0] * a;
1154         g = (uint32_t)(input)[1] * a;
1155         b = (uint32_t)(input)[2] * a;
1156
1157 // For display only
1158         (*output)[0] = (unsigned char)(b >> 24);
1159         (*output)[1] = (unsigned char)(g >> 24);
1160         (*output)[2] = (unsigned char)(r >> 24);
1161         (*output) += 3;
1162 }
1163
1164 static inline void transfer_RGBA16161616_to_RGB888(unsigned char *(*output), uint16_t *input)
1165 {
1166         uint32_t r, g, b, a;
1167         a = input[3];
1168         r = (unsigned int)(input)[0] * a;
1169         g = (unsigned int)(input)[1] * a;
1170         b = (unsigned int)(input)[2] * a;
1171
1172         (*output)[0] = (unsigned char)(r / 0xffffff);
1173         (*output)[1] = (unsigned char)(g / 0xffffff);
1174         (*output)[2] = (unsigned char)(b / 0xffffff);
1175         (*output) += 3;
1176 }
1177
1178
1179 static inline void transfer_RGBA16161616_to_RGBA8888(unsigned char *(*output), uint16_t *input)
1180 {
1181         (*output)[0] = input[0] >> 8;
1182         (*output)[1] = input[1] >> 8;
1183         (*output)[2] = input[2] >> 8;
1184         (*output)[3] = input[3] >> 8;
1185         (*output) += 4;
1186 }
1187
1188
1189 static inline void transfer_RGBA16161616_to_BGR8888(unsigned char *(*output), uint16_t *input)
1190 {
1191         uint32_t r, g, b, a;
1192         a = input[3];
1193         r = (input)[0] * a;
1194         g = (input)[1] * a;
1195         b = (input)[2] * a;
1196
1197 // For display only
1198         (*output)[0] = (unsigned char)(b >> 24);
1199         (*output)[1] = (unsigned char)(g >> 24);
1200         (*output)[2] = (unsigned char)(r >> 24);
1201         (*output) += 4;
1202 }
1203
1204 static inline void transfer_RGBA16161616_to_RGB_FLOAT(float *(*output), uint16_t *input)
1205 {
1206         float opacity = (float)input[3];
1207         *(*output)++ = (float)*input++ * opacity / 0xffff / 0xffff;
1208         *(*output)++ = (float)*input++ * opacity / 0xffff / 0xffff;
1209         *(*output)++ = (float)*input * opacity / 0xffff / 0xffff;
1210 }
1211
1212 static inline void transfer_RGBA16161616_to_RGBA_FLOAT(float *(*output), uint16_t *input)
1213 {
1214         *(*output)++ = (float)*input++ / 0xffff;
1215         *(*output)++ = (float)*input++ / 0xffff;
1216         *(*output)++ = (float)*input++ / 0xffff;
1217         *(*output)++ = (float)*input / 0xffff;
1218 }
1219
1220 static inline void transfer_RGBA16161616_to_YUV888(unsigned char *(*output), uint16_t *input)
1221 {
1222         uint32_t y, u, v, r, g, b, a;
1223         
1224         a = input[3];
1225         r = (uint32_t)input[0] * a / 0xffffff;
1226         g = (uint32_t)input[1] * a / 0xffffff;
1227         b = (uint32_t)input[2] * a / 0xffffff;
1228         
1229         RGB_TO_YUV(y, u, v, r, g, b);
1230         
1231         (*output)[0] = y;
1232         (*output)[1] = u;
1233         (*output)[2] = v;
1234         (*output) += 3;
1235 }
1236
1237 static inline void transfer_RGBA16161616_to_YUVA8888(unsigned char *(*output), uint16_t *input)
1238 {
1239         int y, u, v, r, g, b;
1240
1241         r = input[0] >> 8;
1242         g = input[1] >> 8;
1243         b = input[2] >> 8;
1244         
1245         RGB_TO_YUV(y, u, v, r, g, b);
1246         
1247         (*output)[0] = y;
1248         (*output)[1] = u;
1249         (*output)[2] = v;
1250         (*output)[3] = input[3] >> 8;
1251         (*output) += 4;
1252 }
1253
1254 static inline void transfer_RGBA16161616_to_YUV161616(uint16_t *(*output), uint16_t *input)
1255 {
1256         uint32_t y, u, v, r, g, b, a;
1257         
1258         a = input[3];
1259         r = (uint32_t)input[0] * a / 0xffff;
1260         g = (uint32_t)input[1] * a / 0xffff;
1261         b = (uint32_t)input[2] * a / 0xffff;
1262         
1263         RGB_TO_YUV16(y, u, v, r, g, b);
1264         
1265         (*output)[0] = y;
1266         (*output)[1] = u;
1267         (*output)[2] = v;
1268         (*output) += 3;
1269 }
1270
1271 static inline void transfer_RGBA16161616_to_YUVA16161616(uint16_t *(*output), uint16_t *input)
1272 {
1273         uint32_t y, u, v, r, g, b;
1274         
1275         r = (uint32_t)input[0];
1276         g = (uint32_t)input[1];
1277         b = (uint32_t)input[2];
1278         
1279         RGB_TO_YUV16(y, u, v, r, g, b);
1280         
1281         (*output)[0] = y;
1282         (*output)[1] = u;
1283         (*output)[2] = v;
1284         (*output)[3] = input[3];
1285         (*output) += 4;
1286 }
1287
1288
1289 static inline void transfer_RGBA16161616_to_YUV101010(unsigned char *(*output), uint16_t *input)
1290 {
1291         uint32_t r, g, b, a, y, u, v;
1292
1293         a = input[3];
1294         r = (uint32_t)input[0] * a / 0xffff;
1295         g = (uint32_t)input[1] * a / 0xffff;
1296         b = (uint32_t)input[2] * a / 0xffff;
1297         RGB_TO_YUV16(y, u, v, r, g, b);
1298         WRITE_YUV101010(y, u, v);
1299 }
1300
1301
1302 static inline void transfer_RGBA16161616_to_YUV420P_YUV422P(unsigned char *output_y, 
1303         unsigned char *output_u, 
1304         unsigned char *output_v, 
1305         uint16_t *input,
1306         int output_column)
1307 {
1308         uint32_t y, u, v, r, g, b, a;
1309         a = input[3];
1310         r = (int32_t)input[0] * a / 0xffffff;
1311         g = (int32_t)input[1] * a / 0xffffff;
1312         b = (int32_t)input[2] * a / 0xffffff;
1313
1314         RGB_TO_YUV(y, u, v, r, g, b);
1315
1316         output_y[output_column] = y;
1317         output_u[output_column / 2] = u;
1318         output_v[output_column / 2] = v;
1319 }
1320
1321 static inline void transfer_RGBA16161616_to_YUV444P(unsigned char *output_y, 
1322         unsigned char *output_u, 
1323         unsigned char *output_v, 
1324         uint16_t *input,
1325         int output_column)
1326 {
1327         uint32_t y, u, v, r, g, b, a;
1328         a = input[3];
1329         r = (int32_t)input[0] * a / 0xffffff;
1330         g = (int32_t)input[1] * a / 0xffffff;
1331         b = (int32_t)input[2] * a / 0xffffff;
1332
1333         RGB_TO_YUV(y, u, v, r, g, b);
1334
1335         output_y[output_column] = y;
1336         output_u[output_column] = u;
1337         output_v[output_column] = v;
1338 }
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362 // ********************************** screen capture *****************************
1363
1364 static inline void transfer_BGR8888_to_RGB888(unsigned char *(*output), unsigned char *input)
1365 {
1366         *(*output)++ = input[2];
1367         *(*output)++ = input[1];
1368         *(*output)++ = input[0];
1369 }
1370
1371 static inline void transfer_BGR8888_to_BGR8888(unsigned char *(*output), unsigned char *input)
1372 {
1373         *(*output)++ = input[0];
1374         *(*output)++ = input[1];
1375         *(*output)++ = input[2];
1376         (*output)++;
1377 }
1378
1379 static inline void transfer_BGR888_to_RGB888(unsigned char *(*output), unsigned char *input)
1380 {
1381         *(*output)++ = input[2];
1382         *(*output)++ = input[1];
1383         *(*output)++ = input[0];
1384 }
1385
1386
1387
1388
1389
1390
1391
1392 // ******************************** YUV888 -> *********************************
1393
1394
1395 static inline void transfer_YUV888_to_RGB8(unsigned char *(*output), unsigned char *input)
1396 {
1397         int y, u, v;
1398         int r, g, b;
1399         
1400         y = (input[0] << 16) | (input[0] << 8) | input[0];
1401         u = input[1];
1402         v = input[2];
1403         YUV_TO_RGB(y, u, v, r, g, b);
1404
1405         *(*output) = (unsigned char)((r & 0xc0) +
1406                                                  ((g & 0xe0) >> 2) +
1407                                                  ((b & 0xe0) >> 5));
1408         (*output)++;
1409 }
1410
1411 static inline void transfer_YUV888_to_BGR565(unsigned char *(*output), unsigned char *input)
1412 {
1413         int y, u, v;
1414         int r, g, b;
1415         
1416         y = (input[0] << 16) | (input[0] << 8) | input[0];
1417         u = input[1];
1418         v = input[2];
1419         YUV_TO_RGB(y, u, v, r, g, b);
1420         *(uint16_t*)(*output) = ((b & 0xf8) << 8)
1421                          + ((g & 0xfc) << 3)
1422                          + ((r & 0xf8) >> 3);
1423         (*output) += 2;
1424 }
1425
1426 static inline void transfer_YUV888_to_RGB565(unsigned char *(*output), unsigned char *input)
1427 {
1428         int y, u, v;
1429         int r, g, b;
1430         
1431         y = (input[0] << 16) | (input[0] << 8) | input[0];
1432         u = input[1];
1433         v = input[2];
1434         YUV_TO_RGB(y, u, v, r, g, b);
1435         *(uint16_t*)(*output) = ((r & 0xf8) << 8)
1436                          + ((g & 0xfc) << 3)
1437                          + ((b & 0xf8) >> 3);
1438         (*output) += 2;
1439 }
1440
1441 static inline void transfer_YUV888_to_BGR888(unsigned char *(*output), unsigned char *input)
1442 {
1443         int y, u, v;
1444         int r, g, b;
1445         
1446         y = (input[0] << 16) | (input[0] << 8) | input[0];
1447         u = input[1];
1448         v = input[2];
1449         YUV_TO_RGB(y, u, v, r, g, b);
1450
1451         *(*output)++ = b;
1452         *(*output)++ = g;
1453         *(*output)++ = r;
1454 }
1455
1456 static inline void transfer_YUV888_to_BGR8888(unsigned char *(*output), unsigned char *input)
1457 {
1458         int y, u, v;
1459         int r, g, b;
1460
1461         y = (input[0] << 16) | (input[0] << 8) | input[0];
1462         u = input[1];
1463         v = input[2];
1464         YUV_TO_RGB(y, u, v, r, g, b);
1465         *(*output)++ = b;
1466         *(*output)++ = g;
1467         *(*output)++ = r;
1468         (*output)++;
1469 }
1470
1471 static inline void transfer_YUV888_to_RGB888(unsigned char *(*output), unsigned char *input)
1472 {
1473         int y, u, v;
1474         int r, g, b;
1475         
1476         y = (input[0] << 16) | (input[0] << 8) | input[0];
1477         u = (int)input[1];
1478         v = (int)input[2];
1479         YUV_TO_RGB(y, u, v, r, g, b);
1480
1481         *(*output)++ = r;
1482         *(*output)++ = g;
1483         *(*output)++ = b;
1484 }
1485
1486 static inline void transfer_YUV888_to_RGBA8888(unsigned char *(*output), unsigned char *input)
1487 {
1488         int y, u, v;
1489         int r, g, b;
1490
1491         y = (input[0] << 16) | (input[0] << 8) | input[0];
1492         u = input[1];
1493         v = input[2];
1494         YUV_TO_RGB(y, u, v, r, g, b);
1495         *(*output)++ = r;
1496         *(*output)++ = g;
1497         *(*output)++ = b;
1498         *(*output)++ = 0xff;
1499 }
1500
1501 static inline void transfer_YUV888_to_ARGB8888(unsigned char *(*output), unsigned char *input)
1502 {
1503         int y, u, v;
1504         int r, g, b;
1505
1506         y = (input[0] << 16) | (input[0] << 8) | input[0];
1507         u = input[1];
1508         v = input[2];
1509         YUV_TO_RGB(y, u, v, r, g, b);
1510         *(*output)++ = 0xff;
1511         *(*output)++ = r;
1512         *(*output)++ = g;
1513         *(*output)++ = b;
1514 }
1515
1516 static inline void transfer_YUV888_to_RGB_FLOAT(float *(*output), unsigned char *input)
1517 {
1518         float y = (float)input[0] / 0xff;
1519         int u = input[1];
1520         int v = input[2];
1521         float r, g, b;
1522         
1523         YUV_TO_FLOAT(y, u, v, r, g, b);
1524
1525         *(*output)++ = r;
1526         *(*output)++ = g;
1527         *(*output)++ = b;
1528 }
1529
1530 static inline void transfer_YUV888_to_RGBA_FLOAT(float *(*output), unsigned char *input)
1531 {
1532         float y = (float)input[0] / 0xff;
1533         int u = input[1];
1534         int v = input[2];
1535         float r, g, b;
1536         
1537         YUV_TO_FLOAT(y, u, v, r, g, b);
1538
1539         *(*output)++ = r;
1540         *(*output)++ = g;
1541         *(*output)++ = b;
1542         *(*output)++ = 1.0;
1543 }
1544
1545 static inline void transfer_YUV888_to_YUVA8888(unsigned char *(*output), unsigned char *input)
1546 {
1547         *(*output)++ = (int)input[0];
1548         *(*output)++ = input[1];
1549         *(*output)++ = input[2];
1550         *(*output)++ = 0xff;
1551 }
1552
1553 static inline void transfer_YUV888_to_YUV888(unsigned char *(*output), unsigned char *input)
1554 {
1555         (*output)[0] = (int)input[0];
1556         (*output)[1] = input[1];
1557         (*output)[2] = input[2];
1558         (*output) += 3;
1559 }
1560
1561
1562 static inline void transfer_YUV888_to_VYU888(unsigned char *(*output), unsigned char *input)
1563 {
1564         (*output)[0] = input[2];
1565         (*output)[1] = input[0];
1566         (*output)[2] = input[1];
1567         (*output) += 3;
1568 }
1569
1570
1571 static inline void transfer_YUV888_to_UYVA8888(unsigned char *(*output), unsigned char *input)
1572 {
1573         (*output)[0] = input[1];
1574         (*output)[1] = input[0];
1575         (*output)[2] = input[2];
1576         (*output)[3] = 0xff;
1577         (*output) += 4;
1578 }
1579
1580
1581 static inline void transfer_YUV888_to_YUV101010(unsigned char *(*output), unsigned char *input)
1582 {
1583         uint16_t y_i = ((uint16_t)input[0]) << 8;
1584         uint16_t u_i = ((uint16_t)input[1]) << 8;
1585         uint16_t v_i = ((uint16_t)input[2]) << 8;
1586         WRITE_YUV101010(y_i, u_i, v_i);
1587 }
1588
1589 static inline void transfer_YUV888_to_YUV420P_YUV422P(unsigned char *output_y, 
1590         unsigned char *output_u, 
1591         unsigned char *output_v, 
1592         unsigned char *input,
1593         int output_column)
1594 {
1595         output_y[output_column] = input[0];
1596         output_u[output_column / 2] = input[1];
1597         output_v[output_column / 2] = input[2];
1598 }
1599
1600 static inline void transfer_YUV888_to_YUV444P(unsigned char *output_y, 
1601         unsigned char *output_u, 
1602         unsigned char *output_v, 
1603         unsigned char *input,
1604         int output_column)
1605 {
1606         output_y[output_column] = input[0];
1607         output_u[output_column] = input[1];
1608         output_v[output_column] = input[2];
1609 }
1610
1611 static inline void transfer_YUV888_to_YUV422(unsigned char *(*output), 
1612         unsigned char *input,
1613         int j)
1614 {
1615 // Store U and V for even pixels only
1616         if(!(j & 1))
1617         {
1618                 (*output)[1] = input[1];
1619                 (*output)[3] = input[2];
1620                 (*output)[0] = input[0];
1621         }
1622         else
1623 // Store Y and advance output for odd pixels only
1624         {
1625                 (*output)[2] = input[0];
1626                 (*output) += 4;
1627         }
1628 }
1629
1630
1631
1632
1633
1634
1635 // ******************************** YUVA8888 -> *******************************
1636
1637
1638
1639
1640 static inline void transfer_YUVA8888_to_RGB8(unsigned char *(*output), unsigned char *input)
1641 {
1642         int y, u, v, a;
1643         int r, g, b;
1644         
1645         a = input[3];
1646         y = (input[0] << 16) | (input[0] << 8) | input[0];
1647         u = input[1];
1648         v = input[2];
1649         YUV_TO_RGB(y, u, v, r, g, b);
1650         
1651         r *= a;
1652         g *= a;
1653         b *= a;
1654
1655         *(*output) = (unsigned char)(((r & 0xc000) >> 8) + 
1656                                 ((g & 0xe000) >> 10) + 
1657                                 ((b & 0xe000) >> 13));
1658         (*output)++;
1659 }
1660
1661 static inline void transfer_YUVA8888_to_BGR565(unsigned char *(*output), unsigned char *input)
1662 {
1663         int y, u, v, a;
1664         int r, g, b;
1665         
1666         a = input[3];
1667         y = (input[0] << 16) | (input[0] << 8) | input[0];
1668         u = input[1];
1669         v = input[2];
1670         YUV_TO_RGB(y, u, v, r, g, b);
1671                 
1672         r *= a;
1673         g *= a;
1674         b *= a;
1675
1676         *(uint16_t*)(*output) = (uint16_t)((b & 0xf800) + 
1677                                 ((g & 0xfc00) >> 5) + 
1678                                 ((r & 0xf800) >> 11));
1679         (*output) += 2;
1680 }
1681
1682 static inline void transfer_YUVA8888_to_RGB565(unsigned char *(*output), unsigned char *input)
1683 {
1684         int y, u, v, a;
1685         int r, g, b;
1686         
1687         a = input[3];
1688         y = (input[0] << 16) | (input[0] << 8) | input[0];
1689         u = input[1];
1690         v = input[2];
1691         YUV_TO_RGB(y, u, v, r, g, b);
1692                 
1693         r *= a;
1694         g *= a;
1695         b *= a;
1696
1697         *(uint16_t*)(*output) = (uint16_t)((r & 0xf800) + 
1698                                 ((g & 0xfc00) >> 5) + 
1699                                 ((b & 0xf800) >> 11));
1700         (*output) += 2;
1701 }
1702
1703 static inline void transfer_YUVA8888_to_BGR888(unsigned char *(*output), unsigned char *input)
1704 {
1705         int y, u, v, a;
1706         int r, g, b;
1707         
1708         a = input[3];
1709         y = (input[0] << 16) | (input[0] << 8) | input[0];
1710         u = input[1];
1711         v = input[2];
1712
1713         YUV_TO_RGB(y, u, v, r, g, b);
1714                 
1715         r *= a;
1716         g *= a;
1717         b *= a;
1718
1719         *(*output)++ = b / 0xff;
1720         *(*output)++ = g / 0xff;
1721         *(*output)++ = r / 0xff;
1722 }
1723
1724
1725 static inline void transfer_YUVA8888_to_BGR8888(unsigned char *(*output), unsigned char *input)
1726 {
1727         int y, u, v, a;
1728         int r, g, b;
1729
1730         a = input[3];
1731         y = (input[0] << 16) | (input[0] << 8) | input[0];
1732         u = input[1];
1733         v = input[2];
1734
1735         YUV_TO_RGB(y, u, v, r, g, b);
1736         
1737         r *= a;
1738         g *= a;
1739         b *= a;
1740         *(*output)++ = b / 0xff;
1741         *(*output)++ = g / 0xff;
1742         *(*output)++ = r / 0xff;
1743         (*output)++;
1744 }
1745
1746 static inline void transfer_YUVA8888_to_RGB888(unsigned char *(*output), unsigned char *input)
1747 {
1748         int y, u, v, a;
1749         int r, g, b;
1750         
1751         a = input[3];
1752         y = (input[0] << 16) | (input[0] << 8) | input[0];
1753         u = input[1];
1754         v = input[2];
1755
1756         YUV_TO_RGB(y, u, v, r, g, b);
1757                 
1758         r *= a;
1759         g *= a;
1760         b *= a;
1761
1762         *(*output)++ = r / 0xff;
1763         *(*output)++ = g / 0xff;
1764         *(*output)++ = b / 0xff;
1765 }
1766
1767 static inline void transfer_YUVA8888_to_RGBA8888(unsigned char *(*output), unsigned char *input)
1768 {
1769         int y, u, v, a;
1770         int r, g, b;
1771
1772         a = input[3];
1773         y = (input[0] << 16) | (input[0] << 8) | input[0];
1774         u = input[1];
1775         v = input[2];
1776
1777         YUV_TO_RGB(y, u, v, r, g, b);
1778         *(*output)++ = r;
1779         *(*output)++ = g;
1780         *(*output)++ = b;
1781         *(*output)++ = a;
1782 }
1783
1784 static inline void transfer_YUVA8888_to_ARGB8888(unsigned char *(*output), unsigned char *input)
1785 {
1786         int y, u, v, a;
1787         int r, g, b;
1788
1789         a = input[3];
1790         y = (input[0] << 16) | (input[0] << 8) | input[0];
1791         u = input[1];
1792         v = input[2];
1793
1794         YUV_TO_RGB(y, u, v, r, g, b);
1795         *(*output)++ = a;
1796         *(*output)++ = r;
1797         *(*output)++ = g;
1798         *(*output)++ = b;
1799 }
1800
1801 static inline void transfer_YUVA8888_to_RGB_FLOAT(float *(*output), unsigned char *input)
1802 {
1803         float y, a;
1804         int u, v;
1805         float r, g, b;
1806
1807         a = (float)input[3] / 0xff;
1808         y = (float)input[0] / 0xff;
1809         u = input[1];
1810         v = input[2];
1811
1812         YUV_TO_FLOAT(y, u, v, r, g, b);
1813                 
1814         r *= a;
1815         g *= a;
1816         b *= a;
1817
1818         *(*output)++ = r;
1819         *(*output)++ = g;
1820         *(*output)++ = b;
1821 }
1822
1823 static inline void transfer_YUVA8888_to_RGBA_FLOAT(float *(*output), unsigned char *input)
1824 {
1825         float y = (float)input[0] / 0xff;
1826         int u, v;
1827         float r, g, b, a;
1828
1829         a = (float)input[3] / 0xff;
1830         u = input[1];
1831         v = input[2];
1832
1833         YUV_TO_FLOAT(y, u, v, r, g, b);
1834
1835         *(*output)++ = r;
1836         *(*output)++ = g;
1837         *(*output)++ = b;
1838         *(*output)++ = a;
1839 }
1840
1841 static inline void transfer_YUVA8888_to_YUV888(unsigned char *(*output), unsigned char *input)
1842 {
1843         int y, u, v, a, anti_a;
1844         a = input[3];
1845         anti_a = 0xff - a;
1846         y = ((uint32_t)input[0] * a) / 0xff;
1847         u = ((uint32_t)input[1] * a + 0x80 * anti_a) / 0xff;
1848         v = ((uint32_t)input[2] * a + 0x80 * anti_a) / 0xff;
1849         
1850         *(*output)++ = y;
1851         *(*output)++ = u;
1852         *(*output)++ = v;
1853 }
1854
1855
1856
1857 static inline void transfer_YUVA8888_to_VYU888(unsigned char *(*output), unsigned char *input)
1858 {
1859         int y, u, v, a, anti_a;
1860         a = input[3];
1861         anti_a = 0xff - a;
1862         y = ((uint32_t)input[0] * a) / 0xff;
1863         u = ((uint32_t)input[1] * a + 0x80 * anti_a) / 0xff;
1864         v = ((uint32_t)input[2] * a + 0x80 * anti_a) / 0xff;
1865         
1866         *(*output)++ = v;
1867         *(*output)++ = y;
1868         *(*output)++ = u;
1869 }
1870
1871
1872 static inline void transfer_YUVA8888_to_YUVA8888(unsigned char *(*output), unsigned char *input)
1873 {
1874         *(*output)++ = input[0];
1875         *(*output)++ = input[1];
1876         *(*output)++ = input[2];
1877         *(*output)++ = input[3];
1878 }
1879
1880 static inline void transfer_YUVA8888_to_UYVA8888(unsigned char *(*output), unsigned char *input)
1881 {
1882         *(*output)++ = input[1];
1883         *(*output)++ = input[0];
1884         *(*output)++ = input[2];
1885         *(*output)++ = input[3];
1886 }
1887
1888 static inline void transfer_YUVA8888_to_YUV101010(unsigned char *(*output), unsigned char *input)
1889 {
1890         uint16_t y_i = ((uint16_t)input[0] * input[3]) + 0x1fe;
1891         uint16_t u_i = ((uint16_t)input[1] * input[3]) + 0x1fe;
1892         uint16_t v_i = ((uint16_t)input[2] * input[3]) + 0x1fe;
1893         WRITE_YUV101010(y_i, u_i, v_i);
1894 }
1895
1896
1897 static inline void transfer_YUVA8888_to_YUV420P_YUV422P(unsigned char *output_y, 
1898         unsigned char *output_u, 
1899         unsigned char *output_v, 
1900         unsigned char *input,
1901         int output_column)
1902 {
1903         int opacity = input[3];
1904         int transparency = 0xff - opacity;
1905
1906         output_y[output_column] =     ((input[0] * opacity) >> 8) + 1;
1907         output_u[output_column / 2] = ((input[1] * opacity + 0x80 * transparency) >> 8) + 1;
1908         output_v[output_column / 2] = ((input[2] * opacity + 0x80 * transparency) >> 8) + 1;
1909 }
1910
1911 static inline void transfer_YUVA8888_to_YUV444P(unsigned char *output_y, 
1912         unsigned char *output_u, 
1913         unsigned char *output_v, 
1914         unsigned char *input,
1915         int output_column)
1916 {
1917         int opacity = input[3];
1918         int transparency = 0xff - opacity;
1919
1920         output_y[output_column] =     ((input[0] * opacity) >> 8) + 1;
1921         output_u[output_column] = ((input[1] * opacity + 0x80 * transparency) >> 8) + 1;
1922         output_v[output_column] = ((input[2] * opacity + 0x80 * transparency) >> 8) + 1;
1923 }
1924
1925 static inline void transfer_YUVA8888_to_YUV422(unsigned char *(*output), 
1926         unsigned char *input,
1927         int j)
1928 {
1929         int opacity = input[3];
1930         int transparency = 0xff - opacity;
1931 // Store U and V for even pixels only
1932         if(!(j & 1))
1933         {
1934                 (*output)[0] = ((input[0] * opacity) >> 8) + 1;
1935                 (*output)[1] = ((input[1] * opacity + 0x80 * transparency) >> 8) + 1;
1936                 (*output)[3] = ((input[2] * opacity + 0x80 * transparency) >> 8) + 1;
1937         }
1938         else
1939 // Store Y and advance output for odd pixels only
1940         {
1941                 (*output)[2] = ((input[0] * opacity) >> 8) + 1;
1942                 (*output) += 4;
1943         }
1944 }
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955 // ******************************** YUV161616 -> ******************************
1956
1957
1958 static inline void transfer_YUV161616_to_RGB8(unsigned char *(*output), uint16_t *input)
1959 {
1960         int y, u, v;
1961         int r, g, b;
1962         
1963         y = (((int)input[0]) << 8) | (input[0] >> 8);
1964         u = input[1] >> 8;
1965         v = input[2] >> 8;
1966         YUV_TO_RGB(y, u, v, r, g, b);
1967
1968         *(*output) = (unsigned char)((r & 0xc0) +
1969                                                  ((g & 0xe0) >> 2) +
1970                                                  ((b & 0xe0) >> 5));
1971         (*output)++;
1972 }
1973
1974 static inline void transfer_YUV161616_to_BGR565(unsigned char *(*output), uint16_t *input)
1975 {
1976         int y, u, v;
1977         int r, g, b;
1978         
1979         y = (((int)input[0]) << 8) | (input[0] >> 8);
1980         u = input[1] >> 8;
1981         v = input[2] >> 8;
1982         YUV_TO_RGB(y, u, v, r, g, b);
1983         *(uint16_t*)(*output) = ((b & 0xf8) << 8)
1984                          + ((g & 0xfc) << 3)
1985                          + ((r & 0xf8) >> 3);
1986         (*output) += 2;
1987 }
1988
1989 static inline void transfer_YUV161616_to_RGB565(unsigned char *(*output), uint16_t *input)
1990 {
1991         int y, u, v;
1992         int r, g, b;
1993         
1994         y = (((int)input[0]) << 8) | (input[0] >> 8);
1995         u = input[1] >> 8;
1996         v = input[2] >> 8;
1997         YUV_TO_RGB(y, u, v, r, g, b);
1998         *(uint16_t*)(*output) = ((r & 0xf8) << 8)
1999                          + ((g & 0xfc) << 3)
2000                          + ((b & 0xf8) >> 3);
2001         (*output) += 2;
2002 }
2003
2004 static inline void transfer_YUV161616_to_BGR888(unsigned char *(*output), uint16_t *input)
2005 {
2006         int y, u, v;
2007         int r, g, b;
2008         
2009         y = (((int)input[0]) << 8) | (input[0] >> 8);
2010         u = input[1];
2011         v = input[2];
2012         YUV_TO_RGB16(y, u, v, r, g, b);
2013
2014         *(*output)++ = b >> 8;
2015         *(*output)++ = g >> 8;
2016         *(*output)++ = r >> 8;
2017 }
2018
2019 static inline void transfer_YUV161616_to_RGB888(unsigned char *(*output), uint16_t *input)
2020 {
2021         int y, u, v;
2022         int r, g, b;
2023         
2024         y = (((int)input[0]) << 8) | (input[0] >> 8);
2025         u = input[1];
2026         v = input[2];
2027         YUV_TO_RGB16(y, u, v, r, g, b);
2028
2029         *(*output)++ = r >> 8;
2030         *(*output)++ = g >> 8;
2031         *(*output)++ = b >> 8;
2032 }
2033
2034 static inline void transfer_YUV161616_to_RGBA8888(unsigned char *(*output), uint16_t *input)
2035 {
2036         int y, u, v;
2037         int r, g, b;
2038         
2039         y = (((int)input[0]) << 8) | (input[0] >> 8);
2040         u = input[1];
2041         v = input[2];
2042         YUV_TO_RGB16(y, u, v, r, g, b);
2043
2044         *(*output)++ = r >> 8;
2045         *(*output)++ = g >> 8;
2046         *(*output)++ = b >> 8;
2047         *(*output)++ = 0xff;
2048 }
2049
2050 static inline void transfer_YUV161616_to_ARGB8888(unsigned char *(*output), uint16_t *input)
2051 {
2052         int y, u, v;
2053         int r, g, b;
2054         
2055         y = (((int)input[0]) << 8) | (input[0] >> 8);
2056         u = input[1];
2057         v = input[2];
2058         YUV_TO_RGB16(y, u, v, r, g, b);
2059
2060         *(*output)++ = 0xff;
2061         *(*output)++ = r >> 8;
2062         *(*output)++ = g >> 8;
2063         *(*output)++ = b >> 8;
2064 }
2065
2066 static inline void transfer_YUV161616_to_RGB_FLOAT(float *(*output), uint16_t *input)
2067 {
2068         float y = (float)input[0] / 0xffff;
2069         int u, v;
2070         float r, g, b;
2071         
2072         u = input[1];
2073         v = input[2];
2074         YUV16_TO_RGB_FLOAT(y, u, v, r, g, b);
2075
2076         *(*output)++ = r;
2077         *(*output)++ = g;
2078         *(*output)++ = b;
2079 }
2080
2081 static inline void transfer_YUV161616_to_RGBA_FLOAT(float *(*output), uint16_t *input)
2082 {
2083         float y = (float)input[0] / 0xffff;
2084         int u, v;
2085         float r, g, b;
2086         
2087         u = input[1];
2088         v = input[2];
2089         YUV16_TO_RGB_FLOAT(y, u, v, r, g, b);
2090
2091         *(*output)++ = r;
2092         *(*output)++ = g;
2093         *(*output)++ = b;
2094         *(*output)++ = 1.0;
2095 }
2096
2097 static inline void transfer_YUV161616_to_BGR8888(unsigned char *(*output), uint16_t *input)
2098 {
2099         int y, u, v;
2100         int r, g, b;
2101
2102         y = (((int)input[0]) << 8) | (input[0] >> 8);
2103         u = input[1] >> 8;
2104         v = input[2] >> 8;
2105         YUV_TO_RGB(y, u, v, r, g, b);
2106         *(*output)++ = b;
2107         *(*output)++ = g;
2108         *(*output)++ = r;
2109         (*output)++;
2110 }
2111
2112 static inline void transfer_YUV161616_to_YUV161616(uint16_t *(*output), uint16_t *input)
2113 {
2114         (*output)[0] = input[0];
2115         (*output)[1] = input[1];
2116         (*output)[2] = input[2];
2117         (*output) += 3;
2118 }
2119
2120 static inline void transfer_YUV161616_to_YUVA8888(unsigned char *(*output), uint16_t *input)
2121 {
2122         (*output)[0] = input[0] >> 8;
2123         (*output)[1] = input[1] >> 8;
2124         (*output)[2] = input[2] >> 8;
2125         (*output)[3] = 255;
2126         (*output) += 4;
2127 }
2128
2129
2130 static inline void transfer_YUV161616_to_VYU888(unsigned char *(*output), uint16_t *input)
2131 {
2132         (*output)[0] = input[2] >> 8;
2133         (*output)[1] = input[0] >> 8;
2134         (*output)[2] = input[1] >> 8;
2135         (*output) += 3;
2136 }
2137
2138
2139 static inline void transfer_YUV161616_to_UYVA8888(unsigned char *(*output), uint16_t *input)
2140 {
2141         (*output)[0] = input[1] >> 8;
2142         (*output)[1] = input[0] >> 8;
2143         (*output)[2] = input[2] >> 8;
2144         (*output)[3] = input[3] >> 8;
2145         (*output) += 4;
2146 }
2147
2148 static inline void transfer_YUV161616_to_YUV101010(unsigned char *(*output), uint16_t *input)
2149 {
2150         uint16_t y_i = input[0];
2151         uint16_t u_i = input[1];
2152         uint16_t v_i = input[2];
2153         WRITE_YUV101010(y_i, u_i, v_i);
2154 }
2155
2156 static inline void transfer_YUV161616_to_YUV420P_YUV422P(unsigned char *output_y, 
2157         unsigned char *output_u, 
2158         unsigned char *output_v, 
2159         uint16_t *input,
2160         int output_column)
2161 {
2162         output_y[output_column] = input[0] >> 8;
2163         output_u[output_column / 2] = input[1] >> 8;
2164         output_v[output_column / 2] = input[2] >> 8;
2165 }
2166
2167 static inline void transfer_YUV161616_to_YUV444P(unsigned char *output_y, 
2168         unsigned char *output_u, 
2169         unsigned char *output_v, 
2170         uint16_t *input,
2171         int output_column)
2172 {
2173         output_y[output_column] = input[0] >> 8;
2174         output_u[output_column] = input[1] >> 8;
2175         output_v[output_column] = input[2] >> 8;
2176 }
2177
2178 static inline void transfer_YUV161616_to_YUV422(unsigned char *(*output), 
2179         uint16_t *input,
2180         int j)
2181 {
2182 // Store U and V for even pixels only
2183         if(!(j & 1))
2184         {
2185                 (*output)[1] = input[1] >> 8;
2186                 (*output)[3] = input[2] >> 8;
2187                 (*output)[0] = input[0] >> 8;
2188         }
2189         else
2190 // Store Y and advance output for odd pixels only
2191         {
2192                 (*output)[2] = input[0] >> 8;
2193                 (*output) += 4;
2194         }
2195 }
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207 // ******************************** YUVA16161616 -> ***************************
2208
2209
2210
2211
2212 static inline void transfer_YUVA16161616_to_RGB8(unsigned char *(*output), uint16_t *input)
2213 {
2214         int y, u, v, a;
2215         int r, g, b;
2216         
2217         a = input[3];
2218         y = (((int)input[0]) << 8) | (input[0] >> 8);
2219         u = input[1] >> 8;
2220         v = input[2] >> 8;
2221         YUV_TO_RGB(y, u, v, r, g, b);
2222         
2223         r *= a;
2224         g *= a;
2225         b *= a;
2226
2227         *(*output) = (unsigned char)(((r & 0xc000) >> 8) + 
2228                                 ((g & 0xe000) >> 10) + 
2229                                 ((b & 0xe000) >> 13));
2230         (*output)++;
2231 }
2232
2233 static inline void transfer_YUVA16161616_to_BGR565(unsigned char *(*output), uint16_t *input)
2234 {
2235         int y, u, v, a;
2236         int r, g, b;
2237         
2238         a = input[3] >> 8;
2239         y = (((int)input[0]) << 8) | (input[0] >> 8);
2240         u = input[1] >> 8;
2241         v = input[2] >> 8;
2242         YUV_TO_RGB(y, u, v, r, g, b);
2243                 
2244         r *= a;
2245         g *= a;
2246         b *= a;
2247
2248         *(uint16_t*)(*output) = (uint16_t)((b & 0xf800) + 
2249                                 ((g & 0xfc00) >> 5) + 
2250                                 ((r & 0xf800) >> 11));
2251         (*output) += 2;
2252 }
2253
2254 static inline void transfer_YUVA16161616_to_RGB565(unsigned char *(*output), uint16_t *input)
2255 {
2256         int y, u, v, a;
2257         int r, g, b;
2258         
2259         a = input[3] >> 8;
2260         y = (((int)input[0]) << 8) | (input[0] >> 8);
2261         u = input[1] >> 8;
2262         v = input[2] >> 8;
2263         YUV_TO_RGB(y, u, v, r, g, b);
2264                 
2265         r *= a;
2266         g *= a;
2267         b *= a;
2268
2269         *(uint16_t*)(*output) = (uint16_t)((r & 0xf800) + 
2270                                 ((g & 0xfc00) >> 5) + 
2271                                 ((b & 0xf800) >> 11));
2272         (*output) += 2;
2273 }
2274
2275 static inline void transfer_YUVA16161616_to_BGR888(unsigned char *(*output), uint16_t *input)
2276 {
2277         int y, u, v, a;
2278         int r, g, b;
2279         
2280         a = input[3];
2281         y = (((int)input[0]) << 8) | (input[0] >> 8);
2282         u = input[1];
2283         v = input[2];
2284
2285         YUV_TO_RGB16(y, u, v, r, g, b);
2286                 
2287         r *= a;
2288         g *= a;
2289         b *= a;
2290
2291         *(*output)++ = b / 0xffff00;
2292         *(*output)++ = g / 0xffff00;
2293         *(*output)++ = r / 0xffff00;
2294 }
2295
2296 static inline void transfer_YUVA16161616_to_RGB888(unsigned char *(*output), uint16_t *input)
2297 {
2298         unsigned int y, u, v, a;
2299         unsigned int r, g, b;
2300
2301         a = input[3];
2302         y = (((int)input[0]) << 8) | (input[0] >> 8);
2303         u = input[1];
2304         v = input[2];
2305
2306         YUV_TO_RGB16(y, u, v, r, g, b);
2307         
2308         r *= a;
2309         g *= a;
2310         b *= a;
2311         r /= 0xffff00;
2312         g /= 0xffff00;
2313         b /= 0xffff00;
2314
2315         CLAMP(r, 0, 0xff);
2316         CLAMP(g, 0, 0xff);
2317         CLAMP(b, 0, 0xff);
2318
2319         *(*output)++ = r;
2320         *(*output)++ = g;
2321         *(*output)++ = b;
2322 }
2323
2324 static inline void transfer_YUVA16161616_to_RGBA8888(unsigned char *(*output), uint16_t *input)
2325 {
2326         unsigned int y, u, v;
2327         unsigned int r, g, b;
2328         
2329         y = (((int)input[0]) << 8) | (input[0] >> 8);
2330         u = input[1];
2331         v = input[2];
2332
2333         YUV_TO_RGB16(y, u, v, r, g, b);
2334
2335         *(*output)++ = (r >> 8);
2336         *(*output)++ = (g >> 8);
2337         *(*output)++ = (b >> 8);
2338         *(*output)++ = input[3] >> 8;
2339 }
2340
2341 static inline void transfer_YUVA16161616_to_ARGB8888(unsigned char *(*output), uint16_t *input)
2342 {
2343         unsigned int y, u, v;
2344         unsigned int r, g, b;
2345         
2346         y = (((int)input[0]) << 8) | (input[0] >> 8);
2347         u = input[1];
2348         v = input[2];
2349
2350         YUV_TO_RGB16(y, u, v, r, g, b);
2351
2352         *(*output)++ = input[3] >> 8;
2353         *(*output)++ = (r >> 8);
2354         *(*output)++ = (g >> 8);
2355         *(*output)++ = (b >> 8);
2356 }
2357
2358 static inline void transfer_YUVA16161616_to_RGB_FLOAT(float *(*output), 
2359         uint16_t *input)
2360 {
2361         float y;
2362         int u, v;
2363         float r, g, b, a;
2364
2365         y = (float)input[0] / 0xffff;
2366         u = input[1];
2367         v = input[2];
2368         a = (float)input[3] / 0xffff;
2369
2370         YUV16_TO_RGB_FLOAT(y, u, v, r, g, b);
2371         
2372         r *= a;
2373         g *= a;
2374         b *= a;
2375
2376         *(*output)++ = r;
2377         *(*output)++ = g;
2378         *(*output)++ = b;
2379 }
2380
2381 static inline void transfer_YUVA16161616_to_RGBA_FLOAT(float *(*output), 
2382         uint16_t *input)
2383 {
2384         float y;
2385         int u, v;
2386         float r, g, b, a;
2387         
2388         y = (float)input[0] / 0xffff;
2389         u = input[1];
2390         v = input[2];
2391         a = (float)input[3] / 0xffff;
2392
2393         YUV16_TO_RGB_FLOAT(y, u, v, r, g, b);
2394
2395         *(*output)++ = r;
2396         *(*output)++ = g;
2397         *(*output)++ = b;
2398         *(*output)++ = a;
2399 }
2400
2401 static inline void transfer_YUVA16161616_to_BGR8888(unsigned char *(*output), 
2402         uint16_t *input)
2403 {
2404         int y, u, v, a;
2405         int64_t r, g, b;
2406
2407         a = input[3];
2408         y = (((int)input[0]) << 8) | (input[0] >> 8);
2409         u = input[1] >> 8;
2410         v = input[2] >> 8;
2411
2412         YUV_TO_RGB(y, u, v, r, g, b);
2413
2414         r *= a;
2415         g *= a;
2416         b *= a;
2417         b /= 0xffff;
2418         g /= 0xffff;
2419         r /= 0xffff;
2420
2421         *(*output)++ = b;
2422         *(*output)++ = g;
2423         *(*output)++ = r;
2424         (*output)++;
2425 }
2426
2427
2428 static inline void transfer_YUVA16161616_to_VYU888(unsigned char *(*output), uint16_t *input)
2429 {
2430         int y, u, v, a, anti_a;
2431         a = input[3];
2432         anti_a = 0xffff - a;
2433         y = ((uint32_t)input[0] * a) / 0xffff00;
2434         u = ((uint32_t)input[1] * a + 0x8000 * anti_a) / 0xffff00;
2435         v = ((uint32_t)input[2] * a + 0x8000 * anti_a) / 0xffff00;
2436
2437         *(*output)++ = v;
2438         *(*output)++ = y;
2439         *(*output)++ = u;
2440 }
2441
2442
2443 static inline void transfer_YUVA16161616_to_YUVA16161616(uint16_t *(*output), uint16_t *input)
2444 {
2445         (*output)[0] = input[0];
2446         (*output)[1] = input[1];
2447         (*output)[2] = input[2];
2448         (*output)[3] = input[3];
2449         (*output) += 4;
2450 }
2451
2452 static inline void transfer_YUVA16161616_to_UYVA8888(unsigned char *(*output), uint16_t *input)
2453 {
2454         (*output)[0] = input[1] >> 8;
2455         (*output)[1] = input[0] >> 8;
2456         (*output)[2] = input[2] >> 8;
2457         (*output)[3] = input[3] >> 8;
2458         (*output) += 4;
2459 }
2460
2461
2462 static inline void transfer_YUVA16161616_to_YUV101010(unsigned char *(*output), uint16_t *input)
2463 {
2464         int64_t opacity = input[3];
2465         int64_t transparency = 0xffff - opacity;
2466         uint16_t y_i = ((int64_t)input[0] * opacity + (int64_t)0x8000 * transparency) / 0xffff;
2467         uint16_t u_i = ((int64_t)input[1] * opacity + (int64_t)0x8000 * transparency) / 0xffff;
2468         uint16_t v_i = ((int64_t)input[2] * opacity + (int64_t)0x8000 * transparency) / 0xffff;
2469         WRITE_YUV101010(y_i, u_i, v_i);
2470 }
2471
2472 static inline void transfer_YUVA16161616_to_YUV420P_YUV422P(unsigned char *output_y, 
2473         unsigned char *output_u, 
2474         unsigned char *output_v, 
2475         uint16_t *input,
2476         int output_column)
2477 {
2478         int64_t opacity = input[3];
2479         int64_t transparency = 0xffff - opacity;
2480
2481         output_y[output_column] =     ((int64_t)input[0] * opacity) / 0xffff00;
2482         output_u[output_column / 2] = ((int64_t)input[1] * opacity + 0x8000 * transparency) / 0xffff00;
2483         output_v[output_column / 2] = ((int64_t)input[2] * opacity + 0x8000 * transparency) / 0xffff00;
2484 }
2485
2486 static inline void transfer_YUVA16161616_to_YUV444P(unsigned char *output_y, 
2487         unsigned char *output_u, 
2488         unsigned char *output_v, 
2489         uint16_t *input,
2490         int output_column)
2491 {
2492         int64_t opacity = input[3];
2493         int64_t transparency = 0xffff - opacity;
2494
2495         output_y[output_column] = ((int64_t)input[0] * opacity) / 0xffff00;
2496         output_u[output_column] = ((int64_t)input[1] * opacity + 0x8000 * transparency) / 0xffff00;
2497         output_v[output_column] = ((int64_t)input[2] * opacity + 0x8000 * transparency) / 0xffff00;
2498 }
2499
2500 static inline void transfer_YUVA16161616_to_YUV422(unsigned char *(*output), 
2501         uint16_t *input,
2502         int j)
2503 {
2504         int64_t opacity = input[3];
2505         int64_t transparency = 0xffff - opacity;
2506
2507 // Store U and V for even pixels only
2508         if(!(j & 1))
2509         {
2510                 (*output)[0] = ((int64_t)input[0] * opacity) / 0xffff00;
2511                 (*output)[1] = ((int64_t)input[1] * opacity + 0x8000 * transparency) / 0xffff00;
2512                 (*output)[3] = ((int64_t)input[2] * opacity + 0x8000 * transparency) / 0xffff00;
2513         }
2514         else
2515 // Store Y and advance output for odd pixels only
2516         {
2517                 (*output)[2] = (input[0] * opacity) / 0xffff00;
2518                 (*output) += 4;
2519         }
2520 }
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559 #define ZTYP(ty) typedef ty z_##ty __attribute__ ((__unused__))
2560 ZTYP(int);
2561
2562
2563 // ******************************** Loops *************************************
2564
2565 #define TRANSFER_FRAME_HEAD \
2566         for(i = 0; i < out_h; i++) \
2567         { \
2568                 unsigned char *output_row = output_rows[i + out_y] + out_x * out_pixelsize; \
2569                 unsigned char *input_row = input_rows[row_table[i]]; \
2570                 z_int bit_counter = 7; \
2571                 for(j = 0; j < out_w; j++) \
2572                 {
2573
2574 #define TRANSFER_FRAME_TAIL \
2575                 } \
2576         }
2577
2578 #define TRANSFER_YUV420P_OUT_HEAD \
2579         for(i = 0; i < out_h; i++) \
2580         { \
2581                 unsigned char *input_row = input_rows[row_table[i]]; \
2582                 unsigned char *output_y = out_y_plane + i * total_out_w + out_x; \
2583                 unsigned char *output_u = out_u_plane + i / 2 * total_out_w / 2 + out_x / 2; \
2584                 unsigned char *output_v = out_v_plane + i / 2 * total_out_w / 2 + out_x / 2; \
2585                 for(j = 0; j < out_w; j++) \
2586                 {
2587
2588 #define TRANSFER_YUV422P_OUT_HEAD \
2589         for(i = 0; i < out_h; i++) \
2590         { \
2591                 unsigned char *input_row = input_rows[row_table[i]]; \
2592                 unsigned char *output_y = out_y_plane + i * total_out_w + out_x; \
2593                 unsigned char *output_u = out_u_plane + i * total_out_w / 2 + out_x / 2; \
2594                 unsigned char *output_v = out_v_plane + i * total_out_w / 2 + out_x / 2; \
2595                 for(j = 0; j < out_w; j++) \
2596                 {
2597
2598 #define TRANSFER_YUV444P_OUT_HEAD \
2599         for(i = 0; i < out_h; i++) \
2600         { \
2601                 unsigned char *input_row = input_rows[row_table[i]]; \
2602                 unsigned char *output_y = out_y_plane + i * total_out_w + out_x; \
2603                 unsigned char *output_u = out_u_plane + i * total_out_w + out_x; \
2604                 unsigned char *output_v = out_v_plane + i * total_out_w + out_x; \
2605                 for(j = 0; j < out_w; j++) \
2606                 {
2607
2608 #define TRANSFER_YUV420P_IN_HEAD \
2609         for(i = 0; i < out_h; i++) \
2610         { \
2611                 unsigned char *output_row = output_rows[i + out_y] + out_x * out_pixelsize; \
2612                 unsigned char *input_y = in_y_plane + row_table[i] * total_in_w; \
2613                 unsigned char *input_u = in_u_plane + (row_table[i] / 2) * (total_in_w / 2); \
2614                 unsigned char *input_v = in_v_plane + (row_table[i] / 2) * (total_in_w / 2); \
2615                 for(j = 0; j < out_w; j++) \
2616                 {
2617
2618 #define TRANSFER_YUV9P_IN_HEAD \
2619         for(i = 0; i < out_h; i++) \
2620         { \
2621                 unsigned char *output_row = output_rows[i + out_y] + out_x * out_pixelsize; \
2622                 unsigned char *input_y = in_y_plane + row_table[i] * total_in_w; \
2623                 unsigned char *input_u = in_u_plane + (row_table[i] / 4) * (total_in_w / 4); \
2624                 unsigned char *input_v = in_v_plane + (row_table[i] / 4) * (total_in_w / 4); \
2625                 for(j = 0; j < out_w; j++) \
2626                 {
2627
2628
2629 #define TRANSFER_YUV422P_IN_HEAD \
2630         for(i = 0; i < out_h; i++) \
2631         { \
2632                 unsigned char *output_row = output_rows[i + out_y] + out_x * out_pixelsize; \
2633                 unsigned char *input_y = in_y_plane + row_table[i] * total_in_w; \
2634                 unsigned char *input_u = in_u_plane + row_table[i] * (total_in_w / 2); \
2635                 unsigned char *input_v = in_v_plane + row_table[i] * (total_in_w / 2); \
2636                 for(j = 0; j < out_w; j++) \
2637                 {
2638
2639 #define TRANSFER_YUV444P_IN_HEAD \
2640         for(i = 0; i < out_h; i++) \
2641         { \
2642                 unsigned char *output_row = output_rows[i + out_y] + out_x * out_pixelsize; \
2643                 unsigned char *input_y = in_y_plane + row_table[i] * total_in_w; \
2644                 unsigned char *input_u = in_u_plane + row_table[i] * total_in_w; \
2645                 unsigned char *input_v = in_v_plane + row_table[i] * total_in_w; \
2646                 for(j = 0; j < out_w; j++) \
2647                 {
2648
2649
2650 #define TRANSFER_YUV422_IN_HEAD \
2651         for(i = 0; i < out_h; i++) \
2652         { \
2653                 unsigned char *output_row = output_rows[i + out_y] + ((out_x * out_pixelsize) & 0xfffffffc); \
2654                 unsigned char *input_y = in_y_plane + row_table[i] * total_in_w; \
2655                 unsigned char *input_u = in_u_plane + row_table[i] * (total_in_w / 2); \
2656                 unsigned char *input_v = in_v_plane + row_table[i] * (total_in_w / 2); \
2657                 for(j = 0; j < out_w; j++) \
2658                 {
2659
2660
2661
2662
2663
2664 // ******************************** Permutation *******************************
2665
2666
2667
2668 #define PERMUTATION_ARGS \
2669         unsigned char **output_rows,  \
2670         unsigned char **input_rows, \
2671         unsigned char *out_y_plane, \
2672         unsigned char *out_u_plane, \
2673         unsigned char *out_v_plane, \
2674         unsigned char *in_y_plane, \
2675         unsigned char *in_u_plane, \
2676         unsigned char *in_v_plane, \
2677         int in_x,  \
2678         int in_y,  \
2679         int in_w,  \
2680         int in_h, \
2681         int out_x,  \
2682         int out_y,  \
2683         int out_w,  \
2684         int out_h, \
2685         int in_colormodel,  \
2686         int out_colormodel, \
2687         int bg_color, \
2688         int total_in_w, \
2689         int total_out_w, \
2690         int scale, \
2691         int out_pixelsize, \
2692         int in_pixelsize, \
2693         int *row_table, \
2694         int *column_table, \
2695         int bg_r, \
2696         int bg_g, \
2697         int bg_b
2698
2699 void cmodel_float(PERMUTATION_ARGS);
2700 void cmodel_yuv420p(PERMUTATION_ARGS);
2701 void cmodel_yuv9p(PERMUTATION_ARGS);
2702 void cmodel_yuv444p(PERMUTATION_ARGS);
2703 void cmodel_yuv422(PERMUTATION_ARGS);
2704 void cmodel_default(PERMUTATION_ARGS);
2705