Debian Codegraph @

lib/common/image.cpp

raw
1 #include "image.h"
2 #include "avm_cpuinfo.h"
3 #include "mmx.h"
4 #include "avm_fourcc.h"
5 #include "avm_output.h"
6 #include "utils.h"
7 #include <unistd.h>
8 #include <fcntl.h>
9
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <math.h>
14
15 AVM_BEGIN_NAMESPACE;
16
17 #define VALGRIND
18
19 #define STDCONV_PARAMS \
20 CImage* to, const CImage* from, bool flip_dir
21
22 #define SHORTLINEARGS \
23 uint8_t* dest, const uint8_t* src, int w
24
25 #define LONGLINEARGS \
26 uint8_t* dest_y, uint8_t* dest_cr, uint8_t* dest_cb, \
27 int ds_y, int ds_cr, int ds_cb, \
28 const uint8_t* src_y, const uint8_t* src_cr, const uint8_t* src_cb, \
29 int ss_y, int ss_cr, int ss_cb, \
30 int w, int h
31
32 #define SETWHS() \
33 int w = (to->m_Window.w < from->m_Window.w) ? to->m_Window.w : from->m_Window.w; \
34 int h = (to->m_Window.h < from->m_Window.h) ? to->m_Window.h : from->m_Window.h; \
35 int ss_y = (flip_dir) ? -from->m_iStride[0] : from->m_iStride[0]; \
36 int ds_y = to->m_iStride[0]
37
38
39 #ifdef XXX_ARCH_X86
40 //MMX versions
41 #undef RENAME
42 #define HAVE_MMX
43 #undef HAVE_MMX2
44 #undef HAVE_3DNOW
45 #define RENAME(a) a ## _MMX
46 #include "image_mmx.h"
47
48 //MMX2 versions
49 #undef RENAME
50 #define HAVE_MMX
51 #define HAVE_MMX2
52 #undef HAVE_3DNOW
53 #define RENAME(a) a ## _MMX2
54 #include "image_mmx.h"
55 #endif
56
57 static inline void emms()
58 {
59 #if ARCH_X86
60 if (freq.HaveMMX())
61 __asm__ __volatile__ ("emms":::"memory");
62 #endif
63 }
64
65 static void stride_memcpy(void* to, int strideout, const void* from, int stridein, int bpl, int h)
66 {
67 //printf("Stride %p - %d ::: %p - %d %d %d\n", out, strideout, in, stridein, bpl, h);
68 if (bpl & 0x1f || !freq.HaveMMX())
69 {
70 //printf("wrong h: %d bpl: %d %d %d\n", h, bpl, strideout, stridein);
71 while (--h >= 0)
72 {
73 memcpy(to, from, bpl);
74 to = (char*) to + strideout;
75 from = (const char*) from + stridein;
76 }
77 }
78 #if ARCH_X86
79 // mmx2 doesn't write into l2 cache
80 // XXX: sometimes this doesn't have to be a good idea so be careful here
81 else if (freq.HaveMMXEXT())
82 {
83 //printf("stridecpy h: %d bpl: %d so:%d si:%d\n", h, bpl, strideout, stridein);
84 char* tout = (char*)to + bpl;
85 const char* tin = (const char*)from + bpl;
86 while (--h >= 0)
87 {
88 long tbpl = -bpl;
89 __asm__ __volatile__
90 (
91 ".balign 8 \n\t"
92 "1: \n\t"
93 "movq (%1, %0), %%mm0 \n\t"
94 "prefetchnta 32(%1, %0) \n\t"
95 "movq 8(%1, %0), %%mm1 \n\t"
96 "movq 16(%1, %0), %%mm2 \n\t"
97 "movq 24(%1, %0), %%mm3 \n\t"
98 "prefetchnta 64(%1, %0) \n\t"
99 "movntq %%mm0, (%2, %0) \n\t"
100 "movntq %%mm1, 8(%2, %0) \n\t"
101 "movntq %%mm2, 16(%2, %0) \n\t"
102 "movntq %%mm3, 24(%2, %0) \n\t"
103 "add $32, %0 \n\t"
104 "js 1b \n\t"
105 :"+r"(tbpl)
106 :"r"(tin), "r"(tout)
107 :"memory"
108 );
109 tout += strideout;
110 tin += stridein;
111 }
112 }
113 else
114 {
115 //printf("stridecpy h: %d bpl: %d so:%d si:%d\n", h, bpl, strideout, stridein);
116 char* tout = (char*)to + bpl;
117 const char* tin = (const char*)from + bpl;
118 while (--h >= 0)
119 {
120 long tbpl = -bpl;
121 __asm__ __volatile__
122 (
123 ".balign 8 \n\t"
124 "1: \n\t"
125 "movq (%1, %0), %%mm0 \n\t"
126 "movq %%mm0, (%2, %0) \n\t"
127 "movq 8(%1, %0), %%mm1 \n\t"
128 "movq 16(%1, %0), %%mm2 \n\t"
129 "movq 24(%1, %0), %%mm3 \n\t"
130 "movq %%mm1, 8(%2, %0) \n\t"
131 "movq %%mm2, 16(%2, %0) \n\t"
132 "movq %%mm3, 24(%2, %0) \n\t"
133 "add $32, %0 \n\t"
134 "js 1b \n\t"
135 :"+r"(tbpl)
136 :"r"(tin), "r"(tout)
137 :"memory"
138 );
139 tout += strideout;
140 tin += stridein;
141 }
142 }
143 #endif // ARCH_X86
144 }
145
146 static void bgr32_bgr32_c(SHORTLINEARGS)
147 {
148 stride_memcpy(dest, w * 4, src, w * 4, w * 4, 1);
149 }
150 static void bgr24_bgr24_c(SHORTLINEARGS)
151 {
152 stride_memcpy(dest, w * 3, src, w * 3, w * 3, 1);
153 }
154 static void bgr16_bgr16_c(SHORTLINEARGS)
155 {
156 stride_memcpy(dest, w * 2, src, w * 2, w * 2, 1);
157 }
158
159 static void bgr15_bgr16_c(SHORTLINEARGS)
160 {
161 const uint16_t* s = (const uint16_t*) src;
162 uint16_t* d = (uint16_t*) dest;
163 while (w > 1)
164 {
165 w -= 2;
166 uint32_t q = *((const uint32_t *)&s[w]);
167 *((uint32_t *)&d[w]) = (q & 0x7FFF7FFFU) + (q & 0x7FE07FE0U);
168 }
169 if (w)
170 {
171 uint16_t q = s[0];
172 d[0] = uint16_t(q + uint16_t(q & 0x7FE0U));
173 }
174 }
175 /*
176 * From BGR24 conversions
177 */
178 static void bgr24_bgr15_c(SHORTLINEARGS)
179 {
180 uint16_t* d = (uint16_t*) dest;
181 while (--w >= 0)
182 // looks ugly but gives good compiled code
183 d[w] = uint16_t(((((((src[w*3+2]<<5)&0xff00)|src[w*3+1])<<5)&0xfff00)|src[w*3+0])>>3);
184 }
185 static void bgr24_bgr16_c(SHORTLINEARGS)
186 {
187 //printf("bgr24_bgr16_c %d\n", w);
188 uint16_t* d = (uint16_t*)dest;
189 while (--w >= 0)
190 d[w] = uint16_t(((((((src[w*3+2]<<5)&0xff00)|src[w*3+1])<<6)&0xfff00)|src[w*3+0])>>3);
191 }
192
193 /*
194 test code - but speed is nearly the same as with version above
195
196 static void bgr24_bgr16_cl(SHORTLINEARGS, int h, int ds_y, int ss_y)
197 {
198 while (h--)
199 {
200 #if 0
201 uint16_t* d = (uint16_t*) dest;
202 uint16_t* e = (uint16_t*) dest + w;
203 const uint8_t* s = src;
204 do {
205 // looks ugly but gives very fast compiled code
206 *d++ = ((((((s[2]<<5)&0xff00)|s[1])<<6)&0xfff00)|s[0])>>3;
207 s += 3;
208 } while (d < e);
209 #else
210 uint16_t* d = (uint16_t*)dest;
211 for (int i = w - 1; i >= 0; i--)
212 d[i] = ((((((src[i*3+2]<<5)&0xff00)|src[i*3+1])<<6)&0xfff00)|src[i*3+0])>>3;
213 #endif
214 dest += ds_y;
215 src += ss_y;
216 }
217 }
218 */
219
220 static void bgr24_bgr32_c(SHORTLINEARGS)
221 {
222 uint32_t* d = (uint32_t*) dest;
223 while (w--)
224 {
225 #ifndef WORDS_BIGENDIAN
226 // FIXME - BIGENDIAN should be handled everywhere!!!
227 *d++ = src[0] | (src[1] << 8) | (src[2] << 16); // b g r
228 #else
229 *d++ = src[0] << 24 | (src[1] << 16) | (src[2] << 8); // b g r
230 //dest[0] = src[0]; dest[1] = src[1]; dest[2] = src[2]; dest[3] = 0; dest += 4;
231 #endif
232 src += 3;
233 }
234 }
235
236 /*
237 * To BGR24 conversion
238 */
239 static void bgr15_bgr24_c(SHORTLINEARGS)
240 {
241 while (w--)
242 {
243 uint16_t sh = *(const uint16_t*)src;
244 dest[0] = uint8_t((sh & 0x1f) << 3); // b
245 dest[1] = uint8_t((sh & 0x3e0) >> 2); // g
246 dest[2] = uint8_t((sh & 0x7c00) >> 7); // r
247 dest += 3;
248 src += 2;
249 }
250 }
251 static void bgr16_bgr24_c(SHORTLINEARGS)
252 {
253 while (w--)
254 {
255 uint16_t sh = *(const uint16_t*)src;
256 dest[0] = uint8_t((sh & 0x1f) << 3); // b
257 dest[1] = uint8_t((sh & 0x7e0) >> 3); // g
258 dest[2] = uint8_t((sh & 0xf800) >> 8); // r
259 dest += 3;
260 src += 2;
261 }
262 }
263 static void bgr32_bgr24_c(SHORTLINEARGS)
264 {
265 while (w--)
266 {
267 dest[0] = src[0];
268 dest[1] = src[1];
269 dest[2] = src[2];
270 src += 4;
271 dest += 3;
272 }
273 }
274
275 /*
276 * YUV conversions
277 */
278 static void bgr24_yuv_c(SHORTLINEARGS)
279 {
280 const col* s = (const col*) src;
281 yuv* d = (yuv*) dest;
282 while (w--)
283 *d++ = *s++;
284 }
285 static void yuv_bgr24_c(SHORTLINEARGS)
286 {
287 const yuv* s = (const yuv*) src;
288 col* d = (col*) dest;
289
290 while (w--)
291 *d++ = *s++;
292 }
293
294 static void bgr24_yuy2_c(SHORTLINEARGS)
295 {
296 const col* s = (const col*) src;
297 uint32_t* d = (uint32_t*) dest;
298 while (w--)
299 {
300 yuv yuvt(*s++);
301 dest[0] = yuvt.Y;
302 dest[1] = yuvt.Cb;
303 dest[2] = s->Y();
304 dest[3] = yuvt.Cr;
305
306 s++;
307 dest += 4;
308 }
309 }
310 static void yuy2_bgr24_c(SHORTLINEARGS)
311 {
312 col* d = (col*) dest;
313 while (w--)
314 {
315 yuv p;
316
317 p.Y = src[0];
318 p.Cb = src[1];
319 p.Cr = src[3];
320 d[0] = p;
321 p.Y = src[2];
322 d[1] = p;
323
324 src += 4;
325 d += 2;
326 }
327 }
328 static void uyvy_bgr24_c(SHORTLINEARGS)
329 {
330 //printf("uyvy_bgr24_c %d\n", w);
331 col* d = (col*) dest;
332 while (w--)
333 {
334 yuv p;
335
336 p.Cb = src[0];
337 p.Y = src[1];
338 p.Cr = src[2];
339 d[0] = p;
340 p.Y = src[3];
341 d[1] = p;
342
343 src += 4;
344 d += 2;
345 }
346 }
347
348 static void yv12_yuy2_c(LONGLINEARGS)
349 {
350 //printf("yv12_yuy2_c\n");
351 while (h--)
352 {
353 int i = w;
354 while (--i >= 0)
355 {
356 dest_y[i * 4] = src_y[i * 2];
357 dest_y[i * 4 + 2] = src_y[i * 2 + 1];
358 dest_y[i * 4 + ds_y] = src_y[i * 2 + ss_y];
359 dest_y[i * 4 + ds_y + 2] = src_y[i * 2 + ss_y + 1];
360
361 dest_y[i * 4 + 1] = dest_y[i * 4 + ds_y + 1] = src_cb[i];
362 dest_y[i * 4 + 3] = dest_y[i * 4 + ds_y + 3] = src_cr[i];
363 }
364
365 src_y += 2 * ss_y;
366 src_cr += ss_cr;
367 src_cb += ss_cb;
368
369 dest_y += 2 * ds_y;
370 }
371 }
372
373 static void yv12_bgr16_cSLOW(LONGLINEARGS)
374 {
375 uint8_t l1[5000];
376 uint8_t l2[5000];
377 //printf("YV12 tgb16\n");
378 while (h--)
379 {
380 //printf("H %d %d %d\n", i, fromstride, tostride);
381 col* dest = (col*) l1;
382 col* dest2 = (col*) l2;
383 int i;
384 for (i = 0; i < w; i++)
385 {
386 yuv p;
387 p.Cr = src_cr[i];
388 p.Cb = src_cb[i];
389 p.Y = src_y[i * 2];
390 dest[i * 2] = p;
391 p.Y = src_y[i * 2 + 1];
392 dest[i * 2 + 1] = p;
393 p.Y = src_y[i * 2 + ss_y];
394 dest2[i * 2] = p;
395 p.Y = src_y[i * 2 + ss_y + 1];
396 dest2[i * 2 + 1] = p;
397 }
398
399 bgr24_bgr16_c(dest_y, l1, w * 2);
400 bgr24_bgr16_c(dest_y + ds_y, l2, w * 2);
401
402 src_y += 2 * ss_y;
403 src_cr += ss_cr;
404 src_cb += ss_cb;
405
406 dest_y += 2 * ds_y;
407 }
408 }
409
410 static void yv12_bgr16_c(LONGLINEARGS)
411 {
412 //printf("YV12 tgb16\n");
413 while (h--)
414 {
415 //printf("H %d %d %d\n", i, fromstride, tostride);
416 uint16_t* dest = (uint16_t*) dest_y;
417 uint16_t* dest2 = (uint16_t*) (dest_y + ds_y);
418 int i;
419 for (i = 0; i < w; i++)
420 {
421 yuv p;
422 p.Cr = src_cr[i];
423 p.Cb = src_cb[i];
424 p.Y = src_y[i * 2];
425 dest[i * 2] = col(p).bgr16();
426 p.Y = src_y[i * 2 + 1];
427 dest[i * 2 + 1] = col(p).bgr16();
428 p.Y = src_y[i * 2 + ss_y];
429 dest2[i * 2] = col(p).bgr16();
430 p.Y = src_y[i * 2 + ss_y + 1];
431 dest2[i * 2 + 1] = col(p).bgr16();
432 }
433
434 src_y += 2 * ss_y;
435 src_cr += ss_cr;
436 src_cb += ss_cb;
437
438 dest_y += 2 * ds_y;
439 }
440 }
441
442 static void yv12_bgr24_c(LONGLINEARGS)
443 {
444 while (h--)
445 {
446 //printf("H %d %d %d\n", i, fromstride, tostride);
447 col* dest = (col*) dest_y;
448 col* dest2 = (col*) (dest_y + ds_y);
449 int i;
450 for (i = 0; i < w; i++)
451 {
452 yuv p;
453 p.Cr = src_cr[i];
454 p.Cb = src_cb[i];
455 p.Y = src_y[i * 2];
456 dest[i * 2] = p;
457 p.Y = src_y[i * 2 + 1];
458 dest[i * 2 + 1] = p;
459 p.Y = src_y[i * 2 + ss_y];
460 dest2[i * 2] = p;
461 p.Y = src_y[i * 2 + ss_y + 1];
462 dest2[i * 2 + 1] = p;
463 }
464
465 src_y += 2 * ss_y;
466 src_cr += ss_cr;
467 src_cb += ss_cb;
468
469 dest_y += 2 * ds_y;
470 }
471 }
472
473 static void y800_bgr24_c(LONGLINEARGS)
474 {
475 printf("HxW %dx%d ssy:%d dsy:%d\n", h, w, ss_y, ds_y);
476 while (h--)
477 {
478 col* dest = (col*) dest_y;
479 int i;
480 for (i = 0; i < w; i++)
481 {
482 yuv p;
483 p.Cr = 0x80;
484 p.Cb = 0x80;
485 p.Y = src_y[i];
486 dest[i] = p;
487 }
488
489 src_y += ss_y;
490 dest_y += ds_y;
491 }
492 }
493
494 static void bgr24_yv12_c(LONGLINEARGS)
495 {
496 //printf("bgr24ToYv12 %d %d\n", w, h);
497 while (h--)
498 {
499 const uint8_t* s = src_y;
500 uint8_t* dest_y1 = dest_y + ds_y;
501 for (int i = 0; i < w; i++)
502 {
503 yuv yuvt(*(const col*)s);
504 dest_cb[i] = yuvt.Cb;
505 dest_cr[i] = yuvt.Cr;
506 dest_y[i * 2] = yuvt.Y;
507 dest_y[i * 2 + 1] = ((const col*)(s + 3))->Y();
508 dest_y1[i * 2] = ((const col*)(s + ss_y))->Y();
509 dest_y1[i * 2 + 1] = ((const col*)(s + ss_y + 3))->Y();
510
511 s += 6;
512 }
513
514 src_y += 2 * ss_y;
515
516 dest_y += 2 * ds_y;
517 dest_cr += ds_cr;
518 dest_cb += ds_cb;
519 }
520 }
521
522 static void yuy2_yv12_c(LONGLINEARGS)
523 {
524 //printf("yuy2ToYv12\n");
525 while (h--)
526 {
527 uint8_t* d1 = dest_y;
528 uint8_t* d2 = dest_y + ds_y;
529 int i = w;
530 while (--i >= 0)
531 {
532 d1[i * 2] = src_y[i * 4];
533 dest_cb[i] = src_y[i * 4 + 1];
534 d2[i * 2] = src_y[i * 4 + ss_y];
535 d1[i * 2 + 1] = src_y[i * 4 + 2];
536 d2[i * 2 + 1] = src_y[i * 4 + 2 + ss_y];
537 dest_cr[i] = src_y[i * 4 + 3];
538 }
539 src_y += 2 * ss_y;
540
541 dest_y += 2 * ds_y;
542 dest_cr += ds_cr;
543 dest_cb += ds_cb;
544 }
545 }
546
547 static void uyvy_yv12_c(LONGLINEARGS)
548 {
549 //printf("uyvyToYv12\n");
550 while (h--)
551 {
552 uint8_t* d1 = dest_y;
553 uint8_t* d2 = dest_y + ds_y;
554 int i = w;
555 while (--i >= 0)
556 {
557 dest_cb[i] = src_y[i * 4];
558 d1[i * 2] = src_y[i * 4 + 1];
559 d2[i * 2] = src_y[i * 4 + ss_y + 1];
560 dest_cr[i] = src_y[i * 4 + 2];
561 d1[i * 2 + 2] = src_y[i * 4 + 3];
562 d2[i * 2 + 2] = src_y[i * 4 + 3 + ss_y];
563 }
564 src_y += 2 * ss_y;
565
566 dest_y += 2 * ds_y;
567 dest_cr += ds_cr;
568 dest_cb += ds_cb;
569 }
570 }
571
572 static void i420_yv12_c(LONGLINEARGS)
573 {
574 //printf("i420ToYv12\n");
575 stride_memcpy(dest_y, ds_y, src_y, ss_y, w, h);
576 // src_cr & src_cb are already swapped when this
577 // routine is being called - that's why it looks like
578 // pure copy
579 stride_memcpy(dest_cr, ds_cr, src_cr, ss_cr, w / 2, h / 2);
580 stride_memcpy(dest_cb, ds_cb, src_cb, ss_cb, w / 2, h / 2);
581 }
582
583 static void i411_yv12_c(LONGLINEARGS)
584 {
585 // mainly ffmpeg mjpeg
586 //printf("i411ToYv12\n");
587 stride_memcpy(dest_y, ds_y, src_y, ss_y, w, h);
588
589 h /= 2;
590 w /= 4;
591
592 while (h--)
593 {
594 int i = w;
595 while (--i >= 0)
596 {
597 dest_cb[i * 2] = src_cr[i];
598 dest_cr[i * 2] = src_cb[i];
599 // for next pixels use pixels from the next line
600 dest_cb[i * 2 + 1] = src_cr[i + ss_cr];
601 dest_cr[i * 2 + 1] = src_cb[i + ss_cb];
602 }
603
604 src_cr += 2 * ss_cr;
605 src_cb += 2 * ss_cb;
606
607 dest_cr += ds_cr;
608 dest_cb += ds_cb;
609 }
610 }
611
612 static void i422_yv12_c(LONGLINEARGS)
613 {
614 // mainly ffmpeg mjpeg
615 //printf("i422ToYv12 %d %p %d %d %d %d %d\n", ds_y, src_y, ss_y, w, h, ss_cb, ss_cr);
616 stride_memcpy(dest_y, ds_y, src_y, ss_y, w, h);
617 stride_memcpy(dest_cr, ds_cr, src_cb, 2 * ss_cb, w / 2, h / 2);
618 stride_memcpy(dest_cb, ds_cb, src_cr, 2 * ss_cr, w / 2, h / 2);
619 }
620
621 static void i444_yv12_c(LONGLINEARGS)
622 {
623 // another mjpeg output from ffmpeg
624 //printf("i444ToYv12\n");
625 stride_memcpy(dest_y, ds_y, src_y, ss_y, w, h);
626 h /= 2;
627 w /= 2;
628
629 while (h--)
630 {
631 int i = w;
632 while (--i >= 0)
633 {
634 dest_cb[i] = src_cr[i * 2];
635 dest_cr[i] = src_cb[i * 2];
636 }
637
638 src_cr += 2 * ss_cr;
639 src_cb += 2 * ss_cb;
640
641 dest_cr += ds_cr;
642 dest_cb += ds_cb;
643 }
644 }
645
646 static void i410_yv12_c(LONGLINEARGS)
647 {
648 // mainly ffmpeg indeo
649 //printf("i410ToYv12\n");
650 stride_memcpy(dest_y, ds_y, src_y, ss_y, w, h);
651
652 h /= 4;
653 w /= 4;
654
655 while (h--)
656 {
657 int i = w;
658 while (--i >= 0)
659 {
660 dest_cr[i * 2] = dest_cr[i * 2 + 1] = src_cb[i];
661 dest_cb[i * 2] = dest_cb[i * 2 + 1] = src_cr[i];
662 // for next pixels use pixels from the next line
663 dest_cr[i * 2 + ds_cr] = dest_cr[i * 2 + 1 + ds_cr] = src_cb[i];
664 dest_cb[i * 2 + ds_cb] = dest_cb[i * 2 + 1 + ds_cb] = src_cr[i];
665 //dest_cb[i * 2 + 1] = src_cr[i + ss_cr];
666 //dest_cr[i * 2 + 1] = src_cb[i + ss_cb];
667 }
668
669 src_cr += ss_cr;
670 src_cb += ss_cb;
671
672 dest_cr += 2 * ds_cr;
673 dest_cb += 2 * ds_cb;
674 }
675 }
676
677 static void y800_yv12_c(LONGLINEARGS)
678 {
679 printf("y800ToYv12 not yet implemented\n");
680 return;
681 stride_memcpy(dest_y, ds_y, src_y, ss_y, w, h);
682
683 h /= 4;
684 w /= 4;
685
686 while (h--)
687 {
688 memset(dest_cr, 0x80, w); dest_cr += ds_cr;
689 memset(dest_cb, 0x80, w); dest_cb += ds_cb;
690 }
691 }
692
693 enum {
694 // short
695 BGR32TOBGR32,
696 BGR24TOBGR24,
697 BGR16TOBGR16,
698
699 BGR15TOBGR16,
700
701 BGR24TOBGR15,
702 BGR15TOBGR24,
703
704 BGR24TOBGR16,
705 BGR16TOBGR24,
706
707 BGR24TOBGR32,
708 BGR32TOBGR24,
709
710 BGR24TOYUV,
711 YUVTOBGR24,
712
713 BGR24TOYUV2,
714 YUV2TOBGR24,
715
716 UYVYTOBGR24,
717 Y800TOBGR24,
718
719 MAXSHORT,
720
721 // long
722 BGR24TOYV12,
723 YV12TOBGR24,
724
725 //BGR24TOYV12,
726 YV12TOBGR16,
727
728 YV12TOYUY2,
729 YUY2TOYV12,
730
731 UYVYTOYV12,
732
733 I420TOYV12,
734 I411TOYV12,
735 I422TOYV12,
736 I444TOYV12,
737 I410TOYV12,
738 Y800TOYV12,
739
740 MAXLONG
741 };
742
743 #define V(a) (void*)a
744 // mapping table - order hs to match above enum
745 static const struct lconvmap_s {
746 int div_w;
747 const void* s_c;
748 const void* s_mmx;
749 const void* s_mmx2;
750 } lconvmap[] = {
751
752 // short options (only width is divided)
753 { 1, V(bgr32_bgr32_c), V(bgr32_bgr32_c), V(bgr32_bgr32_c) },
754 { 1, V(bgr24_bgr24_c), V(bgr24_bgr24_c), V(bgr24_bgr24_c) },
755 { 1, V(bgr16_bgr16_c), V(bgr16_bgr16_c), V(bgr16_bgr16_c) },
756
757 { 1, V(bgr15_bgr16_c), V(bgr15_bgr16_c), V(bgr15_bgr16_c) },
758
759 { 1, V(bgr24_bgr15_c), V(bgr24_bgr15_c), V(bgr24_bgr15_c) },
760 { 1, V(bgr15_bgr24_c), V(bgr15_bgr24_c), V(bgr15_bgr24_c) },
761
762 { 1, V(bgr24_bgr16_c), V(bgr24_bgr16_c), V(bgr24_bgr16_c) },
763 { 1, V(bgr16_bgr24_c), V(bgr16_bgr24_c), V(bgr16_bgr24_c) },
764
765 { 1, V(bgr24_bgr32_c), V(bgr24_bgr32_c), V(bgr24_bgr32_c) },
766 { 1, V(bgr32_bgr24_c), V(bgr32_bgr24_c), V(bgr32_bgr24_c) },
767
768 { 1, V(bgr24_yuv_c), V(bgr24_yuv_c), V(bgr24_yuv_c) },
769 { 1, V(yuv_bgr24_c), V(yuv_bgr24_c), V(yuv_bgr24_c) },
770
771 { 2, V(bgr24_yuy2_c), V(bgr24_yuy2_c), V(bgr24_yuy2_c) },
772 { 2, V(yuy2_bgr24_c), V(yuy2_bgr24_c), V(yuy2_bgr24_c) },
773
774 { 2, V(uyvy_bgr24_c), V(uyvy_bgr24_c), V(uyvy_bgr24_c) },
775 { 1, V(y800_bgr24_c), V(y800_bgr24_c), V(y800_bgr24_c) },
776
777 { 0 },
778
779 // long options
780 { 2, V(bgr24_yv12_c), V(bgr24_yv12_c), V(bgr24_yv12_c) },
781 { 2, V(yv12_bgr24_c), V(yv12_bgr24_c), V(yv12_bgr24_c) },
782
783 { 2, V(yv12_bgr16_c), V(yv12_bgr16_c), V(yv12_bgr16_c) },
784
785 { 2, V(yv12_yuy2_c), V(yv12_yuy2_c), V(yv12_yuy2_c) },
786 { 2, V(yuy2_yv12_c), V(yuy2_yv12_c), V(yuy2_yv12_c) },
787
788 { 2, V(uyvy_yv12_c), V(uyvy_yv12_c), V(uyvy_yv12_c) },
789
790 { 1, V(i420_yv12_c), V(i420_yv12_c), V(i422_yv12_c) },
791 { 1, V(i411_yv12_c), V(i411_yv12_c), V(i411_yv12_c) },
792 { 1, V(i422_yv12_c), V(i422_yv12_c), V(i422_yv12_c) },
793 { 1, V(i444_yv12_c), V(i444_yv12_c), V(i444_yv12_c) },
794 { 1, V(i410_yv12_c), V(i410_yv12_c), V(i410_yv12_c) },
795 { 1, V(y800_yv12_c), V(y800_yv12_c), V(y800_yv12_c) },
796 };
797 #undef V
798
799 /*
800 * generic conversion for 1plane routines
801 */
802 static void lineconvert(STDCONV_PARAMS, unsigned type)
803 {
804 typedef void (*LCONV)(SHORTLINEARGS);
805 LCONV lconv;
806 //flip_dir = true;
807
808 const uint8_t* src = from->GetWindow(0);
809 uint8_t* dest = to->GetWindow(0);
810 SETWHS();
811
812 if (flip_dir)
813 src += (from->m_Window.h - 1) * from->Stride();
814 //printf("W %d %d %d w: %d flip: %d\n", from->Width(), from->Stride(), from->m_Window.h, w, flip_dir);
815 w /= lconvmap[type].div_w;
816
817 assert(type < MAXSHORT);
818 lconv = (LCONV) lconvmap[type].s_c;
819 #if 1
820 if (w == to->Width() && 1
821 && w == from->Width()
822 && to->Bpl() == ds_y
823 && from->Bpl() == ss_y)
824 {
825 // one stright call is enough in this case
826 lconv(dest, src, w * h);
827 }
828 else
829 while (h--)
830 {
831 //printf("DEST %p %p %d ds: %d ss: %d %d\n", dest, src, to->Stride(), ds_y, ss_y, h);
832 lconv(dest, src, w);
833 dest += ds_y;
834 src += ss_y;
835 }
836 #else
837 bgr24_bgr16_cl(dest, src, w, h, ds_y, ss_y);
838 #endif
839 emms();
840 }
841
842 static void yuvconv(STDCONV_PARAMS, unsigned type)
843 {
844 typedef void (*LCONV)(LONGLINEARGS);
845 LCONV lconv;
846 //flip_dir = true;
847
848 const uint8_t* src_y = from->GetWindow(0);
849 const uint8_t* src_cr = from->GetWindow(1);
850 const uint8_t* src_cb = from->GetWindow(2);
851
852 SETWHS();
853 w = (w + 1) & ~1;
854 int ss_cr;
855 int ss_cb;
856 //printf("YUVCON %d %d %d %d %d\n", w, h, from->m_Window.x, from->m_Window.y, to->m_Window.x);
857
858 if (flip_dir)
859 {
860 src_y += (from->m_Window.h - 1) * from->m_iStride[0];
861 src_cr += (from->m_Window.h / lconvmap[type].div_w - 1) * from->m_iStride[1];
862 src_cb += (from->m_Window.h / lconvmap[type].div_w - 1) * from->m_iStride[2];
863 ss_cr = -from->m_iStride[1];
864 ss_cb = -from->m_iStride[2];
865 }
866 else
867 {
868 ss_cr = from->m_iStride[1];
869 ss_cb = from->m_iStride[2];
870 }
871
872 if (from->Format() == IMG_FMT_I420)
873 {
874 // swap U V
875 const uint8_t* t = src_cb;
876 int s = ss_cb;
877 src_cb = src_cr;
878 src_cr = t;
879 ss_cb = ss_cr;
880 ss_cr = s;
881 }
882
883 int swds = (to->Format() == IMG_FMT_I420) ? 1 : 0;
884
885 lconv = (LCONV) lconvmap[type].s_c;
886 w /= lconvmap[type].div_w;
887 h /= lconvmap[type].div_w;
888 lconv(to->GetWindow(0), to->GetWindow(1 + swds), to->GetWindow(2 - swds),
889 to->m_iStride[0], to->m_iStride[1 + swds], to->m_iStride[2 - swds],
890 src_y, src_cr, src_cb, ss_y, ss_cr, ss_cb, w, h);
891 emms();
892 }
893
894
895 static void anyFromBGR24(STDCONV_PARAMS)
896 {
897 switch (to->Format())
898 {
899 case 15:
900 lineconvert(to, from, flip_dir, BGR24TOBGR15);
901 return;
902 case 16:
903 lineconvert(to, from, flip_dir, BGR24TOBGR16);
904 return;
905 case 24:
906 lineconvert(to, from, flip_dir, BGR24TOBGR24);
907 return;
908 case 32:
909 lineconvert(to, from, flip_dir, BGR24TOBGR32);
910 return;
911 case IMG_FMT_YUV:
912 lineconvert(to, from, flip_dir, BGR24TOYUV);
913 return;
914 case IMG_FMT_YUY2:
915 lineconvert(to, from, flip_dir, BGR24TOYUV2);
916 return;
917 case IMG_FMT_YV12:
918 yuvconv(to, from, flip_dir, BGR24TOYV12);
919 return;
920 }
921
922 int f = to->Format();
923 AVM_WRITE("CImage", "Cannot convert from 24 bit image to unimplemented %.4s 0x%x\n",
924 (const char*)&f, f);
925 }
926
927 static void anyToBGR24(STDCONV_PARAMS)
928 {
929 switch(from->Format())
930 {
931 case 15:
932 lineconvert(to, from, flip_dir, BGR15TOBGR24);
933 return;
934 case 16:
935 lineconvert(to, from, flip_dir, BGR16TOBGR24);
936 return;
937 case 24:
938 lineconvert(to, from, flip_dir, BGR24TOBGR24);
939 return;
940 case 32:
941 lineconvert(to, from, flip_dir, BGR32TOBGR24);
942 return;
943 case IMG_FMT_YUV:
944 lineconvert(to, from, flip_dir, YUVTOBGR24);
945 return;
946 case IMG_FMT_YUY2:
947 lineconvert(to, from, flip_dir, YUV2TOBGR24);
948 return;
949 case IMG_FMT_Y422:
950 case IMG_FMT_UYVY:
951 lineconvert(to, from, flip_dir, UYVYTOBGR24);
952 return;
953 case IMG_FMT_YV12:
954 case IMG_FMT_I420:
955 // handles both yv12 & i420
956 yuvconv(to, from, flip_dir, YV12TOBGR24);
957 return;;
958 case IMG_FMT_Y800:
959 // handles both yv12 & i420
960 yuvconv(to, from, flip_dir, Y800TOBGR24);
961 return;;
962 }
963
964 int f = from->Format();
965 AVM_WRITE("CImage", "Cannot convert to 24 bit image from unimplemented %.4s 0x%x\n",
966 (const char*)&f, f);
967 }
968
969 lookuptable col::t = lookuptable();
970
971 lookuptable::lookuptable()
972 {
973 for(int i = 255; i >= 0; i--)
974 {
975 m_plY[i] = 298 * (i - 16);
976 m_plRV[i] = 408 * (i - 128);
977 m_plGV[i] = -208 * (i - 128);
978 m_plGU[i] = -100 * (i - 128);
979 m_plBU[i] = 517 * (i - 128);
980 }
981 }
982
983
984 /**
985 * Creates new image in format 'header' from specified memory area.
986 * Either allocates its own memory area & copies src data into it, or reuses
987 * parent data.
988 */
989 CImage::CImage(const BitmapInfo* header, const uint8_t* data, bool copy)
990 :m_pInfo(header)
991 {
992 fillMembers();
993 if (!copy)
994 {
995 m_pPlane[0] = const_cast<uint8_t*>(data);
996 }
997 else
998 {
999 int s = m_iBytes[0] + m_iBytes[1] + m_iBytes[2] + m_iBytes[3];
1000 m_pPlane[0] = new uint8_t[s];
1001 m_bDataOwner[0] = true;
1002 if (data)
1003 memcpy(m_pPlane[0], data, s);
1004 }
1005 setPlanes();
1006 //printf("COMPRESSTRIDE %.4s\n", (char*)&m_iFormat);
1007 }
1008
1009 CImage::CImage(const BitmapInfo* header, const uint8_t* plane[], const int stride[], bool copy)
1010 :m_pInfo(header)
1011 {
1012 //printf("CONST2\n");
1013 fillMembers();
1014
1015 if (!copy)
1016 {
1017 memcpy(m_pPlane, plane, sizeof(uint8_t*) * 3);
1018 memcpy(m_iStride, stride, sizeof(int) * 3);
1019 //printf("CIMAGE %p %p %p\n", m_pPlane[0], m_pPlane[1], m_pPlane[2]);
1020 //printf("CIMAGE %d %d %d\n", m_iStride[0], m_iStride[1], m_iStride[2]);
1021 }
1022 else
1023 {
1024 m_pPlane[0] = new uint8_t[m_iBytes[0] + m_iBytes[1] + m_iBytes[2]];
1025 m_bDataOwner[0] = true;
1026 for (int i = 0; i < 3; i++)
1027 {
1028 if (m_iBytes[i] && plane[i])
1029 stride_memcpy(m_pPlane[i], m_iStride[i],
1030 plane[i], stride[i], m_iWidth, m_iHeight);
1031 }
1032 emms();
1033 }
1034 setPlanes();
1035 }
1036
1037 /* Creates 24-bit RGB image from 24-bit RGB 'data' */
1038 CImage::CImage(const uint8_t* data, int width, int height)
1039 :m_pInfo(BitmapInfo(width, height, 24))
1040 {
1041 fillMembers();
1042 m_pPlane[0] = new uint8_t[m_iBytes[0]];
1043 m_bDataOwner[0] = true;
1044 if (data)
1045 memcpy(m_pPlane[0], data, m_iBytes[0]);
1046 }
1047
1048 // non-conversion constructors
1049 CImage::CImage(const CImage* im)
1050 :m_pInfo(im->GetFmt())
1051 {
1052 fillMembers();
1053 m_fQuality = im->GetQuality();
1054 int s = m_iBytes[0] + m_iBytes[1] + m_iBytes[2] + m_iBytes[3];
1055
1056 m_pPlane[0] = new uint8_t[s];
1057 m_iStride[0] = im->m_iStride[0];
1058 m_iBytes[0] = im->m_iBytes[0];
1059 m_bDataOwner[0] = true;
1060 memcpy(m_pPlane[0], im->m_pPlane[0], m_iBytes[0]);
1061
1062 for (unsigned i = 1; i < CIMAGE_MAX_PLANES; i++)
1063 {
1064 m_iStride[i] = im->m_iStride[i];
1065 m_iBytes[i] = im->m_iBytes[i];
1066 m_pPlane[i] = m_pPlane[i - 1] + m_iBytes[i - 1];
1067 memcpy(m_pPlane[i], im->m_pPlane[i], m_iBytes[i]);
1068 //printf("COPY STRIDE %i %p %d %d\n", i, m_pPlane[i], m_iStride[i], m_iBytes[i]);
1069 //printf("ALLOCPLANE %p(%d): %p\n", this, i, m_pPlane[i]);
1070 }
1071 }
1072
1073 // Conversion constructors
1074 CImage::CImage(const CImage* im, uint_t csp_bits)
1075 :m_pInfo(im->GetFmt())
1076 {
1077 if (csp_bits <= 32)
1078 m_pInfo.SetBits(csp_bits);
1079 else
1080 m_pInfo.SetSpace(csp_bits);
1081 fillMembers();
1082 m_fQuality = im->GetQuality();
1083 int s = m_iBytes[0] + m_iBytes[1] + m_iBytes[2] + m_iBytes[3];
1084 m_pPlane[0] = new uint8_t[s];
1085 m_bDataOwner[0] = true;
1086 setPlanes();
1087 Convert(im);
1088 }
1089
1090 CImage::CImage(const CImage* im, const BitmapInfo* header)
1091 :m_pInfo(header)
1092 {
1093 fillMembers();
1094 m_fQuality = im->GetQuality();
1095 int s = m_iBytes[0] + m_iBytes[1] + m_iBytes[2] + m_iBytes[3];
1096 m_pPlane[0] = new uint8_t[s];
1097 m_bDataOwner[0] = true;
1098 setPlanes();
1099 Convert(im);
1100 }
1101
1102 CImage::~CImage()
1103 {
1104 //printf("CIMDELETE %p cnt: %d own:%d (%p %p %p\n", this, m_iRefcount, m_bDataOwner, m_pPlane[0],m_pPlane[1],m_pPlane[2]);
1105 if (m_iRefcount > 1)
1106 {
1107 AVM_WRITE("CImage", "Unexpected delete of referenced image ! (%d) (USE RELEASE)\n", m_iRefcount);
1108 }
1109 for (uint_t i = 0; i < CIMAGE_MAX_PLANES; i++)
1110 if (m_bDataOwner[i])
1111 delete[] m_pPlane[i];
1112 }
1113
1114 void CImage::fillMembers()
1115 {
1116 m_iRefcount = 1;
1117 m_pUserData = 0;
1118 m_pAllocator = 0;
1119 m_fQuality = 0.0f;
1120 m_iType = 0;
1121 m_fAspectRatio = 1.0;
1122 for (unsigned i = 0; i < CIMAGE_MAX_PLANES; i++)
1123 {
1124 m_pPlane[i] = 0;
1125 m_iStride[i] = 0;
1126 m_iBytes[i] = 0;
1127 m_bDataOwner[i] = false;
1128 }
1129 // setup most common values
1130 // for some colorspaces these HAVE TO BE FIXED
1131 m_iDepth = m_pInfo.biBitCount;
1132 m_iFormat = m_pInfo.biCompression;
1133 if (m_pInfo.IsRGB())
1134 m_iFormat = m_pInfo.Bpp();
1135
1136 m_iBpp = (m_iDepth + 7) / 8;
1137 m_iBpl = m_iBpp * m_pInfo.biWidth;
1138 m_Window.x = m_Window.y = 0;
1139 m_iWidth = m_Window.w = m_pInfo.biWidth;
1140 m_iHeight = m_Window.h = (int) labs(m_pInfo.biHeight);
1141 m_iPixels = m_iWidth * m_iHeight;
1142 m_iBytes[0] = m_iBpl * m_iHeight;
1143 m_iStride[0] = m_iBpl;
1144
1145 switch (m_iFormat)
1146 {
1147 case IMG_FMT_YV12:
1148 case IMG_FMT_I420:
1149 m_iBpp = 1;
1150 m_iStride[0] = m_iBpl = m_iWidth;
1151 m_iStride[1] = m_iStride[2] = m_iWidth / 2;
1152 m_iBytes[0] = m_iStride[0] * m_iHeight;
1153 m_iBytes[1] = m_iStride[1] * m_iHeight / 2;
1154 m_iBytes[2] = m_iStride[2] * m_iHeight / 2;
1155 break;
1156 case IMG_FMT_I422:
1157 m_iBpp = 1;
1158 m_iStride[0] = m_iBpl = m_iWidth;
1159 m_iStride[1] = m_iStride[2] = m_iWidth / 2;
1160 m_iBytes[0] = m_iStride[0] * m_iHeight;
1161 m_iBytes[1] = m_iStride[1] * m_iHeight;
1162 m_iBytes[2] = m_iStride[2] * m_iHeight;
1163 break;
1164 case IMG_FMT_I444:
1165 m_iBpp = 1;
1166 m_iStride[0] = m_iStride[1] = m_iStride[2] = m_iBpl = m_iWidth;
1167 m_iBytes[0] = m_iStride[0] * m_iHeight;
1168 m_iBytes[1] = m_iStride[1] * m_iHeight;
1169 m_iBytes[2] = m_iStride[2] * m_iHeight;
1170 break;
1171 case IMG_FMT_YUY2:
1172 case IMG_FMT_UYVY:
1173 m_iBpp = 2;
1174 m_iBpl = m_iBpp * m_pInfo.biWidth;
1175 break;
1176 }
1177 }
1178
1179 void CImage::setPlanes()
1180 {
1181 switch (m_iFormat)
1182 {
1183 case IMG_FMT_YV12:
1184 case IMG_FMT_I420:
1185 case IMG_FMT_I422:
1186 case IMG_FMT_I444:
1187 if (!m_pPlane[1])
1188 m_pPlane[1] = m_pPlane[0] + m_iBytes[0];
1189 if (!m_pPlane[2])
1190 m_pPlane[2] = m_pPlane[0] + m_iBytes[0] + m_iBytes[1];
1191 break;
1192 }
1193 }
1194
1195 void CImage::Release() const
1196 {
1197 //printf("CIMRELEASE %p cnt: %d own:%d p0:%p\n", this, m_iRefcount, m_bDataOwner, m_pPlane[0]);
1198 m_iRefcount--;
1199 if (!m_iRefcount)
1200 delete this;
1201 }
1202
1203 void CImage::ToYUV()
1204 {
1205 if (m_pInfo.biCompression != 0)
1206 return;
1207
1208 yuv* src;
1209 src = (yuv*)Data() + m_iPixels - 1;
1210 if (m_iDepth != 24)
1211 {
1212 AVM_WRITE("CImage", "Cannot convert non-24 bit image to YUV\n");
1213 return;
1214 }
1215 while ((uint8_t*)src > Data() + 3)
1216 {
1217 *src = *(col*) src;
1218 src--;
1219 *src = *(col*) src;
1220 src--;
1221 *src = *(col*) src;
1222 src--;
1223 *src = *(col*) src;
1224 src--;
1225 }
1226 m_iFormat = m_pInfo.biCompression = IMG_FMT_YUV;
1227 }
1228
1229 void CImage::ToRGB()
1230 {
1231 if (m_pInfo.biCompression != IMG_FMT_YUV || m_iDepth != 24)
1232 {
1233 AVM_WRITE("CImage", "Cannot convert non-YUV image to BGR24\n");
1234 return;
1235 }
1236 col* src= (col*)Data() + m_iPixels - 1;
1237 while ((uint8_t*)src > Data() + 3)
1238 {
1239 *src= *(struct yuv*) src;
1240 src--;
1241 *src= *(struct yuv*) src;
1242 src--;
1243 *src= *(struct yuv*) src;
1244 src--;
1245 *src= *(struct yuv*) src;
1246 src--;
1247 }
1248 //m_pInfo.SetBits(24);
1249 m_iFormat = m_pInfo.biCompression = 0;
1250 }
1251
1252 void CImage::Blur(int range, int from)
1253 {
1254 if(range<=0)return;
1255 if(from<0)return;
1256 if(range<=from)return;
1257
1258 #if ARCH_X86_32
1259 int limit = m_iPixels * 3;
1260 unsigned lim=1<<(range+1);
1261 unsigned ach=3*(1<<(from+1));
1262 uint8_t* p = Data();
1263 while (ach < 3 * lim)
1264 {
1265 //printf("Blur %d\n", ach);
1266 asm volatile
1267 (
1268 //p %0
1269 //ach %1
1270 //limit %2
1271 "pushl %%eax \n\t"
1272 "pushl %%ebx \n\t"
1273 "pushl %%ecx \n\t"
1274 "pushl %%edx \n\t"
1275 "pushl %%edi \n\t"
1276
1277 "movl %0, %%ebx \n\t"
1278
1279 "movl %1, %%ecx \n\t"
1280
1281 "movl %0, %%edx \n\t"
1282 "addl %2, %%edx \n\t"
1283 "subl %1, %%edx \n\t"
1284
1285 "movl %%edx, %%edi \n\t"
1286
1287 "pxor %%mm2, %%mm2 \n\t"
1288
1289 "xorl %%eax, %%eax \n\t"
1290 "xorl %%edx, %%edx \n\t"
1291 // align 8
1292 "1: \n\t"
1293 "movd (%%ebx), %%mm0\n\t"
1294 // "movd %%ebx(%%ecx), %%mm1 \n\t"
1295 "movd (%%ecx,%%ebx), %%mm1 \n\t"
1296 "punpcklbw %%mm2, %%mm1\n\t"
1297 "punpcklbw %%mm2, %%mm0\n\t"
1298 "paddusw %%mm1, %%mm0 \n\t"
1299 "psrlw $1, %%mm0\n\t"
1300 "packuswb %%mm2, %%mm0\n\t"
1301 "movd %%mm0, (%%ebx)\n\t"
1302
1303 "addl $4, %%ebx \n\t"
1304 "cmpl %%edi, %%ebx \n\t"
1305 "jb 1b \n\t"
1306
1307 "popl %%edi \n\t"
1308 "popl %%edx \n\t"
1309 "popl %%ecx \n\t"
1310 "popl %%ebx \n\t"
1311 "popl %%eax \n\t"
1312 :
1313 :"r"(p), "r"(ach), "r"(limit)
1314 );
1315 ach*=m_iWidth;
1316 asm volatile
1317 (
1318 //p %0
1319 //ach %1
1320 //limit %2
1321 "pushl %%eax \n\t"
1322 "pushl %%ebx \n\t"
1323 "pushl %%ecx \n\t"
1324 "pushl %%edx \n\t"
1325 "pushl %%edi \n\t"
1326 "movl %0, %%ebx \n\t"
1327 "movl %0, %%ebx \n\t"
1328 "movl %1, %%ecx \n\t"
1329 "movl %0, %%edx \n\t"
1330 "addl %2, %%edx \n\t"
1331 "subl %1, %%edx \n\t"
1332
1333 "movl %%edx, %%edi \n\t"
1334
1335 "pxor %%mm2, %%mm2 \n\t"
1336
1337 "xorl %%eax, %%eax \n\t"
1338 "xorl %%edx, %%edx \n\t"
1339 // align 8
1340 "1: \n\t"
1341 "movd (%%ebx), %%mm0\n\t"
1342 "movd (%%ebx,%%ecx), %%mm1 \n\t"
1343 "punpcklbw %%mm2, %%mm1\n\t"
1344 "punpcklbw %%mm2, %%mm0\n\t"
1345 "paddusw %%mm1, %%mm0\n\t"
1346 "psrlw $1, %%mm0\n\t"
1347 "packuswb %%mm2, %%mm0\n\t"
1348 "movd %%mm0, (%%ebx)\n\t"
1349
1350 "addl $4, %%ebx \n\t"
1351 "cmpl %%edi, %%ebx \n\t"
1352 "jb 1b \n\t"
1353 "popl %%edi \n\t"
1354 "popl %%edx \n\t"
1355 "popl %%ecx \n\t"
1356 "popl %%ebx \n\t"
1357 "popl %%eax \n\t"
1358 :
1359 :"r"(p), "r"(ach), "r"(limit)
1360 );
1361 ach/=m_iWidth;
1362 ach*=2;
1363 }
1364 emms();
1365 #else
1366 #warning BLUR not implemented in C code
1367 #endif
1368 }
1369
1370 bool CImage::Supported(int csp, int bitcount)
1371 {
1372 //AVM_WRITE("CImage", "SUPPORTED 0x%x (%.4s) %d\n", csp, (char*)&csp, bitcount);
1373 if (csp == 0)
1374 {
1375 switch(bitcount)
1376 {
1377 case 15:
1378 case 16:
1379 case 24:
1380 case 32:
1381 return true;
1382 }
1383 }
1384 else if (csp == 3)
1385 {
1386 switch(bitcount)
1387 {
1388 case 15:
1389 case 16:
1390 return true;
1391 }
1392 }
1393 else
1394 {
1395 switch (StandardFOURCC(csp))
1396 {
1397 case IMG_FMT_YUV:
1398 case IMG_FMT_YUY2:
1399 case IMG_FMT_UYVY:
1400 case IMG_FMT_YV12:
1401 case IMG_FMT_I420:
1402 case IMG_FMT_Y422:
1403 case IMG_FMT_I422:
1404 case IMG_FMT_I444:
1405 //case IMG_FMT_YVYU:
1406 //case IMG_FMT_IYUV:
1407 return true;
1408 }
1409 }
1410 return false; // unsupported
1411 }
1412
1413 bool CImage::Supported(const BITMAPINFOHEADER& head)
1414 {
1415 return Supported(head.biCompression, head.biBitCount);
1416 }
1417
1418 fourcc_t CImage::StandardFOURCC(fourcc_t csp)
1419 {
1420 switch (csp)
1421 {
1422 case IMG_FMT_V422:
1423 case IMG_FMT_YUNV:
1424 return IMG_FMT_YUY2;
1425 case IMG_FMT_IYUV:
1426 return IMG_FMT_I420;
1427 case IMG_FMT_IYU2:
1428 case IMG_FMT_Y422:
1429 return IMG_FMT_UYVY;
1430 case IMG_FMT_Y8:
1431 return IMG_FMT_Y800;
1432 }
1433 return csp;
1434 }
1435
1436 void CImage::ByteSwap()
1437 {
1438 if (m_pInfo.biCompression != 0 || m_pInfo.biBitCount != 24)
1439 return;
1440
1441 uint8_t* t = Data();
1442 uint8_t* e = Data() + m_iPixels * 3 - 11;
1443 while (t < e)
1444 {
1445 uint8_t tmp = t[0];
1446 t[0] = t[2];
1447 t[2] = tmp;
1448
1449 uint8_t tmp1 = t[3];
1450 t[3] = t[5];
1451 t[5] = tmp1;
1452
1453 uint8_t tmp2 = t[6];
1454 t[6] = t[8];
1455 t[8] = tmp2;
1456
1457 uint8_t tmp3 = t[9];
1458 t[9] = t[11];
1459 t[11] = tmp3;
1460
1461 t += 12;
1462 }
1463 }
1464
1465 void CImage::Dump(const char* filename)
1466 {
1467 int fd;
1468 #ifndef WIN32
1469 fd=open(filename, O_WRONLY|O_CREAT|O_TRUNC, 00666);
1470 #else
1471 fd=open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 00666);
1472 #endif
1473 if(fd<0)
1474 {
1475 AVM_WRITE("CImage", "Warning: could not open %s for writing", filename);
1476 return;
1477 }
1478
1479 // int w=im->width();
1480 // int h=im->height();
1481 BitmapInfo bi(m_iWidth, m_iHeight, 24);
1482 CImage* im=0;
1483 if (!IsFmt(&bi))
1484 im = new CImage(this, &bi);
1485 const uint8_t* ptr = (im) ? im->Data() : Data();
1486 int bs = m_iPixels * 3;
1487 uint16_t bfh[7];
1488 bfh[0] = 'B' + 256 * 'M';
1489 int* ibfh = (int*) (bfh + 1);
1490 ibfh[0]=bs+0x36;
1491 ibfh[1]=0;
1492 ibfh[2]=0x36;
1493 write(fd, bfh, 14);
1494 write(fd, &bi, 40);
1495 write(fd, ptr, bs);
1496 close(fd);
1497 if (im)
1498 im->Release();
1499 }
1500
1501 void CImage::Clear()
1502 {
1503 //AVM_WRITE("CImage", "CLEAR IMAGE 0x%x %d,%d,%d\n", Format(), m_iBytes[0], m_iBytes[1], m_iBytes[2]);
1504 switch (StandardFOURCC(Format()))
1505 {
1506 case IMG_FMT_YV12:
1507 case IMG_FMT_I420:
1508 case IMG_FMT_I422:
1509 case IMG_FMT_I444:
1510 #if 1
1511 memset(m_pPlane[0], 0x10, m_iBytes[0]);
1512 memset(m_pPlane[1], 0x80, m_iBytes[1]);
1513 memset(m_pPlane[2], 0x80, m_iBytes[2]);
1514 #else
1515 {
1516 const uint8_t b[3] = { 0x10, 0x80, 0x80 };
1517 for (int j = 0; j < 3; ++j) {
1518 uint8_t c = b[j];
1519 fprintf(stderr, "CLEAN b:%d p:%p c:%x\n", m_iBytes[j], m_pPlane[j], c);
1520 for (unsigned i = 0; i < m_iBytes[j]; i++) m_pPlane[j][i] = c;
1521 }
1522 }
1523 #endif
1524 break;
1525 case IMG_FMT_YUY2:
1526 for (unsigned i = 0; i < m_iBytes[0] / 4; i++)
1527 avm_set_le32(((uint32_t*)m_pPlane[0]) + i, 0x80108010U);
1528 break;
1529 default: // RGB formats
1530 memset(m_pPlane[0], 0, m_iBytes[0]);
1531 break;
1532 }
1533 }
1534
1535 void CImage::Convert(const uint8_t* from_data, const BitmapInfo* from_fmt)
1536 {
1537 CImage ci(from_fmt, from_data);
1538 Convert(&ci);
1539 }
1540
1541 void CImage::Convert(const CImage* from_img)
1542 {
1543 #if 0
1544 AVM_WRITE("CImage", "Convert from\n");
1545 from_img->GetFmt()->Print();
1546 const CImage* f = from_img;
1547 printf("From strides: %d, %d, %d planes: %p, %p, %p\n", f->m_iStride[0], f->m_iStride[1], f->m_iStride[2], f->m_pPlane[0], f->m_pPlane[1], f->m_pPlane[2]);
1548 AVM_WRITE("CImage", "To 0x%x -> 0x%x\n", Format(), from_img->Format());
1549 m_pInfo.Print();
1550 #endif
1551
1552 //assert(m_iHeight == from_img->m_iHeight
1553 // && m_iWidth == from_img->m_iWidth);
1554
1555 // FIXME
1556 bool flip_dir = ((GetFmt()->biHeight * from_img->GetFmt()->biHeight) < 0);
1557
1558 //printf("FLIP %d\n", flip_dir);
1559 if (Format() == from_img->Format())
1560 {
1561 switch (Format())
1562 {
1563 case IMG_FMT_YV12:
1564 case IMG_FMT_I420:
1565 yuvconv(this, from_img, flip_dir, I420TOYV12);
1566 return;
1567 case IMG_FMT_Y422:
1568 case IMG_FMT_UYVY:
1569 case IMG_FMT_YUY2:
1570 case 15:
1571 case 16:
1572 lineconvert(this, from_img, flip_dir, BGR16TOBGR16);
1573 return;
1574 case 24:
1575 lineconvert(this, from_img, flip_dir, BGR24TOBGR24);
1576 return;
1577 case 32:
1578 lineconvert(this, from_img, flip_dir, BGR32TOBGR32);
1579 return;
1580 default:
1581 printf("Format %.4s 0x%x\n", (const char*) &m_iFormat, m_iFormat);
1582 //abort();
1583 }
1584 }
1585
1586 if (from_img->Format() == 24)
1587 {
1588 anyFromBGR24(this, from_img, flip_dir);
1589 return;
1590 }
1591 switch (Format())
1592 {
1593 case 24:
1594 anyToBGR24(this, from_img, flip_dir);
1595 return;
1596 //shortcuts here
1597 case 16:
1598 switch (from_img->Format())
1599 {
1600 case 15: lineconvert(this, from_img, flip_dir, BGR15TOBGR16); return;
1601 case IMG_FMT_YV12:
1602 case IMG_FMT_I420:
1603 yuvconv(this, from_img, flip_dir, YV12TOBGR16);
1604 return;
1605 }
1606 break;
1607 case IMG_FMT_YUY2:
1608 switch (from_img->Format())
1609 {
1610 case IMG_FMT_YV12:
1611 case IMG_FMT_I420:
1612 yuvconv(this, from_img, flip_dir, YV12TOYUY2);
1613 return;
1614 }
1615 break;
1616 case IMG_FMT_YV12:
1617 switch (from_img->Format())
1618 {
1619 case IMG_FMT_YUY2:
1620 yuvconv(this, from_img, flip_dir, YUY2TOYV12);
1621 return;
1622 case IMG_FMT_Y422:
1623 case IMG_FMT_UYVY:
1624 yuvconv(this, from_img, flip_dir, UYVYTOYV12);
1625 return;
1626 case IMG_FMT_I420:
1627 case IMG_FMT_YV12: // copy
1628 yuvconv(this, from_img, flip_dir, I420TOYV12);
1629 return;
1630 case IMG_FMT_I411:
1631 yuvconv(this, from_img, flip_dir, I411TOYV12);
1632 return;
1633 case IMG_FMT_I422:
1634 yuvconv(this, from_img, flip_dir, I422TOYV12);
1635 return;
1636 case IMG_FMT_I444:
1637 yuvconv(this, from_img, flip_dir, I444TOYV12);
1638 return;
1639 case IMG_FMT_I410:
1640 yuvconv(this, from_img, flip_dir, I410TOYV12);
1641 return;
1642 case IMG_FMT_Y800:
1643 yuvconv(this, from_img, flip_dir, Y800TOYV12);
1644 return;
1645 }
1646 break;
1647 }
1648
1649 CImage tmp(0, m_iWidth, m_iHeight);
1650 anyToBGR24(&tmp, from_img, flip_dir);
1651 anyFromBGR24(this, &tmp, false);
1652 }
1653
1654 void CImage::Slice(const ci_surface_t* ci, int alpha)
1655 {
1656 //printf("aaa %p %d %d %d %d x %d h:%d\n", planes, strides[0], strides[1], strides[2], w, h, y);
1657 //printf("bbb %p %d %d %d %d x %d\n", m_pPlane, m_iStride[0], m_iStride[1], m_iStride[2], m_iWidth, m_iHeight);
1658 //printf("W %d H %d %d, %d %d\n", m_Window.w, m_Window.h, m_Window.x, m_Window.y, ci->m_Window.y);
1659 if (m_iFormat == ci->m_iFormat)
1660 {
1661 int w = ci->m_iWidth;
1662 if (m_Window.w < w)
1663 w = m_Window.w;
1664 int h = ci->m_iHeight;
1665 if (m_Window.h < h)
1666 h = m_Window.h;
1667 int d = 1;
1668 int dx = m_Window.x;
1669 int dy = m_Window.y;
1670 int cx = ci->m_Window.x;
1671 int cy = ci->m_Window.y;
1672
1673 if (cx < dx)
1674 {
1675 w -= (dx - dx);
1676 cx = dx;
1677 }
1678 else
1679 dx = cx;
1680
1681
1682 if (cy < dy)
1683 {
1684 h -= (dy - cy);
1685 cy = dy;
1686 }
1687 else
1688 dy = cy;
1689
1690 //printf("WWW %d HHH %d %d %d %d %d \n", w, h, dx, cx, dy, cy);
1691 if (w > 0 && h > 0)
1692 {
1693 switch (m_iFormat)
1694 {
1695 case IMG_FMT_YV12:
1696 case IMG_FMT_I420:
1697 for (int i = 0; i < 3; i++)
1698 {
1699 stride_memcpy(m_pPlane[i] + m_iStride[i] * dy / d + dx / d,
1700 m_iStride[i],
1701 ci->m_pPlane[i] + ci->m_iStride[i] * (cy - ci->m_Window.y) / d + (cx - ci->m_Window.x) / d,
1702 ci->m_iStride[i],
1703 w / d, h / d);
1704 d = 2;
1705 }
1706 emms();
1707 break;
1708 }
1709 }
1710 }
1711 }
1712
1713 void CImage::Interlaced(CImage* from)
1714 {
1715 int dv = 1;
1716
1717 switch (m_iFormat)
1718 {
1719 case IMG_FMT_YV12:
1720 case IMG_FMT_I420:
1721 for (int i = 0; i < 3; ++i)
1722 {
1723 for (int j = 0; j < m_iHeight / dv; j += 2)
1724 {
1725 memcpy(GetWindow(i) + j * m_iStride[i],
1726 from->GetWindow(i) + (j + 1) * m_iStride[i],
1727 m_iWidth / dv);
1728 #if 1
1729 memcpy(GetWindow(i) + (j + 1) * m_iStride[i],
1730 from->GetWindow(i) + (j + 1) * m_iStride[i],
1731 m_iWidth / dv);
1732 #else
1733 memset(GetWindow(i) + (j + 1) * m_iStride[i],
1734 0,
1735 m_iWidth / dv);
1736 #endif
1737 memcpy(from->GetWindow(i) + (j + 1) * m_iStride[i],
1738 from->GetWindow(i) + j * m_iStride[i],
1739 m_iWidth / dv);
1740 }
1741
1742 dv = 2;
1743 }
1744 //return m_pPlane[idx] + m_iStride[idx] * m_Window.y / dv + m_iBpp * m_Window.x / dv;
1745 emms();
1746 break;
1747 }
1748 }
1749
1750 void ci_surface_t::SetWindow(int x, int y, int w, int h)
1751 {
1752 //printf("SET WINDOWS %d %d %d %d\n", x, y, w, h);
1753 m_Window.x = x; m_Window.y = y, m_Window.w = w; m_Window.h = h;
1754 if (w <= 0 || h <= 0 || x < 0 || y < 0
1755 || x > m_iWidth || y > m_iWidth)
1756 {
1757 x = y = 0; m_Window.w = m_iWidth; m_Window.h = m_iHeight;
1758 }
1759 else
1760 {
1761 if ((x + w) > m_iWidth)
1762 m_Window.w = m_iWidth - x;
1763 if ((y + h) > m_iHeight)
1764 m_Window.h = m_iHeight - y;
1765 }
1766 //printf("SET WINDOWS DONE %d %d %d %d\n", m_Window.x, m_Window.y, m_Window.w, m_Window.h);
1767 }
1768
1769
1770 uint8_t* ci_surface_t::GetWindow(uint_t idx)
1771 {
1772 int dv = 1;
1773 //printf("GETWIN1 %d %d\n", m_Window.x, m_iBpp);
1774 if (idx > 0)
1775 {
1776 switch (m_iFormat)
1777 {
1778 case IMG_FMT_YV12:
1779 case IMG_FMT_I420:
1780 dv = 2;
1781 }
1782 }
1783 return m_pPlane[idx] + m_iStride[idx] * m_Window.y / dv + m_iBpp * m_Window.x / dv;
1784 }
1785
1786 const uint8_t* ci_surface_t::GetWindow(uint_t idx) const
1787 {
1788 int dv = 1;
1789 //printf("GETWIN2 %d %d\n", m_Window.x, m_iBpp);
1790 if (idx > 0)
1791 {
1792 switch (m_iFormat)
1793 {
1794 case IMG_FMT_YV12:
1795 case IMG_FMT_I420:
1796 dv = 2;
1797 }
1798 }
1799
1800 return m_pPlane[idx] + m_iStride[idx] * m_Window.y / dv + m_iBpp * m_Window.x / dv;
1801 }
1802
1803 AVM_END_NAMESPACE;