2 * Misc image convertion routines
3 * Copyright (c) 2001, 2002 Fabrice Bellard.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "fastmemcpy.h"
25 /* XXX: totally non optimized */
27 static void yuv422_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
28 UINT8 *src, int width, int height)
33 for(y=0;y<height;y+=2) {
34 for(x=0;x<width;x+=2) {
44 for(x=0;x<width;x+=2) {
54 #define ONE_HALF (1 << (SCALEBITS - 1))
55 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
57 static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
58 UINT8 *src, int width, int height)
60 int wrap, wrap3, x, y;
61 int r, g, b, r1, g1, b1;
67 for(y=0;y<height;y+=2) {
68 for(x=0;x<width;x+=2) {
75 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
76 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
83 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
84 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
94 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
95 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
102 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
103 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
105 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
106 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
107 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
108 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
120 static void rgba32_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
121 UINT8 *src, int width, int height)
123 int wrap, wrap4, x, y;
124 int r, g, b, r1, g1, b1;
130 for(y=0;y<height;y+=2) {
131 for(x=0;x<width;x+=2) {
138 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
139 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
146 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
147 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
157 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
158 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
165 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
166 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
168 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
169 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
170 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
171 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
183 static void bgr24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
184 UINT8 *src, int width, int height)
186 int wrap, wrap3, x, y;
187 int r, g, b, r1, g1, b1;
193 for(y=0;y<height;y+=2) {
194 for(x=0;x<width;x+=2) {
201 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
202 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
209 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
210 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
220 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
221 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
228 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
229 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
231 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
232 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
233 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
234 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
246 static void bgra32_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
247 UINT8 *src, int width, int height)
249 int wrap, wrap4, x, y;
250 int r, g, b, r1, g1, b1;
256 for(y=0;y<height;y+=2) {
257 for(x=0;x<width;x+=2) {
264 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
265 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
272 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
273 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
283 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
284 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
291 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
292 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
294 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
295 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
296 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
297 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
309 /* XXX: use generic filter ? */
311 static void shrink2(UINT8 *dst, int dst_wrap,
312 UINT8 *src, int src_wrap,
313 int width, int height)
318 for(;height > 0; height--) {
322 for(w = width;w >= 4; w-=4) {
323 d[0] = (s1[0] + s2[0]) >> 1;
324 d[1] = (s1[1] + s2[1]) >> 1;
325 d[2] = (s1[2] + s2[2]) >> 1;
326 d[3] = (s1[3] + s2[3]) >> 1;
332 d[0] = (s1[0] + s2[0]) >> 1;
343 static void shrink22(UINT8 *dst, int dst_wrap,
344 UINT8 *src, int src_wrap,
345 int width, int height)
350 for(;height > 0; height--) {
354 for(w = width;w >= 4; w-=4) {
355 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
356 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
357 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
358 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
364 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
375 static void grow22(UINT8 *dst, int dst_wrap,
376 UINT8 *src, int src_wrap,
377 int width, int height)
382 for(;height > 0; height--) {
385 for(w = width;w >= 4; w-=4) {
402 /* 1x2 -> 2x1. width and height are given for the source picture */
403 static void conv411(UINT8 *dst, int dst_wrap,
404 UINT8 *src, int src_wrap,
405 int width, int height)
410 for(;height > 0; height -= 2) {
414 for(w = width;w > 0; w--) {
415 c = (s1[0] + s2[0]) >> 1;
427 static void img_copy(UINT8 *dst, int dst_wrap,
428 UINT8 *src, int src_wrap,
429 int width, int height)
431 for(;height > 0; height--) {
432 memcpy(dst, src, width);
438 #define SCALE_BITS 10
440 #define C_Y (76309 >> (16 - SCALE_BITS))
441 #define C_RV (117504 >> (16 - SCALE_BITS))
442 #define C_BU (138453 >> (16 - SCALE_BITS))
443 #define C_GU (13954 >> (16 - SCALE_BITS))
444 #define C_GV (34903 >> (16 - SCALE_BITS))
446 #define RGBOUT(r, g, b, y1)\
448 y = (y1 - 16) * C_Y;\
449 r = cm[(y + r_add) >> SCALE_BITS];\
450 g = cm[(y + g_add) >> SCALE_BITS];\
451 b = cm[(y + b_add) >> SCALE_BITS];\
454 /* XXX: no chroma interpolating is done */
455 static void yuv420p_to_bgra32(AVPicture *dst, AVPicture *src,
456 int width, int height)
458 UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
459 int w, y, cb, cr, r_add, g_add, b_add, width2;
460 UINT8 *cm = cropTbl + MAX_NEG_CROP;
463 y1_ptr = src->data[0];
464 cb_ptr = src->data[1];
465 cr_ptr = src->data[2];
467 for(;height > 0; height -= 2) {
469 d2 = d + dst->linesize[0];
470 y2_ptr = y1_ptr + src->linesize[0];
471 for(w = width2; w > 0; w --) {
472 cb = cb_ptr[0] - 128;
473 cr = cr_ptr[0] - 128;
474 r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
475 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
476 b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
478 /* output 4 pixels */
479 RGBOUT(d1[2], d1[1], d1[0], y1_ptr[0]);
480 RGBOUT(d1[6], d1[5], d1[4], y1_ptr[1]);
481 RGBOUT(d2[2], d2[1], d2[0], y2_ptr[0]);
482 RGBOUT(d2[6], d2[5], d2[4], y2_ptr[1]);
484 d1[3] = d1[7] = d2[3] = d2[7] = 255;
493 d += 2 * dst->linesize[0];
494 y1_ptr += 2 * src->linesize[0] - width;
495 cb_ptr += src->linesize[1] - width2;
496 cr_ptr += src->linesize[2] - width2;
500 /* XXX: no chroma interpolating is done */
501 static void yuv420p_to_rgba32(AVPicture *dst, AVPicture *src,
502 int width, int height)
504 UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
505 int w, y, cb, cr, r_add, g_add, b_add, width2;
506 UINT8 *cm = cropTbl + MAX_NEG_CROP;
509 y1_ptr = src->data[0];
510 cb_ptr = src->data[1];
511 cr_ptr = src->data[2];
513 for(;height > 0; height -= 2) {
515 d2 = d + dst->linesize[0];
516 y2_ptr = y1_ptr + src->linesize[0];
517 for(w = width2; w > 0; w --) {
518 cb = cb_ptr[0] - 128;
519 cr = cr_ptr[0] - 128;
520 r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
521 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
522 b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
524 /* output 4 pixels */
525 RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
526 RGBOUT(d1[4], d1[5], d1[6], y1_ptr[1]);
527 RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]);
528 RGBOUT(d2[4], d2[5], d2[6], y2_ptr[1]);
530 d1[3] = d1[7] = d2[3] = d2[7] = 255;
539 d += 2 * dst->linesize[0];
540 y1_ptr += 2 * src->linesize[0] - width;
541 cb_ptr += src->linesize[1] - width2;
542 cr_ptr += src->linesize[2] - width2;
546 /* XXX: no chroma interpolating is done */
547 static void yuv420p_to_rgb24(AVPicture *dst, AVPicture *src,
548 int width, int height)
550 UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
551 int w, y, cb, cr, r_add, g_add, b_add, width2;
552 UINT8 *cm = cropTbl + MAX_NEG_CROP;
555 y1_ptr = src->data[0];
556 cb_ptr = src->data[1];
557 cr_ptr = src->data[2];
559 for(;height > 0; height -= 2) {
561 d2 = d + dst->linesize[0];
562 y2_ptr = y1_ptr + src->linesize[0];
563 for(w = width2; w > 0; w --) {
564 cb = cb_ptr[0] - 128;
565 cr = cr_ptr[0] - 128;
566 r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
567 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
568 b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
570 /* output 4 pixels */
571 RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
572 RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
573 RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]);
574 RGBOUT(d2[3], d2[4], d2[5], y2_ptr[1]);
583 d += 2 * dst->linesize[0];
584 y1_ptr += 2 * src->linesize[0] - width;
585 cb_ptr += src->linesize[1] - width2;
586 cr_ptr += src->linesize[2] - width2;
590 /* XXX: no chroma interpolating is done */
591 static void yuv422p_to_rgb24(AVPicture *dst, AVPicture *src,
592 int width, int height)
594 UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1;
595 int w, y, cb, cr, r_add, g_add, b_add, width2;
596 UINT8 *cm = cropTbl + MAX_NEG_CROP;
599 y1_ptr = src->data[0];
600 cb_ptr = src->data[1];
601 cr_ptr = src->data[2];
603 for(;height > 0; height --) {
605 for(w = width2; w > 0; w --) {
606 cb = cb_ptr[0] - 128;
607 cr = cr_ptr[0] - 128;
608 r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
609 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
610 b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
612 /* output 2 pixels */
613 RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
614 RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
621 d += dst->linesize[0];
622 y1_ptr += src->linesize[0] - width;
623 cb_ptr += src->linesize[1] - width2;
624 cr_ptr += src->linesize[2] - width2;
628 /* XXX: always use linesize. Return -1 if not supported */
629 int img_convert(AVPicture *dst, int dst_pix_fmt,
630 AVPicture *src, int pix_fmt,
631 int width, int height)
635 assert(pix_fmt != PIX_FMT_ANY && dst_pix_fmt != PIX_FMT_ANY);
637 if (dst_pix_fmt == pix_fmt) {
639 case PIX_FMT_YUV420P:
645 img_copy(dst->data[i], dst->linesize[i],
646 src->data[i], src->linesize[i],
653 } else if (dst_pix_fmt == PIX_FMT_YUV420P) {
656 case PIX_FMT_YUV411P:
657 img_copy(dst->data[0], dst->linesize[0],
658 src->data[0], src->linesize[0],
660 conv411(dst->data[1], dst->linesize[1],
661 src->data[1], src->linesize[1],
663 conv411(dst->data[2], dst->linesize[2],
664 src->data[2], src->linesize[2],
667 case PIX_FMT_YUV410P:
668 img_copy(dst->data[0], dst->linesize[0],
669 src->data[0], src->linesize[0],
671 grow22(dst->data[1], dst->linesize[1],
672 src->data[1], src->linesize[1],
674 grow22(dst->data[2], dst->linesize[2],
675 src->data[2], src->linesize[2],
678 case PIX_FMT_YUV420P:
680 img_copy(dst->data[i], dst->linesize[i],
681 src->data[i], src->linesize[i],
685 case PIX_FMT_YUV422P:
686 img_copy(dst->data[0], dst->linesize[0],
687 src->data[0], src->linesize[0],
692 shrink2(dst->data[i], dst->linesize[i],
693 src->data[i], src->linesize[i],
697 case PIX_FMT_YUV444P:
698 img_copy(dst->data[0], dst->linesize[0],
699 src->data[0], src->linesize[0],
704 shrink22(dst->data[i], dst->linesize[i],
705 src->data[i], src->linesize[i],
710 yuv422_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
711 src->data[0], width, height);
714 rgb24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
715 src->data[0], width, height);
718 rgba32_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
719 src->data[0], width, height);
722 bgr24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
723 src->data[0], width, height);
726 bgra32_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
727 src->data[0], width, height);
732 } else if (dst_pix_fmt == PIX_FMT_RGB24) {
734 case PIX_FMT_YUV420P:
735 yuv420p_to_rgb24(dst, src, width, height);
737 case PIX_FMT_YUV422P:
738 yuv422p_to_rgb24(dst, src, width, height);
743 } else if (dst_pix_fmt == PIX_FMT_RGBA32) {
745 case PIX_FMT_YUV420P:
746 yuv420p_to_rgba32(dst, src, width, height);
751 } else if (dst_pix_fmt == PIX_FMT_BGRA32) {
753 case PIX_FMT_YUV420P:
754 yuv420p_to_bgra32(dst, src, width, height);
765 /* filter parameters: [-1 4 2 4 -1] // 8 */
766 static void deinterlace_line(UINT8 *dst, UINT8 *src, int src_wrap,
769 UINT8 *cm = cropTbl + MAX_NEG_CROP;
773 for(;size > 0;size--) {
784 dst[0] = cm[(sum + 4) >> 3];
790 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
791 top field is copied as is, but the bottom field is deinterlaced
792 against the top field. */
793 static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
794 UINT8 *src1, int src_wrap,
795 int width, int height)
801 buf = (UINT8*)av_malloc(5 * width);
804 for(y=0;y<height;y+=2) {
805 /* copy top field line */
806 memcpy(dst, src, width);
808 src += (1 - 2) * src_wrap;
810 if (y1 >= 0 && (y1 + 4) < height) {
811 /* fast case : no edges */
812 deinterlace_line(dst, src, src_wrap, width);
814 /* in order to use the same function, we use an intermediate buffer */
818 memcpy(ptr, src1, width);
819 else if (y1 >= height)
820 memcpy(ptr, src1 + (height - 1) * src_wrap, width);
822 memcpy(ptr, src1 + y1 * src_wrap, width);
826 deinterlace_line(dst, buf, width, width);
829 src += (2 + 1) * src_wrap;
835 /* deinterlace, return -1 if format not handled */
836 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
837 int pix_fmt, int width, int height)
841 if (pix_fmt != PIX_FMT_YUV420P &&
842 pix_fmt != PIX_FMT_YUV422P &&
843 pix_fmt != PIX_FMT_YUV444P)
845 if ((width & 1) != 0 || (height & 3) != 0)
851 case PIX_FMT_YUV420P:
855 case PIX_FMT_YUV422P:
862 deinterlace_bottom_field(dst->data[i], dst->linesize[i],
863 src->data[i], src->linesize[i],