2 * Misc image convertion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Misc image convertion routines.
28 * - write 'ffimg' program to test all the image related stuff
29 * - move all api to slice based system
30 * - integrate deinterlacing, postprocessing and scaling in the conversion process
37 #include "libvo/fastmemcpy.h"
44 #define xglue(x, y) x ## y
45 #define glue(x, y) xglue(x, y)
47 #define FF_COLOR_RGB 0 /**< RGB color space */
48 #define FF_COLOR_GRAY 1 /**< gray color space */
49 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
50 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
52 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
53 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
54 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
56 typedef struct PixFmtInfo {
58 uint8_t nb_channels; /**< number of channels (including alpha) */
59 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
60 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
61 uint8_t is_alpha : 1; /**< true if alpha can be specified */
62 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
63 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
64 uint8_t depth; /**< bit depth of the color components */
67 /* this table gives more information about formats */
68 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
73 .color_type = FF_COLOR_YUV,
74 .pixel_type = FF_PIXEL_PLANAR,
76 .x_chroma_shift = 1, .y_chroma_shift = 1,
81 .color_type = FF_COLOR_YUV,
82 .pixel_type = FF_PIXEL_PLANAR,
84 .x_chroma_shift = 1, .y_chroma_shift = 0,
89 .color_type = FF_COLOR_YUV,
90 .pixel_type = FF_PIXEL_PLANAR,
92 .x_chroma_shift = 0, .y_chroma_shift = 0,
97 .color_type = FF_COLOR_YUV,
98 .pixel_type = FF_PIXEL_PACKED,
100 .x_chroma_shift = 1, .y_chroma_shift = 0,
102 [PIX_FMT_UYVY422] = {
105 .color_type = FF_COLOR_YUV,
106 .pixel_type = FF_PIXEL_PACKED,
108 .x_chroma_shift = 1, .y_chroma_shift = 0,
110 [PIX_FMT_YUV410P] = {
113 .color_type = FF_COLOR_YUV,
114 .pixel_type = FF_PIXEL_PLANAR,
116 .x_chroma_shift = 2, .y_chroma_shift = 2,
118 [PIX_FMT_YUV411P] = {
121 .color_type = FF_COLOR_YUV,
122 .pixel_type = FF_PIXEL_PLANAR,
124 .x_chroma_shift = 2, .y_chroma_shift = 0,
128 [PIX_FMT_YUVJ420P] = {
131 .color_type = FF_COLOR_YUV_JPEG,
132 .pixel_type = FF_PIXEL_PLANAR,
134 .x_chroma_shift = 1, .y_chroma_shift = 1,
136 [PIX_FMT_YUVJ422P] = {
139 .color_type = FF_COLOR_YUV_JPEG,
140 .pixel_type = FF_PIXEL_PLANAR,
142 .x_chroma_shift = 1, .y_chroma_shift = 0,
144 [PIX_FMT_YUVJ444P] = {
147 .color_type = FF_COLOR_YUV_JPEG,
148 .pixel_type = FF_PIXEL_PLANAR,
150 .x_chroma_shift = 0, .y_chroma_shift = 0,
157 .color_type = FF_COLOR_RGB,
158 .pixel_type = FF_PIXEL_PACKED,
160 .x_chroma_shift = 0, .y_chroma_shift = 0,
165 .color_type = FF_COLOR_RGB,
166 .pixel_type = FF_PIXEL_PACKED,
168 .x_chroma_shift = 0, .y_chroma_shift = 0,
172 .nb_channels = 4, .is_alpha = 1,
173 .color_type = FF_COLOR_RGB,
174 .pixel_type = FF_PIXEL_PACKED,
176 .x_chroma_shift = 0, .y_chroma_shift = 0,
181 .color_type = FF_COLOR_RGB,
182 .pixel_type = FF_PIXEL_PACKED,
184 .x_chroma_shift = 0, .y_chroma_shift = 0,
189 .color_type = FF_COLOR_RGB,
190 .pixel_type = FF_PIXEL_PACKED,
192 .x_chroma_shift = 0, .y_chroma_shift = 0,
195 /* gray / mono formats */
196 [PIX_FMT_GRAY16BE] = {
199 .color_type = FF_COLOR_GRAY,
200 .pixel_type = FF_PIXEL_PLANAR,
203 [PIX_FMT_GRAY16LE] = {
206 .color_type = FF_COLOR_GRAY,
207 .pixel_type = FF_PIXEL_PLANAR,
213 .color_type = FF_COLOR_GRAY,
214 .pixel_type = FF_PIXEL_PLANAR,
217 [PIX_FMT_MONOWHITE] = {
220 .color_type = FF_COLOR_GRAY,
221 .pixel_type = FF_PIXEL_PLANAR,
224 [PIX_FMT_MONOBLACK] = {
227 .color_type = FF_COLOR_GRAY,
228 .pixel_type = FF_PIXEL_PLANAR,
232 /* paletted formats */
235 .nb_channels = 4, .is_alpha = 1,
236 .color_type = FF_COLOR_RGB,
237 .pixel_type = FF_PIXEL_PALETTE,
240 [PIX_FMT_XVMC_MPEG2_MC] = {
243 [PIX_FMT_XVMC_MPEG2_IDCT] = {
246 [PIX_FMT_UYYVYY411] = {
249 .color_type = FF_COLOR_YUV,
250 .pixel_type = FF_PIXEL_PACKED,
252 .x_chroma_shift = 2, .y_chroma_shift = 0,
256 .nb_channels = 4, .is_alpha = 1,
257 .color_type = FF_COLOR_RGB,
258 .pixel_type = FF_PIXEL_PACKED,
260 .x_chroma_shift = 0, .y_chroma_shift = 0,
265 .color_type = FF_COLOR_RGB,
266 .pixel_type = FF_PIXEL_PACKED,
268 .x_chroma_shift = 0, .y_chroma_shift = 0,
273 .color_type = FF_COLOR_RGB,
274 .pixel_type = FF_PIXEL_PACKED,
276 .x_chroma_shift = 0, .y_chroma_shift = 0,
281 .color_type = FF_COLOR_RGB,
282 .pixel_type = FF_PIXEL_PACKED,
284 .x_chroma_shift = 0, .y_chroma_shift = 0,
289 .color_type = FF_COLOR_RGB,
290 .pixel_type = FF_PIXEL_PACKED,
292 .x_chroma_shift = 0, .y_chroma_shift = 0,
294 [PIX_FMT_RGB4_BYTE] = {
297 .color_type = FF_COLOR_RGB,
298 .pixel_type = FF_PIXEL_PACKED,
300 .x_chroma_shift = 0, .y_chroma_shift = 0,
305 .color_type = FF_COLOR_RGB,
306 .pixel_type = FF_PIXEL_PACKED,
308 .x_chroma_shift = 0, .y_chroma_shift = 0,
313 .color_type = FF_COLOR_RGB,
314 .pixel_type = FF_PIXEL_PACKED,
316 .x_chroma_shift = 0, .y_chroma_shift = 0,
318 [PIX_FMT_BGR4_BYTE] = {
321 .color_type = FF_COLOR_RGB,
322 .pixel_type = FF_PIXEL_PACKED,
324 .x_chroma_shift = 0, .y_chroma_shift = 0,
329 .color_type = FF_COLOR_YUV,
330 .pixel_type = FF_PIXEL_PLANAR,
332 .x_chroma_shift = 1, .y_chroma_shift = 1,
337 .color_type = FF_COLOR_YUV,
338 .pixel_type = FF_PIXEL_PLANAR,
340 .x_chroma_shift = 1, .y_chroma_shift = 1,
343 [PIX_FMT_BGR32_1] = {
345 .nb_channels = 4, .is_alpha = 1,
346 .color_type = FF_COLOR_RGB,
347 .pixel_type = FF_PIXEL_PACKED,
349 .x_chroma_shift = 0, .y_chroma_shift = 0,
351 [PIX_FMT_RGB32_1] = {
353 .nb_channels = 4, .is_alpha = 1,
354 .color_type = FF_COLOR_RGB,
355 .pixel_type = FF_PIXEL_PACKED,
357 .x_chroma_shift = 0, .y_chroma_shift = 0,
361 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
363 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
364 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
367 const char *avcodec_get_pix_fmt_name(int pix_fmt)
369 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
372 return pix_fmt_info[pix_fmt].name;
375 enum PixelFormat avcodec_get_pix_fmt(const char* name)
379 for (i=0; i < PIX_FMT_NB; i++)
380 if (!strcmp(pix_fmt_info[i].name, name))
385 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
386 int pix_fmt, int width, int height)
388 int size, w2, h2, size2;
389 const PixFmtInfo *pinfo;
391 if(avcodec_check_dimensions(NULL, width, height))
394 pinfo = &pix_fmt_info[pix_fmt];
395 size = width * height;
397 case PIX_FMT_YUV420P:
398 case PIX_FMT_YUV422P:
399 case PIX_FMT_YUV444P:
400 case PIX_FMT_YUV410P:
401 case PIX_FMT_YUV411P:
402 case PIX_FMT_YUVJ420P:
403 case PIX_FMT_YUVJ422P:
404 case PIX_FMT_YUVJ444P:
405 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
406 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
408 picture->data[0] = ptr;
409 picture->data[1] = picture->data[0] + size;
410 picture->data[2] = picture->data[1] + size2;
411 picture->linesize[0] = width;
412 picture->linesize[1] = w2;
413 picture->linesize[2] = w2;
414 return size + 2 * size2;
417 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
418 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
420 picture->data[0] = ptr;
421 picture->data[1] = picture->data[0] + size;
422 picture->data[2] = NULL;
423 picture->linesize[0] = width;
424 picture->linesize[1] = w2;
425 picture->linesize[2] = 0;
426 return size + 2 * size2;
429 picture->data[0] = ptr;
430 picture->data[1] = NULL;
431 picture->data[2] = NULL;
432 picture->linesize[0] = width * 3;
436 case PIX_FMT_RGB32_1:
437 case PIX_FMT_BGR32_1:
438 picture->data[0] = ptr;
439 picture->data[1] = NULL;
440 picture->data[2] = NULL;
441 picture->linesize[0] = width * 4;
443 case PIX_FMT_GRAY16BE:
444 case PIX_FMT_GRAY16LE:
449 case PIX_FMT_YUYV422:
450 picture->data[0] = ptr;
451 picture->data[1] = NULL;
452 picture->data[2] = NULL;
453 picture->linesize[0] = width * 2;
455 case PIX_FMT_UYVY422:
456 picture->data[0] = ptr;
457 picture->data[1] = NULL;
458 picture->data[2] = NULL;
459 picture->linesize[0] = width * 2;
461 case PIX_FMT_UYYVYY411:
462 picture->data[0] = ptr;
463 picture->data[1] = NULL;
464 picture->data[2] = NULL;
465 picture->linesize[0] = width + width/2;
466 return size + size/2;
469 case PIX_FMT_RGB4_BYTE:
470 case PIX_FMT_BGR4_BYTE:
472 picture->data[0] = ptr;
473 picture->data[1] = NULL;
474 picture->data[2] = NULL;
475 picture->linesize[0] = width;
479 picture->data[0] = ptr;
480 picture->data[1] = NULL;
481 picture->data[2] = NULL;
482 picture->linesize[0] = width / 2;
484 case PIX_FMT_MONOWHITE:
485 case PIX_FMT_MONOBLACK:
486 picture->data[0] = ptr;
487 picture->data[1] = NULL;
488 picture->data[2] = NULL;
489 picture->linesize[0] = (width + 7) >> 3;
490 return picture->linesize[0] * height;
492 size2 = (size + 3) & ~3;
493 picture->data[0] = ptr;
494 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
495 picture->data[2] = NULL;
496 picture->linesize[0] = width;
497 picture->linesize[1] = 4;
498 return size2 + 256 * 4;
501 picture->data[0] = NULL;
502 picture->data[1] = NULL;
503 picture->data[2] = NULL;
504 picture->data[3] = NULL;
509 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
510 unsigned char *dest, int dest_size)
512 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
513 int i, j, w, h, data_planes;
514 const unsigned char* s;
515 int size = avpicture_get_size(pix_fmt, width, height);
517 if (size > dest_size || size < 0)
520 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
521 if (pix_fmt == PIX_FMT_YUYV422 ||
522 pix_fmt == PIX_FMT_UYVY422 ||
523 pix_fmt == PIX_FMT_BGR565 ||
524 pix_fmt == PIX_FMT_BGR555 ||
525 pix_fmt == PIX_FMT_RGB565 ||
526 pix_fmt == PIX_FMT_RGB555)
528 else if (pix_fmt == PIX_FMT_UYYVYY411)
530 else if (pix_fmt == PIX_FMT_PAL8)
533 w = width * (pf->depth * pf->nb_channels / 8);
538 data_planes = pf->nb_channels;
539 w = (width*pf->depth + 7)/8;
543 for (i=0; i<data_planes; i++) {
545 w = width >> pf->x_chroma_shift;
546 h = height >> pf->y_chroma_shift;
552 s += src->linesize[i];
556 if (pf->pixel_type == FF_PIXEL_PALETTE)
557 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
562 int avpicture_get_size(int pix_fmt, int width, int height)
564 AVPicture dummy_pict;
565 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
568 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
571 const PixFmtInfo *pf, *ps;
574 ps = &pix_fmt_info[src_pix_fmt];
575 pf = &pix_fmt_info[dst_pix_fmt];
579 pf = &pix_fmt_info[dst_pix_fmt];
580 if (pf->depth < ps->depth ||
581 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
582 loss |= FF_LOSS_DEPTH;
583 if (pf->x_chroma_shift > ps->x_chroma_shift ||
584 pf->y_chroma_shift > ps->y_chroma_shift)
585 loss |= FF_LOSS_RESOLUTION;
586 switch(pf->color_type) {
588 if (ps->color_type != FF_COLOR_RGB &&
589 ps->color_type != FF_COLOR_GRAY)
590 loss |= FF_LOSS_COLORSPACE;
593 if (ps->color_type != FF_COLOR_GRAY)
594 loss |= FF_LOSS_COLORSPACE;
597 if (ps->color_type != FF_COLOR_YUV)
598 loss |= FF_LOSS_COLORSPACE;
600 case FF_COLOR_YUV_JPEG:
601 if (ps->color_type != FF_COLOR_YUV_JPEG &&
602 ps->color_type != FF_COLOR_YUV &&
603 ps->color_type != FF_COLOR_GRAY)
604 loss |= FF_LOSS_COLORSPACE;
608 if (ps->color_type != pf->color_type)
609 loss |= FF_LOSS_COLORSPACE;
612 if (pf->color_type == FF_COLOR_GRAY &&
613 ps->color_type != FF_COLOR_GRAY)
614 loss |= FF_LOSS_CHROMA;
615 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
616 loss |= FF_LOSS_ALPHA;
617 if (pf->pixel_type == FF_PIXEL_PALETTE &&
618 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
619 loss |= FF_LOSS_COLORQUANT;
623 static int avg_bits_per_pixel(int pix_fmt)
626 const PixFmtInfo *pf;
628 pf = &pix_fmt_info[pix_fmt];
629 switch(pf->pixel_type) {
630 case FF_PIXEL_PACKED:
632 case PIX_FMT_YUYV422:
633 case PIX_FMT_UYVY422:
640 case PIX_FMT_UYYVYY411:
644 bits = pf->depth * pf->nb_channels;
648 case FF_PIXEL_PLANAR:
649 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
650 bits = pf->depth * pf->nb_channels;
652 bits = pf->depth + ((2 * pf->depth) >>
653 (pf->x_chroma_shift + pf->y_chroma_shift));
656 case FF_PIXEL_PALETTE:
666 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
671 int dist, i, loss, min_dist, dst_pix_fmt;
673 /* find exact color match with smallest size */
675 min_dist = 0x7fffffff;
676 for(i = 0;i < PIX_FMT_NB; i++) {
677 if (pix_fmt_mask & (1 << i)) {
678 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
680 dist = avg_bits_per_pixel(i);
681 if (dist < min_dist) {
691 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
692 int has_alpha, int *loss_ptr)
694 int dst_pix_fmt, loss_mask, i;
695 static const int loss_mask_order[] = {
696 ~0, /* no loss first */
699 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
705 /* try with successive loss */
708 loss_mask = loss_mask_order[i++];
709 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
710 has_alpha, loss_mask);
711 if (dst_pix_fmt >= 0)
719 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
723 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
724 const uint8_t *src, int src_wrap,
725 int width, int height)
729 for(;height > 0; height--) {
730 memcpy(dst, src, width);
736 void av_picture_copy(AVPicture *dst, const AVPicture *src,
737 int pix_fmt, int width, int height)
740 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
742 pf = &pix_fmt_info[pix_fmt];
743 switch(pf->pixel_type) {
744 case FF_PIXEL_PACKED:
746 case PIX_FMT_YUYV422:
747 case PIX_FMT_UYVY422:
754 case PIX_FMT_UYYVYY411:
758 bits = pf->depth * pf->nb_channels;
761 bwidth = (width * bits + 7) >> 3;
762 ff_img_copy_plane(dst->data[0], dst->linesize[0],
763 src->data[0], src->linesize[0],
766 case FF_PIXEL_PLANAR:
767 for(i = 0; i < pf->nb_channels; i++) {
771 if (i == 1 || i == 2) {
772 w >>= pf->x_chroma_shift;
773 h >>= pf->y_chroma_shift;
775 bwidth = (w * pf->depth + 7) >> 3;
776 ff_img_copy_plane(dst->data[i], dst->linesize[i],
777 src->data[i], src->linesize[i],
781 case FF_PIXEL_PALETTE:
782 ff_img_copy_plane(dst->data[0], dst->linesize[0],
783 src->data[0], src->linesize[0],
785 /* copy the palette */
786 ff_img_copy_plane(dst->data[1], dst->linesize[1],
787 src->data[1], src->linesize[1],
793 /* XXX: totally non optimized */
795 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
796 int width, int height)
798 const uint8_t *p, *p1;
799 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
807 for(;height >= 1; height -= 2) {
812 for(w = width; w >= 2; w -= 2) {
829 p1 += src->linesize[0];
830 lum1 += dst->linesize[0];
834 for(w = width; w >= 2; w -= 2) {
843 p1 += src->linesize[0];
844 lum1 += dst->linesize[0];
846 cb1 += dst->linesize[1];
847 cr1 += dst->linesize[2];
851 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
852 int width, int height)
854 const uint8_t *p, *p1;
855 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
864 for(;height >= 1; height -= 2) {
869 for(w = width; w >= 2; w -= 2) {
886 p1 += src->linesize[0];
887 lum1 += dst->linesize[0];
891 for(w = width; w >= 2; w -= 2) {
900 p1 += src->linesize[0];
901 lum1 += dst->linesize[0];
903 cb1 += dst->linesize[1];
904 cr1 += dst->linesize[2];
909 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
910 int width, int height)
912 const uint8_t *p, *p1;
913 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
920 for(;height > 0; height--) {
925 for(w = width; w >= 2; w -= 2) {
935 p1 += src->linesize[0];
936 lum1 += dst->linesize[0];
937 cb1 += dst->linesize[1];
938 cr1 += dst->linesize[2];
943 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
944 int width, int height)
946 const uint8_t *p, *p1;
947 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
954 for(;height > 0; height--) {
959 for(w = width; w >= 2; w -= 2) {
969 p1 += src->linesize[0];
970 lum1 += dst->linesize[0];
971 cb1 += dst->linesize[1];
972 cr1 += dst->linesize[2];
976 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
977 int width, int height)
980 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
987 for(;height > 0; height--) {
992 for(w = width; w >= 2; w -= 2) {
1002 p1 += dst->linesize[0];
1003 lum1 += src->linesize[0];
1004 cb1 += src->linesize[1];
1005 cr1 += src->linesize[2];
1009 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1010 int width, int height)
1013 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1017 lum1 = src->data[0];
1020 for(;height > 0; height--) {
1025 for(w = width; w >= 2; w -= 2) {
1035 p1 += dst->linesize[0];
1036 lum1 += src->linesize[0];
1037 cb1 += src->linesize[1];
1038 cr1 += src->linesize[2];
1042 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1043 int width, int height)
1045 const uint8_t *p, *p1;
1046 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1050 lum1 = dst->data[0];
1053 for(;height > 0; height--) {
1058 for(w = width; w >= 4; w -= 4) {
1070 p1 += src->linesize[0];
1071 lum1 += dst->linesize[0];
1072 cb1 += dst->linesize[1];
1073 cr1 += dst->linesize[2];
1078 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1079 int width, int height)
1082 uint8_t *line1, *line2, *linesrc = dst->data[0];
1083 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1084 uint8_t *cb1, *cb2 = src->data[1];
1085 uint8_t *cr1, *cr2 = src->data[2];
1087 for(h = height / 2; h--;) {
1089 line2 = linesrc + dst->linesize[0];
1092 lum2 = lumsrc + src->linesize[0];
1097 for(w = width / 2; w--;) {
1098 *line1++ = *lum1++; *line2++ = *lum2++;
1099 *line1++ = *line2++ = *cb1++;
1100 *line1++ = *lum1++; *line2++ = *lum2++;
1101 *line1++ = *line2++ = *cr1++;
1104 linesrc += dst->linesize[0] * 2;
1105 lumsrc += src->linesize[0] * 2;
1106 cb2 += src->linesize[1];
1107 cr2 += src->linesize[2];
1111 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1112 int width, int height)
1115 uint8_t *line1, *line2, *linesrc = dst->data[0];
1116 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1117 uint8_t *cb1, *cb2 = src->data[1];
1118 uint8_t *cr1, *cr2 = src->data[2];
1120 for(h = height / 2; h--;) {
1122 line2 = linesrc + dst->linesize[0];
1125 lum2 = lumsrc + src->linesize[0];
1130 for(w = width / 2; w--;) {
1131 *line1++ = *line2++ = *cb1++;
1132 *line1++ = *lum1++; *line2++ = *lum2++;
1133 *line1++ = *line2++ = *cr1++;
1134 *line1++ = *lum1++; *line2++ = *lum2++;
1137 linesrc += dst->linesize[0] * 2;
1138 lumsrc += src->linesize[0] * 2;
1139 cb2 += src->linesize[1];
1140 cr2 += src->linesize[2];
1144 #define SCALEBITS 10
1145 #define ONE_HALF (1 << (SCALEBITS - 1))
1146 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1148 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1152 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1153 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1155 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1158 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1160 y = ((y1) - 16) * FIX(255.0/219.0);\
1161 r = cm[(y + r_add) >> SCALEBITS];\
1162 g = cm[(y + g_add) >> SCALEBITS];\
1163 b = cm[(y + b_add) >> SCALEBITS];\
1166 #define YUV_TO_RGB1(cb1, cr1)\
1170 r_add = FIX(1.40200) * cr + ONE_HALF;\
1171 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1172 b_add = FIX(1.77200) * cb + ONE_HALF;\
1175 #define YUV_TO_RGB2(r, g, b, y1)\
1177 y = (y1) << SCALEBITS;\
1178 r = cm[(y + r_add) >> SCALEBITS];\
1179 g = cm[(y + g_add) >> SCALEBITS];\
1180 b = cm[(y + b_add) >> SCALEBITS];\
1183 #define Y_CCIR_TO_JPEG(y)\
1184 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1186 #define Y_JPEG_TO_CCIR(y)\
1187 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1189 #define C_CCIR_TO_JPEG(y)\
1190 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1192 /* NOTE: the clamp is really necessary! */
1193 static inline int C_JPEG_TO_CCIR(int y) {
1194 y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1201 #define RGB_TO_Y(r, g, b) \
1202 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1203 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1205 #define RGB_TO_U(r1, g1, b1, shift)\
1206 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1207 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1209 #define RGB_TO_V(r1, g1, b1, shift)\
1210 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1211 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1213 #define RGB_TO_Y_CCIR(r, g, b) \
1214 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1215 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1217 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1218 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1219 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1221 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1222 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1223 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1225 static uint8_t y_ccir_to_jpeg[256];
1226 static uint8_t y_jpeg_to_ccir[256];
1227 static uint8_t c_ccir_to_jpeg[256];
1228 static uint8_t c_jpeg_to_ccir[256];
1230 /* init various conversion tables */
1231 static void img_convert_init(void)
1234 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1236 for(i = 0;i < 256; i++) {
1237 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1238 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1239 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1240 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1244 /* apply to each pixel the given table */
1245 static void img_apply_table(uint8_t *dst, int dst_wrap,
1246 const uint8_t *src, int src_wrap,
1247 int width, int height, const uint8_t *table1)
1252 const uint8_t *table;
1255 for(;height > 0; height--) {
1279 /* XXX: use generic filter ? */
1280 /* XXX: in most cases, the sampling position is incorrect */
1283 static void shrink41(uint8_t *dst, int dst_wrap,
1284 const uint8_t *src, int src_wrap,
1285 int width, int height)
1291 for(;height > 0; height--) {
1294 for(w = width;w > 0; w--) {
1295 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1305 static void shrink21(uint8_t *dst, int dst_wrap,
1306 const uint8_t *src, int src_wrap,
1307 int width, int height)
1313 for(;height > 0; height--) {
1316 for(w = width;w > 0; w--) {
1317 d[0] = (s[0] + s[1]) >> 1;
1327 static void shrink12(uint8_t *dst, int dst_wrap,
1328 const uint8_t *src, int src_wrap,
1329 int width, int height)
1333 const uint8_t *s1, *s2;
1335 for(;height > 0; height--) {
1339 for(w = width;w >= 4; w-=4) {
1340 d[0] = (s1[0] + s2[0]) >> 1;
1341 d[1] = (s1[1] + s2[1]) >> 1;
1342 d[2] = (s1[2] + s2[2]) >> 1;
1343 d[3] = (s1[3] + s2[3]) >> 1;
1349 d[0] = (s1[0] + s2[0]) >> 1;
1354 src += 2 * src_wrap;
1360 void ff_shrink22(uint8_t *dst, int dst_wrap,
1361 const uint8_t *src, int src_wrap,
1362 int width, int height)
1365 const uint8_t *s1, *s2;
1368 for(;height > 0; height--) {
1372 for(w = width;w >= 4; w-=4) {
1373 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1374 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1375 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1376 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1382 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1387 src += 2 * src_wrap;
1393 void ff_shrink44(uint8_t *dst, int dst_wrap,
1394 const uint8_t *src, int src_wrap,
1395 int width, int height)
1398 const uint8_t *s1, *s2, *s3, *s4;
1401 for(;height > 0; height--) {
1407 for(w = width;w > 0; w--) {
1408 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1409 s2[0] + s2[1] + s2[2] + s2[3] +
1410 s3[0] + s3[1] + s3[2] + s3[3] +
1411 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1418 src += 4 * src_wrap;
1424 void ff_shrink88(uint8_t *dst, int dst_wrap,
1425 const uint8_t *src, int src_wrap,
1426 int width, int height)
1430 for(;height > 0; height--) {
1431 for(w = width;w > 0; w--) {
1434 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1437 *(dst++) = (tmp + 32)>>6;
1438 src += 8 - 8*src_wrap;
1440 src += 8*src_wrap - 8*width;
1441 dst += dst_wrap - width;
1445 static void grow21_line(uint8_t *dst, const uint8_t *src,
1454 for(w = width;w >= 4; w-=4) {
1455 d[1] = d[0] = s1[0];
1456 d[3] = d[2] = s1[1];
1460 for(;w >= 2; w -= 2) {
1461 d[1] = d[0] = s1[0];
1465 /* only needed if width is not a multiple of two */
1466 /* XXX: veryfy that */
1472 static void grow41_line(uint8_t *dst, const uint8_t *src,
1481 for(w = width;w >= 4; w-=4) {
1493 static void grow21(uint8_t *dst, int dst_wrap,
1494 const uint8_t *src, int src_wrap,
1495 int width, int height)
1497 for(;height > 0; height--) {
1498 grow21_line(dst, src, width);
1505 static void grow22(uint8_t *dst, int dst_wrap,
1506 const uint8_t *src, int src_wrap,
1507 int width, int height)
1509 for(;height > 0; height--) {
1510 grow21_line(dst, src, width);
1518 static void grow41(uint8_t *dst, int dst_wrap,
1519 const uint8_t *src, int src_wrap,
1520 int width, int height)
1522 for(;height > 0; height--) {
1523 grow41_line(dst, src, width);
1530 static void grow44(uint8_t *dst, int dst_wrap,
1531 const uint8_t *src, int src_wrap,
1532 int width, int height)
1534 for(;height > 0; height--) {
1535 grow41_line(dst, src, width);
1536 if ((height & 3) == 1)
1543 static void conv411(uint8_t *dst, int dst_wrap,
1544 const uint8_t *src, int src_wrap,
1545 int width, int height)
1548 const uint8_t *s1, *s2;
1553 for(;height > 0; height--) {
1555 s2 = src + src_wrap;
1557 for(w = width;w > 0; w--) {
1558 c = (s1[0] + s2[0]) >> 1;
1565 src += src_wrap * 2;
1570 /* XXX: add jpeg quantize code */
1572 #define TRANSP_INDEX (6*6*6)
1574 /* this is maybe slow, but allows for extensions */
1575 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1577 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1580 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1583 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1586 pal = (uint32_t *)palette;
1588 for(r = 0; r < 6; r++) {
1589 for(g = 0; g < 6; g++) {
1590 for(b = 0; b < 6; b++) {
1591 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1592 (pal_value[g] << 8) | pal_value[b];
1599 pal[i++] = 0xff000000;
1602 /* copy bit n to bits 0 ... n - 1 */
1603 static inline unsigned int bitcopy_n(unsigned int a, int n)
1606 mask = (1 << n) - 1;
1607 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1610 /* rgb555 handling */
1612 #define RGB_NAME rgb555
1614 #define RGB_IN(r, g, b, s)\
1616 unsigned int v = ((const uint16_t *)(s))[0];\
1617 r = bitcopy_n(v >> (10 - 3), 3);\
1618 g = bitcopy_n(v >> (5 - 3), 3);\
1619 b = bitcopy_n(v << 3, 3);\
1623 #define RGB_OUT(d, r, g, b)\
1625 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1630 #include "imgconvert_template.h"
1632 /* rgb565 handling */
1634 #define RGB_NAME rgb565
1636 #define RGB_IN(r, g, b, s)\
1638 unsigned int v = ((const uint16_t *)(s))[0];\
1639 r = bitcopy_n(v >> (11 - 3), 3);\
1640 g = bitcopy_n(v >> (5 - 2), 2);\
1641 b = bitcopy_n(v << 3, 3);\
1644 #define RGB_OUT(d, r, g, b)\
1646 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1651 #include "imgconvert_template.h"
1653 /* bgr24 handling */
1655 #define RGB_NAME bgr24
1657 #define RGB_IN(r, g, b, s)\
1664 #define RGB_OUT(d, r, g, b)\
1673 #include "imgconvert_template.h"
1679 /* rgb24 handling */
1681 #define RGB_NAME rgb24
1684 #define RGB_IN(r, g, b, s)\
1691 #define RGB_OUT(d, r, g, b)\
1700 #include "imgconvert_template.h"
1702 /* rgb32 handling */
1704 #define RGB_NAME rgb32
1707 #define RGB_IN(r, g, b, s)\
1709 unsigned int v = ((const uint32_t *)(s))[0];\
1710 r = (v >> 16) & 0xff;\
1711 g = (v >> 8) & 0xff;\
1715 #define RGBA_IN(r, g, b, a, s)\
1717 unsigned int v = ((const uint32_t *)(s))[0];\
1718 a = (v >> 24) & 0xff;\
1719 r = (v >> 16) & 0xff;\
1720 g = (v >> 8) & 0xff;\
1724 #define RGBA_OUT(d, r, g, b, a)\
1726 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1731 #include "imgconvert_template.h"
1733 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1734 int width, int height, int xor_mask)
1736 const unsigned char *p;
1738 int v, dst_wrap, src_wrap;
1742 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1745 dst_wrap = dst->linesize[0] - width;
1746 for(y=0;y<height;y++) {
1749 v = *p++ ^ xor_mask;
1751 q[1] = -((v >> 6) & 1);
1752 q[2] = -((v >> 5) & 1);
1753 q[3] = -((v >> 4) & 1);
1754 q[4] = -((v >> 3) & 1);
1755 q[5] = -((v >> 2) & 1);
1756 q[6] = -((v >> 1) & 1);
1757 q[7] = -((v >> 0) & 1);
1762 v = *p++ ^ xor_mask;
1764 q[0] = -((v >> 7) & 1);
1774 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1775 int width, int height)
1777 mono_to_gray(dst, src, width, height, 0xff);
1780 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1781 int width, int height)
1783 mono_to_gray(dst, src, width, height, 0x00);
1786 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1787 int width, int height, int xor_mask)
1792 int j, b, v, n1, src_wrap, dst_wrap, y;
1795 src_wrap = src->linesize[0] - width;
1798 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1800 for(y=0;y<height;y++) {
1807 v = (v << 1) | (b >> 7);
1809 d[0] = v ^ xor_mask;
1819 v = (v << 1) | (b >> 7);
1822 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1830 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1831 int width, int height)
1833 gray_to_mono(dst, src, width, height, 0xff);
1836 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1837 int width, int height)
1839 gray_to_mono(dst, src, width, height, 0x00);
1842 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1843 int width, int height)
1845 int x, y, src_wrap, dst_wrap;
1848 src_wrap = src->linesize[0] - width;
1850 dst_wrap = dst->linesize[0] - width * 2;
1851 for(y=0; y<height; y++){
1852 for(x=0; x<width; x++){
1861 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1862 int width, int height)
1864 int x, y, src_wrap, dst_wrap;
1867 src_wrap = src->linesize[0] - width * 2;
1869 dst_wrap = dst->linesize[0] - width;
1870 for(y=0; y<height; y++){
1871 for(x=0; x<width; x++){
1880 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1881 int width, int height)
1883 gray16_to_gray(dst, src, width, height);
1886 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1887 int width, int height)
1889 AVPicture tmpsrc = *src;
1891 gray16_to_gray(dst, &tmpsrc, width, height);
1894 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1895 int width, int height)
1897 int x, y, src_wrap, dst_wrap;
1900 src_wrap = (src->linesize[0] - width * 2)/2;
1902 dst_wrap = (dst->linesize[0] - width * 2)/2;
1903 for(y=0; y<height; y++){
1904 for(x=0; x<width; x++){
1905 *d++ = bswap_16(*s++);
1913 typedef struct ConvertEntry {
1914 void (*convert)(AVPicture *dst,
1915 const AVPicture *src, int width, int height);
1918 /* Add each new convertion function in this table. In order to be able
1919 to convert from any format to any format, the following constraints
1922 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1924 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1926 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1928 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1931 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1933 The other conversion functions are just optimisations for common cases.
1935 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1936 [PIX_FMT_YUV420P] = {
1937 [PIX_FMT_YUYV422] = {
1938 .convert = yuv420p_to_yuyv422,
1940 [PIX_FMT_RGB555] = {
1941 .convert = yuv420p_to_rgb555
1943 [PIX_FMT_RGB565] = {
1944 .convert = yuv420p_to_rgb565
1947 .convert = yuv420p_to_bgr24
1950 .convert = yuv420p_to_rgb24
1953 .convert = yuv420p_to_rgb32
1955 [PIX_FMT_UYVY422] = {
1956 .convert = yuv420p_to_uyvy422,
1959 [PIX_FMT_YUV422P] = {
1960 [PIX_FMT_YUYV422] = {
1961 .convert = yuv422p_to_yuyv422,
1963 [PIX_FMT_UYVY422] = {
1964 .convert = yuv422p_to_uyvy422,
1967 [PIX_FMT_YUV444P] = {
1969 .convert = yuv444p_to_rgb24
1972 [PIX_FMT_YUVJ420P] = {
1973 [PIX_FMT_RGB555] = {
1974 .convert = yuvj420p_to_rgb555
1976 [PIX_FMT_RGB565] = {
1977 .convert = yuvj420p_to_rgb565
1980 .convert = yuvj420p_to_bgr24
1983 .convert = yuvj420p_to_rgb24
1986 .convert = yuvj420p_to_rgb32
1989 [PIX_FMT_YUVJ444P] = {
1991 .convert = yuvj444p_to_rgb24
1994 [PIX_FMT_YUYV422] = {
1995 [PIX_FMT_YUV420P] = {
1996 .convert = yuyv422_to_yuv420p,
1998 [PIX_FMT_YUV422P] = {
1999 .convert = yuyv422_to_yuv422p,
2002 [PIX_FMT_UYVY422] = {
2003 [PIX_FMT_YUV420P] = {
2004 .convert = uyvy422_to_yuv420p,
2006 [PIX_FMT_YUV422P] = {
2007 .convert = uyvy422_to_yuv422p,
2011 [PIX_FMT_YUV420P] = {
2012 .convert = rgb24_to_yuv420p
2014 [PIX_FMT_RGB565] = {
2015 .convert = rgb24_to_rgb565
2017 [PIX_FMT_RGB555] = {
2018 .convert = rgb24_to_rgb555
2021 .convert = rgb24_to_rgb32
2024 .convert = rgb24_to_bgr24
2027 .convert = rgb24_to_gray
2030 .convert = rgb24_to_pal8
2032 [PIX_FMT_YUV444P] = {
2033 .convert = rgb24_to_yuv444p
2035 [PIX_FMT_YUVJ420P] = {
2036 .convert = rgb24_to_yuvj420p
2038 [PIX_FMT_YUVJ444P] = {
2039 .convert = rgb24_to_yuvj444p
2044 .convert = rgb32_to_rgb24
2047 .convert = rgb32_to_bgr24
2049 [PIX_FMT_RGB565] = {
2050 .convert = rgb32_to_rgb565
2052 [PIX_FMT_RGB555] = {
2053 .convert = rgb32_to_rgb555
2056 .convert = rgb32_to_pal8
2058 [PIX_FMT_YUV420P] = {
2059 .convert = rgb32_to_yuv420p
2062 .convert = rgb32_to_gray
2067 .convert = bgr24_to_rgb32
2070 .convert = bgr24_to_rgb24
2072 [PIX_FMT_YUV420P] = {
2073 .convert = bgr24_to_yuv420p
2076 .convert = bgr24_to_gray
2079 [PIX_FMT_RGB555] = {
2081 .convert = rgb555_to_rgb24
2084 .convert = rgb555_to_rgb32
2086 [PIX_FMT_YUV420P] = {
2087 .convert = rgb555_to_yuv420p
2090 .convert = rgb555_to_gray
2093 [PIX_FMT_RGB565] = {
2095 .convert = rgb565_to_rgb32
2098 .convert = rgb565_to_rgb24
2100 [PIX_FMT_YUV420P] = {
2101 .convert = rgb565_to_yuv420p
2104 .convert = rgb565_to_gray
2107 [PIX_FMT_GRAY16BE] = {
2109 .convert = gray16be_to_gray
2111 [PIX_FMT_GRAY16LE] = {
2112 .convert = gray16_to_gray16
2115 [PIX_FMT_GRAY16LE] = {
2117 .convert = gray16le_to_gray
2119 [PIX_FMT_GRAY16BE] = {
2120 .convert = gray16_to_gray16
2124 [PIX_FMT_RGB555] = {
2125 .convert = gray_to_rgb555
2127 [PIX_FMT_RGB565] = {
2128 .convert = gray_to_rgb565
2131 .convert = gray_to_rgb24
2134 .convert = gray_to_bgr24
2137 .convert = gray_to_rgb32
2139 [PIX_FMT_MONOWHITE] = {
2140 .convert = gray_to_monowhite
2142 [PIX_FMT_MONOBLACK] = {
2143 .convert = gray_to_monoblack
2145 [PIX_FMT_GRAY16LE] = {
2146 .convert = gray_to_gray16
2148 [PIX_FMT_GRAY16BE] = {
2149 .convert = gray_to_gray16
2152 [PIX_FMT_MONOWHITE] = {
2154 .convert = monowhite_to_gray
2157 [PIX_FMT_MONOBLACK] = {
2159 .convert = monoblack_to_gray
2163 [PIX_FMT_RGB555] = {
2164 .convert = pal8_to_rgb555
2166 [PIX_FMT_RGB565] = {
2167 .convert = pal8_to_rgb565
2170 .convert = pal8_to_bgr24
2173 .convert = pal8_to_rgb24
2176 .convert = pal8_to_rgb32
2179 [PIX_FMT_UYYVYY411] = {
2180 [PIX_FMT_YUV411P] = {
2181 .convert = uyyvyy411_to_yuv411p,
2187 int avpicture_alloc(AVPicture *picture,
2188 int pix_fmt, int width, int height)
2193 size = avpicture_get_size(pix_fmt, width, height);
2196 ptr = av_malloc(size);
2199 avpicture_fill(picture, ptr, pix_fmt, width, height);
2202 memset(picture, 0, sizeof(AVPicture));
2206 void avpicture_free(AVPicture *picture)
2208 av_free(picture->data[0]);
2211 /* return true if yuv planar */
2212 static inline int is_yuv_planar(const PixFmtInfo *ps)
2214 return (ps->color_type == FF_COLOR_YUV ||
2215 ps->color_type == FF_COLOR_YUV_JPEG) &&
2216 ps->pixel_type == FF_PIXEL_PLANAR;
2219 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2220 int pix_fmt, int top_band, int left_band)
2225 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2228 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2229 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2231 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2232 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2233 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2235 dst->linesize[0] = src->linesize[0];
2236 dst->linesize[1] = src->linesize[1];
2237 dst->linesize[2] = src->linesize[2];
2241 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2242 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2251 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2252 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2254 for (i = 0; i < 3; i++) {
2255 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2256 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2258 if (padtop || padleft) {
2259 memset(dst->data[i], color[i],
2260 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2263 if (padleft || padright) {
2264 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2265 (dst->linesize[i] - (padright >> x_shift));
2266 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2267 for (y = 0; y < yheight; y++) {
2268 memset(optr, color[i], (padleft + padright) >> x_shift);
2269 optr += dst->linesize[i];
2273 if (src) { /* first line */
2274 uint8_t *iptr = src->data[i];
2275 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2276 (padleft >> x_shift);
2277 memcpy(optr, iptr, src->linesize[i]);
2278 iptr += src->linesize[i];
2279 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2280 (dst->linesize[i] - (padright >> x_shift));
2281 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2282 for (y = 0; y < yheight; y++) {
2283 memset(optr, color[i], (padleft + padright) >> x_shift);
2284 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2286 iptr += src->linesize[i];
2287 optr += dst->linesize[i];
2291 if (padbottom || padright) {
2292 optr = dst->data[i] + dst->linesize[i] *
2293 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2294 memset(optr, color[i],dst->linesize[i] *
2295 (padbottom >> y_shift) + (padright >> x_shift));
2301 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2302 void img_copy(AVPicture *dst, const AVPicture *src,
2303 int pix_fmt, int width, int height)
2305 av_picture_copy(dst, src, pix_fmt, width, height);
2308 int img_crop(AVPicture *dst, const AVPicture *src,
2309 int pix_fmt, int top_band, int left_band)
2311 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2314 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2315 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2318 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2322 #ifndef CONFIG_SWSCALER
2323 /* XXX: always use linesize. Return -1 if not supported */
2324 int img_convert(AVPicture *dst, int dst_pix_fmt,
2325 const AVPicture *src, int src_pix_fmt,
2326 int src_width, int src_height)
2329 int i, ret, dst_width, dst_height, int_pix_fmt;
2330 const PixFmtInfo *src_pix, *dst_pix;
2331 const ConvertEntry *ce;
2332 AVPicture tmp1, *tmp = &tmp1;
2334 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2335 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2337 if (src_width <= 0 || src_height <= 0)
2345 dst_width = src_width;
2346 dst_height = src_height;
2348 dst_pix = &pix_fmt_info[dst_pix_fmt];
2349 src_pix = &pix_fmt_info[src_pix_fmt];
2350 if (src_pix_fmt == dst_pix_fmt) {
2351 /* no conversion needed: just copy */
2352 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2356 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2358 /* specific conversion routine */
2359 ce->convert(dst, src, dst_width, dst_height);
2364 if (is_yuv_planar(dst_pix) &&
2365 src_pix_fmt == PIX_FMT_GRAY8) {
2369 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2370 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2371 src->data[0], src->linesize[0],
2372 dst_width, dst_height);
2374 img_apply_table(dst->data[0], dst->linesize[0],
2375 src->data[0], src->linesize[0],
2376 dst_width, dst_height,
2379 /* fill U and V with 128 */
2382 w >>= dst_pix->x_chroma_shift;
2383 h >>= dst_pix->y_chroma_shift;
2384 for(i = 1; i <= 2; i++) {
2386 for(y = 0; y< h; y++) {
2388 d += dst->linesize[i];
2395 if (is_yuv_planar(src_pix) &&
2396 dst_pix_fmt == PIX_FMT_GRAY8) {
2397 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2398 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2399 src->data[0], src->linesize[0],
2400 dst_width, dst_height);
2402 img_apply_table(dst->data[0], dst->linesize[0],
2403 src->data[0], src->linesize[0],
2404 dst_width, dst_height,
2410 /* YUV to YUV planar */
2411 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2412 int x_shift, y_shift, w, h, xy_shift;
2413 void (*resize_func)(uint8_t *dst, int dst_wrap,
2414 const uint8_t *src, int src_wrap,
2415 int width, int height);
2417 /* compute chroma size of the smallest dimensions */
2420 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2421 w >>= dst_pix->x_chroma_shift;
2423 w >>= src_pix->x_chroma_shift;
2424 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2425 h >>= dst_pix->y_chroma_shift;
2427 h >>= src_pix->y_chroma_shift;
2429 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2430 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2431 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2432 /* there must be filters for conversion at least from and to
2436 resize_func = ff_img_copy_plane;
2439 resize_func = shrink21;
2442 resize_func = shrink41;
2445 resize_func = shrink12;
2448 resize_func = ff_shrink22;
2451 resize_func = ff_shrink44;
2454 resize_func = grow21;
2457 resize_func = grow41;
2460 resize_func = grow22;
2463 resize_func = grow44;
2466 resize_func = conv411;
2469 /* currently not handled */
2470 goto no_chroma_filter;
2473 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2474 src->data[0], src->linesize[0],
2475 dst_width, dst_height);
2477 for(i = 1;i <= 2; i++)
2478 resize_func(dst->data[i], dst->linesize[i],
2479 src->data[i], src->linesize[i],
2480 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2481 /* if yuv color space conversion is needed, we do it here on
2482 the destination image */
2483 if (dst_pix->color_type != src_pix->color_type) {
2484 const uint8_t *y_table, *c_table;
2485 if (dst_pix->color_type == FF_COLOR_YUV) {
2486 y_table = y_jpeg_to_ccir;
2487 c_table = c_jpeg_to_ccir;
2489 y_table = y_ccir_to_jpeg;
2490 c_table = c_ccir_to_jpeg;
2492 img_apply_table(dst->data[0], dst->linesize[0],
2493 dst->data[0], dst->linesize[0],
2494 dst_width, dst_height,
2497 for(i = 1;i <= 2; i++)
2498 img_apply_table(dst->data[i], dst->linesize[i],
2499 dst->data[i], dst->linesize[i],
2500 dst_width>>dst_pix->x_chroma_shift,
2501 dst_height>>dst_pix->y_chroma_shift,
2508 /* try to use an intermediate format */
2509 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2510 dst_pix_fmt == PIX_FMT_YUYV422) {
2511 /* specific case: convert to YUV422P first */
2512 int_pix_fmt = PIX_FMT_YUV422P;
2513 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2514 dst_pix_fmt == PIX_FMT_UYVY422) {
2515 /* specific case: convert to YUV422P first */
2516 int_pix_fmt = PIX_FMT_YUV422P;
2517 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2518 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2519 /* specific case: convert to YUV411P first */
2520 int_pix_fmt = PIX_FMT_YUV411P;
2521 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2522 src_pix_fmt != PIX_FMT_GRAY8) ||
2523 (dst_pix->color_type == FF_COLOR_GRAY &&
2524 dst_pix_fmt != PIX_FMT_GRAY8)) {
2525 /* gray8 is the normalized format */
2526 int_pix_fmt = PIX_FMT_GRAY8;
2527 } else if ((is_yuv_planar(src_pix) &&
2528 src_pix_fmt != PIX_FMT_YUV444P &&
2529 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2530 /* yuv444 is the normalized format */
2531 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2532 int_pix_fmt = PIX_FMT_YUVJ444P;
2534 int_pix_fmt = PIX_FMT_YUV444P;
2535 } else if ((is_yuv_planar(dst_pix) &&
2536 dst_pix_fmt != PIX_FMT_YUV444P &&
2537 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2538 /* yuv444 is the normalized format */
2539 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2540 int_pix_fmt = PIX_FMT_YUVJ444P;
2542 int_pix_fmt = PIX_FMT_YUV444P;
2544 /* the two formats are rgb or gray8 or yuv[j]444p */
2545 if (src_pix->is_alpha && dst_pix->is_alpha)
2546 int_pix_fmt = PIX_FMT_RGB32;
2548 int_pix_fmt = PIX_FMT_RGB24;
2550 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2553 if (img_convert(tmp, int_pix_fmt,
2554 src, src_pix_fmt, src_width, src_height) < 0)
2556 if (img_convert(dst, dst_pix_fmt,
2557 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2561 avpicture_free(tmp);
2566 /* NOTE: we scan all the pixels to have an exact information */
2567 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2569 const unsigned char *p;
2570 int src_wrap, ret, x, y;
2572 uint32_t *palette = (uint32_t *)src->data[1];
2575 src_wrap = src->linesize[0] - width;
2577 for(y=0;y<height;y++) {
2578 for(x=0;x<width;x++) {
2579 a = palette[p[0]] >> 24;
2581 ret |= FF_ALPHA_TRANSP;
2582 } else if (a != 0xff) {
2583 ret |= FF_ALPHA_SEMI_TRANSP;
2592 int img_get_alpha_info(const AVPicture *src,
2593 int pix_fmt, int width, int height)
2595 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2598 pf = &pix_fmt_info[pix_fmt];
2599 /* no alpha can be represented in format */
2604 ret = get_alpha_info_rgb32(src, width, height);
2607 ret = get_alpha_info_pal8(src, width, height);
2610 /* we do not know, so everything is indicated */
2611 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2618 #define DEINT_INPLACE_LINE_LUM \
2619 movd_m2r(lum_m4[0],mm0);\
2620 movd_m2r(lum_m3[0],mm1);\
2621 movd_m2r(lum_m2[0],mm2);\
2622 movd_m2r(lum_m1[0],mm3);\
2623 movd_m2r(lum[0],mm4);\
2624 punpcklbw_r2r(mm7,mm0);\
2625 movd_r2m(mm2,lum_m4[0]);\
2626 punpcklbw_r2r(mm7,mm1);\
2627 punpcklbw_r2r(mm7,mm2);\
2628 punpcklbw_r2r(mm7,mm3);\
2629 punpcklbw_r2r(mm7,mm4);\
2630 paddw_r2r(mm3,mm1);\
2632 paddw_r2r(mm4,mm0);\
2634 paddw_r2r(mm6,mm2);\
2635 paddw_r2r(mm2,mm1);\
2636 psubusw_r2r(mm0,mm1);\
2638 packuswb_r2r(mm7,mm1);\
2639 movd_r2m(mm1,lum_m2[0]);
2641 #define DEINT_LINE_LUM \
2642 movd_m2r(lum_m4[0],mm0);\
2643 movd_m2r(lum_m3[0],mm1);\
2644 movd_m2r(lum_m2[0],mm2);\
2645 movd_m2r(lum_m1[0],mm3);\
2646 movd_m2r(lum[0],mm4);\
2647 punpcklbw_r2r(mm7,mm0);\
2648 punpcklbw_r2r(mm7,mm1);\
2649 punpcklbw_r2r(mm7,mm2);\
2650 punpcklbw_r2r(mm7,mm3);\
2651 punpcklbw_r2r(mm7,mm4);\
2652 paddw_r2r(mm3,mm1);\
2654 paddw_r2r(mm4,mm0);\
2656 paddw_r2r(mm6,mm2);\
2657 paddw_r2r(mm2,mm1);\
2658 psubusw_r2r(mm0,mm1);\
2660 packuswb_r2r(mm7,mm1);\
2661 movd_r2m(mm1,dst[0]);
2664 /* filter parameters: [-1 4 2 4 -1] // 8 */
2665 static void deinterlace_line(uint8_t *dst,
2666 const uint8_t *lum_m4, const uint8_t *lum_m3,
2667 const uint8_t *lum_m2, const uint8_t *lum_m1,
2672 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2675 for(;size > 0;size--) {
2677 sum += lum_m3[0] << 2;
2678 sum += lum_m2[0] << 1;
2679 sum += lum_m1[0] << 2;
2681 dst[0] = cm[(sum + 4) >> 3];
2698 movq_m2r(rounder,mm6);
2700 for (;size > 3; size-=4) {
2711 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2715 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2718 for(;size > 0;size--) {
2720 sum += lum_m3[0] << 2;
2721 sum += lum_m2[0] << 1;
2722 lum_m4[0]=lum_m2[0];
2723 sum += lum_m1[0] << 2;
2725 lum_m2[0] = cm[(sum + 4) >> 3];
2741 movq_m2r(rounder,mm6);
2743 for (;size > 3; size-=4) {
2744 DEINT_INPLACE_LINE_LUM
2754 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2755 top field is copied as is, but the bottom field is deinterlaced
2756 against the top field. */
2757 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2758 const uint8_t *src1, int src_wrap,
2759 int width, int height)
2761 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2766 src_0=&src_m1[src_wrap];
2767 src_p1=&src_0[src_wrap];
2768 src_p2=&src_p1[src_wrap];
2769 for(y=0;y<(height-2);y+=2) {
2770 memcpy(dst,src_m1,width);
2772 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2776 src_p1 += 2*src_wrap;
2777 src_p2 += 2*src_wrap;
2780 memcpy(dst,src_m1,width);
2783 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2786 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2787 int width, int height)
2789 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2792 buf = (uint8_t*)av_malloc(width);
2795 memcpy(buf,src_m1,width);
2796 src_0=&src_m1[src_wrap];
2797 src_p1=&src_0[src_wrap];
2798 src_p2=&src_p1[src_wrap];
2799 for(y=0;y<(height-2);y+=2) {
2800 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2803 src_p1 += 2*src_wrap;
2804 src_p2 += 2*src_wrap;
2807 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2811 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2812 int pix_fmt, int width, int height)
2816 if (pix_fmt != PIX_FMT_YUV420P &&
2817 pix_fmt != PIX_FMT_YUV422P &&
2818 pix_fmt != PIX_FMT_YUV444P &&
2819 pix_fmt != PIX_FMT_YUV411P)
2821 if ((width & 3) != 0 || (height & 3) != 0)
2827 case PIX_FMT_YUV420P:
2831 case PIX_FMT_YUV422P:
2834 case PIX_FMT_YUV411P:
2842 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2845 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2846 src->data[i], src->linesize[i],