2 * Misc image conversion 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 conversion 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
35 #include "colorspace.h"
41 #define xglue(x, y) x ## y
42 #define glue(x, y) xglue(x, y)
44 #define FF_COLOR_RGB 0 /**< RGB color space */
45 #define FF_COLOR_GRAY 1 /**< gray color space */
46 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
47 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
50 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
51 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
53 typedef struct PixFmtInfo {
55 uint8_t nb_channels; /**< number of channels (including alpha) */
56 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
57 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
58 uint8_t is_alpha : 1; /**< true if alpha can be specified */
59 uint8_t x_chroma_shift; /**< X chroma subsampling factor is 2 ^ shift */
60 uint8_t y_chroma_shift; /**< Y chroma subsampling factor is 2 ^ shift */
61 uint8_t depth; /**< bit depth of the color components */
64 /* this table gives more information about formats */
65 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
70 .color_type = FF_COLOR_YUV,
71 .pixel_type = FF_PIXEL_PLANAR,
73 .x_chroma_shift = 1, .y_chroma_shift = 1,
78 .color_type = FF_COLOR_YUV,
79 .pixel_type = FF_PIXEL_PLANAR,
81 .x_chroma_shift = 1, .y_chroma_shift = 0,
86 .color_type = FF_COLOR_YUV,
87 .pixel_type = FF_PIXEL_PLANAR,
89 .x_chroma_shift = 0, .y_chroma_shift = 0,
94 .color_type = FF_COLOR_YUV,
95 .pixel_type = FF_PIXEL_PACKED,
97 .x_chroma_shift = 1, .y_chroma_shift = 0,
102 .color_type = FF_COLOR_YUV,
103 .pixel_type = FF_PIXEL_PACKED,
105 .x_chroma_shift = 1, .y_chroma_shift = 0,
107 [PIX_FMT_YUV410P] = {
110 .color_type = FF_COLOR_YUV,
111 .pixel_type = FF_PIXEL_PLANAR,
113 .x_chroma_shift = 2, .y_chroma_shift = 2,
115 [PIX_FMT_YUV411P] = {
118 .color_type = FF_COLOR_YUV,
119 .pixel_type = FF_PIXEL_PLANAR,
121 .x_chroma_shift = 2, .y_chroma_shift = 0,
123 [PIX_FMT_YUV440P] = {
126 .color_type = FF_COLOR_YUV,
127 .pixel_type = FF_PIXEL_PLANAR,
129 .x_chroma_shift = 0, .y_chroma_shift = 1,
133 [PIX_FMT_YUVJ420P] = {
136 .color_type = FF_COLOR_YUV_JPEG,
137 .pixel_type = FF_PIXEL_PLANAR,
139 .x_chroma_shift = 1, .y_chroma_shift = 1,
141 [PIX_FMT_YUVJ422P] = {
144 .color_type = FF_COLOR_YUV_JPEG,
145 .pixel_type = FF_PIXEL_PLANAR,
147 .x_chroma_shift = 1, .y_chroma_shift = 0,
149 [PIX_FMT_YUVJ444P] = {
152 .color_type = FF_COLOR_YUV_JPEG,
153 .pixel_type = FF_PIXEL_PLANAR,
155 .x_chroma_shift = 0, .y_chroma_shift = 0,
157 [PIX_FMT_YUVJ440P] = {
160 .color_type = FF_COLOR_YUV_JPEG,
161 .pixel_type = FF_PIXEL_PLANAR,
163 .x_chroma_shift = 0, .y_chroma_shift = 1,
170 .color_type = FF_COLOR_RGB,
171 .pixel_type = FF_PIXEL_PACKED,
173 .x_chroma_shift = 0, .y_chroma_shift = 0,
178 .color_type = FF_COLOR_RGB,
179 .pixel_type = FF_PIXEL_PACKED,
181 .x_chroma_shift = 0, .y_chroma_shift = 0,
185 .nb_channels = 4, .is_alpha = 1,
186 .color_type = FF_COLOR_RGB,
187 .pixel_type = FF_PIXEL_PACKED,
189 .x_chroma_shift = 0, .y_chroma_shift = 0,
194 .color_type = FF_COLOR_RGB,
195 .pixel_type = FF_PIXEL_PACKED,
197 .x_chroma_shift = 0, .y_chroma_shift = 0,
202 .color_type = FF_COLOR_RGB,
203 .pixel_type = FF_PIXEL_PACKED,
205 .x_chroma_shift = 0, .y_chroma_shift = 0,
208 /* gray / mono formats */
209 [PIX_FMT_GRAY16BE] = {
212 .color_type = FF_COLOR_GRAY,
213 .pixel_type = FF_PIXEL_PLANAR,
216 [PIX_FMT_GRAY16LE] = {
219 .color_type = FF_COLOR_GRAY,
220 .pixel_type = FF_PIXEL_PLANAR,
226 .color_type = FF_COLOR_GRAY,
227 .pixel_type = FF_PIXEL_PLANAR,
230 [PIX_FMT_MONOWHITE] = {
233 .color_type = FF_COLOR_GRAY,
234 .pixel_type = FF_PIXEL_PLANAR,
237 [PIX_FMT_MONOBLACK] = {
240 .color_type = FF_COLOR_GRAY,
241 .pixel_type = FF_PIXEL_PLANAR,
245 /* paletted formats */
248 .nb_channels = 4, .is_alpha = 1,
249 .color_type = FF_COLOR_RGB,
250 .pixel_type = FF_PIXEL_PALETTE,
253 [PIX_FMT_XVMC_MPEG2_MC] = {
256 [PIX_FMT_XVMC_MPEG2_IDCT] = {
259 [PIX_FMT_UYYVYY411] = {
262 .color_type = FF_COLOR_YUV,
263 .pixel_type = FF_PIXEL_PACKED,
265 .x_chroma_shift = 2, .y_chroma_shift = 0,
269 .nb_channels = 4, .is_alpha = 1,
270 .color_type = FF_COLOR_RGB,
271 .pixel_type = FF_PIXEL_PACKED,
273 .x_chroma_shift = 0, .y_chroma_shift = 0,
278 .color_type = FF_COLOR_RGB,
279 .pixel_type = FF_PIXEL_PACKED,
281 .x_chroma_shift = 0, .y_chroma_shift = 0,
286 .color_type = FF_COLOR_RGB,
287 .pixel_type = FF_PIXEL_PACKED,
289 .x_chroma_shift = 0, .y_chroma_shift = 0,
294 .color_type = FF_COLOR_RGB,
295 .pixel_type = FF_PIXEL_PACKED,
297 .x_chroma_shift = 0, .y_chroma_shift = 0,
302 .color_type = FF_COLOR_RGB,
303 .pixel_type = FF_PIXEL_PACKED,
305 .x_chroma_shift = 0, .y_chroma_shift = 0,
307 [PIX_FMT_RGB4_BYTE] = {
310 .color_type = FF_COLOR_RGB,
311 .pixel_type = FF_PIXEL_PACKED,
313 .x_chroma_shift = 0, .y_chroma_shift = 0,
318 .color_type = FF_COLOR_RGB,
319 .pixel_type = FF_PIXEL_PACKED,
321 .x_chroma_shift = 0, .y_chroma_shift = 0,
326 .color_type = FF_COLOR_RGB,
327 .pixel_type = FF_PIXEL_PACKED,
329 .x_chroma_shift = 0, .y_chroma_shift = 0,
331 [PIX_FMT_BGR4_BYTE] = {
334 .color_type = FF_COLOR_RGB,
335 .pixel_type = FF_PIXEL_PACKED,
337 .x_chroma_shift = 0, .y_chroma_shift = 0,
342 .color_type = FF_COLOR_YUV,
343 .pixel_type = FF_PIXEL_PLANAR,
345 .x_chroma_shift = 1, .y_chroma_shift = 1,
350 .color_type = FF_COLOR_YUV,
351 .pixel_type = FF_PIXEL_PLANAR,
353 .x_chroma_shift = 1, .y_chroma_shift = 1,
356 [PIX_FMT_BGR32_1] = {
358 .nb_channels = 4, .is_alpha = 1,
359 .color_type = FF_COLOR_RGB,
360 .pixel_type = FF_PIXEL_PACKED,
362 .x_chroma_shift = 0, .y_chroma_shift = 0,
364 [PIX_FMT_RGB32_1] = {
366 .nb_channels = 4, .is_alpha = 1,
367 .color_type = FF_COLOR_RGB,
368 .pixel_type = FF_PIXEL_PACKED,
370 .x_chroma_shift = 0, .y_chroma_shift = 0,
374 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
376 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
377 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
380 const char *avcodec_get_pix_fmt_name(int pix_fmt)
382 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
385 return pix_fmt_info[pix_fmt].name;
388 enum PixelFormat avcodec_get_pix_fmt(const char* name)
392 for (i=0; i < PIX_FMT_NB; i++)
393 if (!strcmp(pix_fmt_info[i].name, name))
398 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
400 PixFmtInfo info= pix_fmt_info[pix_fmt];
402 char is_alpha_char= info.is_alpha ? 'y' : 'n';
406 snprintf (buf, buf_size,
407 "name " " nb_channels" " depth" " is_alpha"
410 snprintf (buf, buf_size,
411 "%-10s" " %1d " " %2d " " %c ",
419 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
420 int pix_fmt, int width, int height)
422 int size, w2, h2, size2;
423 const PixFmtInfo *pinfo;
425 if(avcodec_check_dimensions(NULL, width, height))
428 pinfo = &pix_fmt_info[pix_fmt];
429 size = width * height;
431 case PIX_FMT_YUV420P:
432 case PIX_FMT_YUV422P:
433 case PIX_FMT_YUV444P:
434 case PIX_FMT_YUV410P:
435 case PIX_FMT_YUV411P:
436 case PIX_FMT_YUV440P:
437 case PIX_FMT_YUVJ420P:
438 case PIX_FMT_YUVJ422P:
439 case PIX_FMT_YUVJ444P:
440 case PIX_FMT_YUVJ440P:
441 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
442 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
444 picture->data[0] = ptr;
445 picture->data[1] = picture->data[0] + size;
446 picture->data[2] = picture->data[1] + size2;
447 picture->linesize[0] = width;
448 picture->linesize[1] = w2;
449 picture->linesize[2] = w2;
450 return size + 2 * size2;
453 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
454 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
456 picture->data[0] = ptr;
457 picture->data[1] = picture->data[0] + size;
458 picture->data[2] = NULL;
459 picture->linesize[0] = width;
460 picture->linesize[1] = w2;
461 picture->linesize[2] = 0;
462 return size + 2 * size2;
465 picture->data[0] = ptr;
466 picture->data[1] = NULL;
467 picture->data[2] = NULL;
468 picture->linesize[0] = width * 3;
472 case PIX_FMT_RGB32_1:
473 case PIX_FMT_BGR32_1:
474 picture->data[0] = ptr;
475 picture->data[1] = NULL;
476 picture->data[2] = NULL;
477 picture->linesize[0] = width * 4;
479 case PIX_FMT_GRAY16BE:
480 case PIX_FMT_GRAY16LE:
485 case PIX_FMT_YUYV422:
486 picture->data[0] = ptr;
487 picture->data[1] = NULL;
488 picture->data[2] = NULL;
489 picture->linesize[0] = width * 2;
491 case PIX_FMT_UYVY422:
492 picture->data[0] = ptr;
493 picture->data[1] = NULL;
494 picture->data[2] = NULL;
495 picture->linesize[0] = width * 2;
497 case PIX_FMT_UYYVYY411:
498 picture->data[0] = ptr;
499 picture->data[1] = NULL;
500 picture->data[2] = NULL;
501 picture->linesize[0] = width + width/2;
502 return size + size/2;
505 case PIX_FMT_RGB4_BYTE:
506 case PIX_FMT_BGR4_BYTE:
508 picture->data[0] = ptr;
509 picture->data[1] = NULL;
510 picture->data[2] = NULL;
511 picture->linesize[0] = width;
515 picture->data[0] = ptr;
516 picture->data[1] = NULL;
517 picture->data[2] = NULL;
518 picture->linesize[0] = width / 2;
520 case PIX_FMT_MONOWHITE:
521 case PIX_FMT_MONOBLACK:
522 picture->data[0] = ptr;
523 picture->data[1] = NULL;
524 picture->data[2] = NULL;
525 picture->linesize[0] = (width + 7) >> 3;
526 return picture->linesize[0] * height;
528 size2 = (size + 3) & ~3;
529 picture->data[0] = ptr;
530 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
531 picture->data[2] = NULL;
532 picture->linesize[0] = width;
533 picture->linesize[1] = 4;
534 return size2 + 256 * 4;
537 picture->data[0] = NULL;
538 picture->data[1] = NULL;
539 picture->data[2] = NULL;
540 picture->data[3] = NULL;
545 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
546 unsigned char *dest, int dest_size)
548 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
549 int i, j, w, h, data_planes;
550 const unsigned char* s;
551 int size = avpicture_get_size(pix_fmt, width, height);
553 if (size > dest_size || size < 0)
556 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
557 if (pix_fmt == PIX_FMT_YUYV422 ||
558 pix_fmt == PIX_FMT_UYVY422 ||
559 pix_fmt == PIX_FMT_BGR565 ||
560 pix_fmt == PIX_FMT_BGR555 ||
561 pix_fmt == PIX_FMT_RGB565 ||
562 pix_fmt == PIX_FMT_RGB555)
564 else if (pix_fmt == PIX_FMT_UYYVYY411)
566 else if (pix_fmt == PIX_FMT_PAL8)
569 w = width * (pf->depth * pf->nb_channels / 8);
574 data_planes = pf->nb_channels;
575 w = (width*pf->depth + 7)/8;
579 for (i=0; i<data_planes; i++) {
581 w = width >> pf->x_chroma_shift;
582 h = height >> pf->y_chroma_shift;
588 s += src->linesize[i];
592 if (pf->pixel_type == FF_PIXEL_PALETTE)
593 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
598 int avpicture_get_size(int pix_fmt, int width, int height)
600 AVPicture dummy_pict;
601 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
604 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
607 const PixFmtInfo *pf, *ps;
610 ps = &pix_fmt_info[src_pix_fmt];
611 pf = &pix_fmt_info[dst_pix_fmt];
615 pf = &pix_fmt_info[dst_pix_fmt];
616 if (pf->depth < ps->depth ||
617 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
618 loss |= FF_LOSS_DEPTH;
619 if (pf->x_chroma_shift > ps->x_chroma_shift ||
620 pf->y_chroma_shift > ps->y_chroma_shift)
621 loss |= FF_LOSS_RESOLUTION;
622 switch(pf->color_type) {
624 if (ps->color_type != FF_COLOR_RGB &&
625 ps->color_type != FF_COLOR_GRAY)
626 loss |= FF_LOSS_COLORSPACE;
629 if (ps->color_type != FF_COLOR_GRAY)
630 loss |= FF_LOSS_COLORSPACE;
633 if (ps->color_type != FF_COLOR_YUV)
634 loss |= FF_LOSS_COLORSPACE;
636 case FF_COLOR_YUV_JPEG:
637 if (ps->color_type != FF_COLOR_YUV_JPEG &&
638 ps->color_type != FF_COLOR_YUV &&
639 ps->color_type != FF_COLOR_GRAY)
640 loss |= FF_LOSS_COLORSPACE;
644 if (ps->color_type != pf->color_type)
645 loss |= FF_LOSS_COLORSPACE;
648 if (pf->color_type == FF_COLOR_GRAY &&
649 ps->color_type != FF_COLOR_GRAY)
650 loss |= FF_LOSS_CHROMA;
651 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
652 loss |= FF_LOSS_ALPHA;
653 if (pf->pixel_type == FF_PIXEL_PALETTE &&
654 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
655 loss |= FF_LOSS_COLORQUANT;
659 static int avg_bits_per_pixel(int pix_fmt)
662 const PixFmtInfo *pf;
664 pf = &pix_fmt_info[pix_fmt];
665 switch(pf->pixel_type) {
666 case FF_PIXEL_PACKED:
668 case PIX_FMT_YUYV422:
669 case PIX_FMT_UYVY422:
676 case PIX_FMT_UYYVYY411:
680 bits = pf->depth * pf->nb_channels;
684 case FF_PIXEL_PLANAR:
685 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
686 bits = pf->depth * pf->nb_channels;
688 bits = pf->depth + ((2 * pf->depth) >>
689 (pf->x_chroma_shift + pf->y_chroma_shift));
692 case FF_PIXEL_PALETTE:
702 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
707 int dist, i, loss, min_dist, dst_pix_fmt;
709 /* find exact color match with smallest size */
711 min_dist = 0x7fffffff;
712 for(i = 0;i < PIX_FMT_NB; i++) {
713 if (pix_fmt_mask & (1 << i)) {
714 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
716 dist = avg_bits_per_pixel(i);
717 if (dist < min_dist) {
727 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
728 int has_alpha, int *loss_ptr)
730 int dst_pix_fmt, loss_mask, i;
731 static const int loss_mask_order[] = {
732 ~0, /* no loss first */
735 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
741 /* try with successive loss */
744 loss_mask = loss_mask_order[i++];
745 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
746 has_alpha, loss_mask);
747 if (dst_pix_fmt >= 0)
755 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
759 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
760 const uint8_t *src, int src_wrap,
761 int width, int height)
765 for(;height > 0; height--) {
766 memcpy(dst, src, width);
772 void av_picture_copy(AVPicture *dst, const AVPicture *src,
773 int pix_fmt, int width, int height)
776 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
778 pf = &pix_fmt_info[pix_fmt];
779 switch(pf->pixel_type) {
780 case FF_PIXEL_PACKED:
782 case PIX_FMT_YUYV422:
783 case PIX_FMT_UYVY422:
790 case PIX_FMT_UYYVYY411:
794 bits = pf->depth * pf->nb_channels;
797 bwidth = (width * bits + 7) >> 3;
798 ff_img_copy_plane(dst->data[0], dst->linesize[0],
799 src->data[0], src->linesize[0],
802 case FF_PIXEL_PLANAR:
803 for(i = 0; i < pf->nb_channels; i++) {
807 if (i == 1 || i == 2) {
808 w >>= pf->x_chroma_shift;
809 h >>= pf->y_chroma_shift;
811 bwidth = (w * pf->depth + 7) >> 3;
812 ff_img_copy_plane(dst->data[i], dst->linesize[i],
813 src->data[i], src->linesize[i],
817 case FF_PIXEL_PALETTE:
818 ff_img_copy_plane(dst->data[0], dst->linesize[0],
819 src->data[0], src->linesize[0],
821 /* copy the palette */
822 ff_img_copy_plane(dst->data[1], dst->linesize[1],
823 src->data[1], src->linesize[1],
829 /* XXX: totally non optimized */
831 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
832 int width, int height)
834 const uint8_t *p, *p1;
835 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
843 for(;height >= 1; height -= 2) {
848 for(w = width; w >= 2; w -= 2) {
865 p1 += src->linesize[0];
866 lum1 += dst->linesize[0];
870 for(w = width; w >= 2; w -= 2) {
879 p1 += src->linesize[0];
880 lum1 += dst->linesize[0];
882 cb1 += dst->linesize[1];
883 cr1 += dst->linesize[2];
887 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
888 int width, int height)
890 const uint8_t *p, *p1;
891 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
900 for(;height >= 1; height -= 2) {
905 for(w = width; w >= 2; w -= 2) {
922 p1 += src->linesize[0];
923 lum1 += dst->linesize[0];
927 for(w = width; w >= 2; w -= 2) {
936 p1 += src->linesize[0];
937 lum1 += dst->linesize[0];
939 cb1 += dst->linesize[1];
940 cr1 += dst->linesize[2];
945 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
946 int width, int height)
948 const uint8_t *p, *p1;
949 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
956 for(;height > 0; height--) {
961 for(w = width; w >= 2; w -= 2) {
971 p1 += src->linesize[0];
972 lum1 += dst->linesize[0];
973 cb1 += dst->linesize[1];
974 cr1 += dst->linesize[2];
979 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
980 int width, int height)
982 const uint8_t *p, *p1;
983 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
990 for(;height > 0; height--) {
995 for(w = width; w >= 2; w -= 2) {
1005 p1 += src->linesize[0];
1006 lum1 += dst->linesize[0];
1007 cb1 += dst->linesize[1];
1008 cr1 += dst->linesize[2];
1012 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1013 int width, int height)
1016 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1020 lum1 = src->data[0];
1023 for(;height > 0; height--) {
1028 for(w = width; w >= 2; w -= 2) {
1038 p1 += dst->linesize[0];
1039 lum1 += src->linesize[0];
1040 cb1 += src->linesize[1];
1041 cr1 += src->linesize[2];
1045 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1046 int width, int height)
1049 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1053 lum1 = src->data[0];
1056 for(;height > 0; height--) {
1061 for(w = width; w >= 2; w -= 2) {
1071 p1 += dst->linesize[0];
1072 lum1 += src->linesize[0];
1073 cb1 += src->linesize[1];
1074 cr1 += src->linesize[2];
1078 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1079 int width, int height)
1081 const uint8_t *p, *p1;
1082 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1086 lum1 = dst->data[0];
1089 for(;height > 0; height--) {
1094 for(w = width; w >= 4; w -= 4) {
1106 p1 += src->linesize[0];
1107 lum1 += dst->linesize[0];
1108 cb1 += dst->linesize[1];
1109 cr1 += dst->linesize[2];
1114 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1115 int width, int height)
1118 uint8_t *line1, *line2, *linesrc = dst->data[0];
1119 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1120 uint8_t *cb1, *cb2 = src->data[1];
1121 uint8_t *cr1, *cr2 = src->data[2];
1123 for(h = height / 2; h--;) {
1125 line2 = linesrc + dst->linesize[0];
1128 lum2 = lumsrc + src->linesize[0];
1133 for(w = width / 2; w--;) {
1134 *line1++ = *lum1++; *line2++ = *lum2++;
1135 *line1++ = *line2++ = *cb1++;
1136 *line1++ = *lum1++; *line2++ = *lum2++;
1137 *line1++ = *line2++ = *cr1++;
1140 linesrc += dst->linesize[0] * 2;
1141 lumsrc += src->linesize[0] * 2;
1142 cb2 += src->linesize[1];
1143 cr2 += src->linesize[2];
1147 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1148 int width, int height)
1151 uint8_t *line1, *line2, *linesrc = dst->data[0];
1152 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1153 uint8_t *cb1, *cb2 = src->data[1];
1154 uint8_t *cr1, *cr2 = src->data[2];
1156 for(h = height / 2; h--;) {
1158 line2 = linesrc + dst->linesize[0];
1161 lum2 = lumsrc + src->linesize[0];
1166 for(w = width / 2; w--;) {
1167 *line1++ = *line2++ = *cb1++;
1168 *line1++ = *lum1++; *line2++ = *lum2++;
1169 *line1++ = *line2++ = *cr1++;
1170 *line1++ = *lum1++; *line2++ = *lum2++;
1173 linesrc += dst->linesize[0] * 2;
1174 lumsrc += src->linesize[0] * 2;
1175 cb2 += src->linesize[1];
1176 cr2 += src->linesize[2];
1180 static uint8_t y_ccir_to_jpeg[256];
1181 static uint8_t y_jpeg_to_ccir[256];
1182 static uint8_t c_ccir_to_jpeg[256];
1183 static uint8_t c_jpeg_to_ccir[256];
1185 /* init various conversion tables */
1186 static void img_convert_init(void)
1189 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1191 for(i = 0;i < 256; i++) {
1192 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1193 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1194 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1195 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1199 /* apply to each pixel the given table */
1200 static void img_apply_table(uint8_t *dst, int dst_wrap,
1201 const uint8_t *src, int src_wrap,
1202 int width, int height, const uint8_t *table1)
1207 const uint8_t *table;
1210 for(;height > 0; height--) {
1234 /* XXX: use generic filter ? */
1235 /* XXX: in most cases, the sampling position is incorrect */
1238 static void shrink41(uint8_t *dst, int dst_wrap,
1239 const uint8_t *src, int src_wrap,
1240 int width, int height)
1246 for(;height > 0; height--) {
1249 for(w = width;w > 0; w--) {
1250 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1260 static void shrink21(uint8_t *dst, int dst_wrap,
1261 const uint8_t *src, int src_wrap,
1262 int width, int height)
1268 for(;height > 0; height--) {
1271 for(w = width;w > 0; w--) {
1272 d[0] = (s[0] + s[1]) >> 1;
1282 static void shrink12(uint8_t *dst, int dst_wrap,
1283 const uint8_t *src, int src_wrap,
1284 int width, int height)
1288 const uint8_t *s1, *s2;
1290 for(;height > 0; height--) {
1294 for(w = width;w >= 4; w-=4) {
1295 d[0] = (s1[0] + s2[0]) >> 1;
1296 d[1] = (s1[1] + s2[1]) >> 1;
1297 d[2] = (s1[2] + s2[2]) >> 1;
1298 d[3] = (s1[3] + s2[3]) >> 1;
1304 d[0] = (s1[0] + s2[0]) >> 1;
1309 src += 2 * src_wrap;
1315 void ff_shrink22(uint8_t *dst, int dst_wrap,
1316 const uint8_t *src, int src_wrap,
1317 int width, int height)
1320 const uint8_t *s1, *s2;
1323 for(;height > 0; height--) {
1327 for(w = width;w >= 4; w-=4) {
1328 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1329 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1330 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1331 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1337 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1342 src += 2 * src_wrap;
1348 void ff_shrink44(uint8_t *dst, int dst_wrap,
1349 const uint8_t *src, int src_wrap,
1350 int width, int height)
1353 const uint8_t *s1, *s2, *s3, *s4;
1356 for(;height > 0; height--) {
1362 for(w = width;w > 0; w--) {
1363 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1364 s2[0] + s2[1] + s2[2] + s2[3] +
1365 s3[0] + s3[1] + s3[2] + s3[3] +
1366 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1373 src += 4 * src_wrap;
1379 void ff_shrink88(uint8_t *dst, int dst_wrap,
1380 const uint8_t *src, int src_wrap,
1381 int width, int height)
1385 for(;height > 0; height--) {
1386 for(w = width;w > 0; w--) {
1389 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1392 *(dst++) = (tmp + 32)>>6;
1393 src += 8 - 8*src_wrap;
1395 src += 8*src_wrap - 8*width;
1396 dst += dst_wrap - width;
1400 static void grow21_line(uint8_t *dst, const uint8_t *src,
1409 for(w = width;w >= 4; w-=4) {
1410 d[1] = d[0] = s1[0];
1411 d[3] = d[2] = s1[1];
1415 for(;w >= 2; w -= 2) {
1416 d[1] = d[0] = s1[0];
1420 /* only needed if width is not a multiple of two */
1421 /* XXX: veryfy that */
1427 static void grow41_line(uint8_t *dst, const uint8_t *src,
1436 for(w = width;w >= 4; w-=4) {
1448 static void grow21(uint8_t *dst, int dst_wrap,
1449 const uint8_t *src, int src_wrap,
1450 int width, int height)
1452 for(;height > 0; height--) {
1453 grow21_line(dst, src, width);
1460 static void grow12(uint8_t *dst, int dst_wrap,
1461 const uint8_t *src, int src_wrap,
1462 int width, int height)
1464 for(;height > 0; height-=2) {
1465 memcpy(dst, src, width);
1467 memcpy(dst, src, width);
1474 static void grow22(uint8_t *dst, int dst_wrap,
1475 const uint8_t *src, int src_wrap,
1476 int width, int height)
1478 for(;height > 0; height--) {
1479 grow21_line(dst, src, width);
1487 static void grow41(uint8_t *dst, int dst_wrap,
1488 const uint8_t *src, int src_wrap,
1489 int width, int height)
1491 for(;height > 0; height--) {
1492 grow41_line(dst, src, width);
1499 static void grow44(uint8_t *dst, int dst_wrap,
1500 const uint8_t *src, int src_wrap,
1501 int width, int height)
1503 for(;height > 0; height--) {
1504 grow41_line(dst, src, width);
1505 if ((height & 3) == 1)
1512 static void conv411(uint8_t *dst, int dst_wrap,
1513 const uint8_t *src, int src_wrap,
1514 int width, int height)
1517 const uint8_t *s1, *s2;
1522 for(;height > 0; height--) {
1524 s2 = src + src_wrap;
1526 for(w = width;w > 0; w--) {
1527 c = (s1[0] + s2[0]) >> 1;
1534 src += src_wrap * 2;
1539 /* XXX: add jpeg quantize code */
1541 #define TRANSP_INDEX (6*6*6)
1543 /* this is maybe slow, but allows for extensions */
1544 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1546 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1549 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1552 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1555 pal = (uint32_t *)palette;
1557 for(r = 0; r < 6; r++) {
1558 for(g = 0; g < 6; g++) {
1559 for(b = 0; b < 6; b++) {
1560 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1561 (pal_value[g] << 8) | pal_value[b];
1568 pal[i++] = 0xff000000;
1571 /* copy bit n to bits 0 ... n - 1 */
1572 static inline unsigned int bitcopy_n(unsigned int a, int n)
1575 mask = (1 << n) - 1;
1576 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1579 /* rgb555 handling */
1581 #define RGB_NAME rgb555
1583 #define RGB_IN(r, g, b, s)\
1585 unsigned int v = ((const uint16_t *)(s))[0];\
1586 r = bitcopy_n(v >> (10 - 3), 3);\
1587 g = bitcopy_n(v >> (5 - 3), 3);\
1588 b = bitcopy_n(v << 3, 3);\
1592 #define RGB_OUT(d, r, g, b)\
1594 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1599 #include "imgconvert_template.h"
1601 /* rgb565 handling */
1603 #define RGB_NAME rgb565
1605 #define RGB_IN(r, g, b, s)\
1607 unsigned int v = ((const uint16_t *)(s))[0];\
1608 r = bitcopy_n(v >> (11 - 3), 3);\
1609 g = bitcopy_n(v >> (5 - 2), 2);\
1610 b = bitcopy_n(v << 3, 3);\
1613 #define RGB_OUT(d, r, g, b)\
1615 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1620 #include "imgconvert_template.h"
1622 /* bgr24 handling */
1624 #define RGB_NAME bgr24
1626 #define RGB_IN(r, g, b, s)\
1633 #define RGB_OUT(d, r, g, b)\
1642 #include "imgconvert_template.h"
1648 /* rgb24 handling */
1650 #define RGB_NAME rgb24
1653 #define RGB_IN(r, g, b, s)\
1660 #define RGB_OUT(d, r, g, b)\
1669 #include "imgconvert_template.h"
1671 /* rgb32 handling */
1673 #define RGB_NAME rgb32
1676 #define RGB_IN(r, g, b, s)\
1678 unsigned int v = ((const uint32_t *)(s))[0];\
1679 r = (v >> 16) & 0xff;\
1680 g = (v >> 8) & 0xff;\
1684 #define RGBA_IN(r, g, b, a, s)\
1686 unsigned int v = ((const uint32_t *)(s))[0];\
1687 a = (v >> 24) & 0xff;\
1688 r = (v >> 16) & 0xff;\
1689 g = (v >> 8) & 0xff;\
1693 #define RGBA_OUT(d, r, g, b, a)\
1695 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1700 #include "imgconvert_template.h"
1702 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1703 int width, int height, int xor_mask)
1705 const unsigned char *p;
1707 int v, dst_wrap, src_wrap;
1711 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1714 dst_wrap = dst->linesize[0] - width;
1715 for(y=0;y<height;y++) {
1718 v = *p++ ^ xor_mask;
1720 q[1] = -((v >> 6) & 1);
1721 q[2] = -((v >> 5) & 1);
1722 q[3] = -((v >> 4) & 1);
1723 q[4] = -((v >> 3) & 1);
1724 q[5] = -((v >> 2) & 1);
1725 q[6] = -((v >> 1) & 1);
1726 q[7] = -((v >> 0) & 1);
1731 v = *p++ ^ xor_mask;
1733 q[0] = -((v >> 7) & 1);
1743 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1744 int width, int height)
1746 mono_to_gray(dst, src, width, height, 0xff);
1749 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1750 int width, int height)
1752 mono_to_gray(dst, src, width, height, 0x00);
1755 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1756 int width, int height, int xor_mask)
1761 int j, b, v, n1, src_wrap, dst_wrap, y;
1764 src_wrap = src->linesize[0] - width;
1767 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1769 for(y=0;y<height;y++) {
1776 v = (v << 1) | (b >> 7);
1778 d[0] = v ^ xor_mask;
1788 v = (v << 1) | (b >> 7);
1791 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1799 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1800 int width, int height)
1802 gray_to_mono(dst, src, width, height, 0xff);
1805 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1806 int width, int height)
1808 gray_to_mono(dst, src, width, height, 0x00);
1811 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1812 int width, int height)
1814 int x, y, src_wrap, dst_wrap;
1817 src_wrap = src->linesize[0] - width;
1819 dst_wrap = dst->linesize[0] - width * 2;
1820 for(y=0; y<height; y++){
1821 for(x=0; x<width; x++){
1830 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1831 int width, int height)
1833 int x, y, src_wrap, dst_wrap;
1836 src_wrap = src->linesize[0] - width * 2;
1838 dst_wrap = dst->linesize[0] - width;
1839 for(y=0; y<height; y++){
1840 for(x=0; x<width; x++){
1849 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1850 int width, int height)
1852 gray16_to_gray(dst, src, width, height);
1855 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1856 int width, int height)
1858 AVPicture tmpsrc = *src;
1860 gray16_to_gray(dst, &tmpsrc, width, height);
1863 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1864 int width, int height)
1866 int x, y, src_wrap, dst_wrap;
1869 src_wrap = (src->linesize[0] - width * 2)/2;
1871 dst_wrap = (dst->linesize[0] - width * 2)/2;
1872 for(y=0; y<height; y++){
1873 for(x=0; x<width; x++){
1874 *d++ = bswap_16(*s++);
1882 typedef struct ConvertEntry {
1883 void (*convert)(AVPicture *dst,
1884 const AVPicture *src, int width, int height);
1887 /* Add each new conversion function in this table. In order to be able
1888 to convert from any format to any format, the following constraints
1891 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1893 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1895 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1897 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1900 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1902 The other conversion functions are just optimisations for common cases.
1904 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1905 [PIX_FMT_YUV420P] = {
1906 [PIX_FMT_YUYV422] = {
1907 .convert = yuv420p_to_yuyv422,
1909 [PIX_FMT_RGB555] = {
1910 .convert = yuv420p_to_rgb555
1912 [PIX_FMT_RGB565] = {
1913 .convert = yuv420p_to_rgb565
1916 .convert = yuv420p_to_bgr24
1919 .convert = yuv420p_to_rgb24
1922 .convert = yuv420p_to_rgb32
1924 [PIX_FMT_UYVY422] = {
1925 .convert = yuv420p_to_uyvy422,
1928 [PIX_FMT_YUV422P] = {
1929 [PIX_FMT_YUYV422] = {
1930 .convert = yuv422p_to_yuyv422,
1932 [PIX_FMT_UYVY422] = {
1933 .convert = yuv422p_to_uyvy422,
1936 [PIX_FMT_YUV444P] = {
1938 .convert = yuv444p_to_rgb24
1941 [PIX_FMT_YUVJ420P] = {
1942 [PIX_FMT_RGB555] = {
1943 .convert = yuvj420p_to_rgb555
1945 [PIX_FMT_RGB565] = {
1946 .convert = yuvj420p_to_rgb565
1949 .convert = yuvj420p_to_bgr24
1952 .convert = yuvj420p_to_rgb24
1955 .convert = yuvj420p_to_rgb32
1958 [PIX_FMT_YUVJ444P] = {
1960 .convert = yuvj444p_to_rgb24
1963 [PIX_FMT_YUYV422] = {
1964 [PIX_FMT_YUV420P] = {
1965 .convert = yuyv422_to_yuv420p,
1967 [PIX_FMT_YUV422P] = {
1968 .convert = yuyv422_to_yuv422p,
1971 [PIX_FMT_UYVY422] = {
1972 [PIX_FMT_YUV420P] = {
1973 .convert = uyvy422_to_yuv420p,
1975 [PIX_FMT_YUV422P] = {
1976 .convert = uyvy422_to_yuv422p,
1980 [PIX_FMT_YUV420P] = {
1981 .convert = rgb24_to_yuv420p
1983 [PIX_FMT_RGB565] = {
1984 .convert = rgb24_to_rgb565
1986 [PIX_FMT_RGB555] = {
1987 .convert = rgb24_to_rgb555
1990 .convert = rgb24_to_rgb32
1993 .convert = rgb24_to_bgr24
1996 .convert = rgb24_to_gray
1999 .convert = rgb24_to_pal8
2001 [PIX_FMT_YUV444P] = {
2002 .convert = rgb24_to_yuv444p
2004 [PIX_FMT_YUVJ420P] = {
2005 .convert = rgb24_to_yuvj420p
2007 [PIX_FMT_YUVJ444P] = {
2008 .convert = rgb24_to_yuvj444p
2013 .convert = rgb32_to_rgb24
2016 .convert = rgb32_to_bgr24
2018 [PIX_FMT_RGB565] = {
2019 .convert = rgb32_to_rgb565
2021 [PIX_FMT_RGB555] = {
2022 .convert = rgb32_to_rgb555
2025 .convert = rgb32_to_pal8
2027 [PIX_FMT_YUV420P] = {
2028 .convert = rgb32_to_yuv420p
2031 .convert = rgb32_to_gray
2036 .convert = bgr24_to_rgb32
2039 .convert = bgr24_to_rgb24
2041 [PIX_FMT_YUV420P] = {
2042 .convert = bgr24_to_yuv420p
2045 .convert = bgr24_to_gray
2048 [PIX_FMT_RGB555] = {
2050 .convert = rgb555_to_rgb24
2053 .convert = rgb555_to_rgb32
2055 [PIX_FMT_YUV420P] = {
2056 .convert = rgb555_to_yuv420p
2059 .convert = rgb555_to_gray
2062 [PIX_FMT_RGB565] = {
2064 .convert = rgb565_to_rgb32
2067 .convert = rgb565_to_rgb24
2069 [PIX_FMT_YUV420P] = {
2070 .convert = rgb565_to_yuv420p
2073 .convert = rgb565_to_gray
2076 [PIX_FMT_GRAY16BE] = {
2078 .convert = gray16be_to_gray
2080 [PIX_FMT_GRAY16LE] = {
2081 .convert = gray16_to_gray16
2084 [PIX_FMT_GRAY16LE] = {
2086 .convert = gray16le_to_gray
2088 [PIX_FMT_GRAY16BE] = {
2089 .convert = gray16_to_gray16
2093 [PIX_FMT_RGB555] = {
2094 .convert = gray_to_rgb555
2096 [PIX_FMT_RGB565] = {
2097 .convert = gray_to_rgb565
2100 .convert = gray_to_rgb24
2103 .convert = gray_to_bgr24
2106 .convert = gray_to_rgb32
2108 [PIX_FMT_MONOWHITE] = {
2109 .convert = gray_to_monowhite
2111 [PIX_FMT_MONOBLACK] = {
2112 .convert = gray_to_monoblack
2114 [PIX_FMT_GRAY16LE] = {
2115 .convert = gray_to_gray16
2117 [PIX_FMT_GRAY16BE] = {
2118 .convert = gray_to_gray16
2121 [PIX_FMT_MONOWHITE] = {
2123 .convert = monowhite_to_gray
2126 [PIX_FMT_MONOBLACK] = {
2128 .convert = monoblack_to_gray
2132 [PIX_FMT_RGB555] = {
2133 .convert = pal8_to_rgb555
2135 [PIX_FMT_RGB565] = {
2136 .convert = pal8_to_rgb565
2139 .convert = pal8_to_bgr24
2142 .convert = pal8_to_rgb24
2145 .convert = pal8_to_rgb32
2148 [PIX_FMT_UYYVYY411] = {
2149 [PIX_FMT_YUV411P] = {
2150 .convert = uyyvyy411_to_yuv411p,
2156 int avpicture_alloc(AVPicture *picture,
2157 int pix_fmt, int width, int height)
2162 size = avpicture_get_size(pix_fmt, width, height);
2165 ptr = av_malloc(size);
2168 avpicture_fill(picture, ptr, pix_fmt, width, height);
2171 memset(picture, 0, sizeof(AVPicture));
2175 void avpicture_free(AVPicture *picture)
2177 av_free(picture->data[0]);
2180 /* return true if yuv planar */
2181 static inline int is_yuv_planar(const PixFmtInfo *ps)
2183 return (ps->color_type == FF_COLOR_YUV ||
2184 ps->color_type == FF_COLOR_YUV_JPEG) &&
2185 ps->pixel_type == FF_PIXEL_PLANAR;
2188 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2189 int pix_fmt, int top_band, int left_band)
2194 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2197 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2198 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2200 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2201 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2202 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2204 dst->linesize[0] = src->linesize[0];
2205 dst->linesize[1] = src->linesize[1];
2206 dst->linesize[2] = src->linesize[2];
2210 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2211 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2220 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2221 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2223 for (i = 0; i < 3; i++) {
2224 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2225 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2227 if (padtop || padleft) {
2228 memset(dst->data[i], color[i],
2229 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2232 if (padleft || padright) {
2233 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2234 (dst->linesize[i] - (padright >> x_shift));
2235 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2236 for (y = 0; y < yheight; y++) {
2237 memset(optr, color[i], (padleft + padright) >> x_shift);
2238 optr += dst->linesize[i];
2242 if (src) { /* first line */
2243 uint8_t *iptr = src->data[i];
2244 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2245 (padleft >> x_shift);
2246 memcpy(optr, iptr, src->linesize[i]);
2247 iptr += src->linesize[i];
2248 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2249 (dst->linesize[i] - (padright >> x_shift));
2250 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2251 for (y = 0; y < yheight; y++) {
2252 memset(optr, color[i], (padleft + padright) >> x_shift);
2253 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2255 iptr += src->linesize[i];
2256 optr += dst->linesize[i];
2260 if (padbottom || padright) {
2261 optr = dst->data[i] + dst->linesize[i] *
2262 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2263 memset(optr, color[i],dst->linesize[i] *
2264 (padbottom >> y_shift) + (padright >> x_shift));
2270 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2271 void img_copy(AVPicture *dst, const AVPicture *src,
2272 int pix_fmt, int width, int height)
2274 av_picture_copy(dst, src, pix_fmt, width, height);
2277 int img_crop(AVPicture *dst, const AVPicture *src,
2278 int pix_fmt, int top_band, int left_band)
2280 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2283 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2284 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2287 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2291 #ifndef CONFIG_SWSCALER
2292 /* XXX: always use linesize. Return -1 if not supported */
2293 int img_convert(AVPicture *dst, int dst_pix_fmt,
2294 const AVPicture *src, int src_pix_fmt,
2295 int src_width, int src_height)
2298 int i, ret, dst_width, dst_height, int_pix_fmt;
2299 const PixFmtInfo *src_pix, *dst_pix;
2300 const ConvertEntry *ce;
2301 AVPicture tmp1, *tmp = &tmp1;
2303 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2304 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2306 if (src_width <= 0 || src_height <= 0)
2314 dst_width = src_width;
2315 dst_height = src_height;
2317 dst_pix = &pix_fmt_info[dst_pix_fmt];
2318 src_pix = &pix_fmt_info[src_pix_fmt];
2319 if (src_pix_fmt == dst_pix_fmt) {
2320 /* no conversion needed: just copy */
2321 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2325 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2327 /* specific conversion routine */
2328 ce->convert(dst, src, dst_width, dst_height);
2333 if (is_yuv_planar(dst_pix) &&
2334 src_pix_fmt == PIX_FMT_GRAY8) {
2338 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2339 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2340 src->data[0], src->linesize[0],
2341 dst_width, dst_height);
2343 img_apply_table(dst->data[0], dst->linesize[0],
2344 src->data[0], src->linesize[0],
2345 dst_width, dst_height,
2348 /* fill U and V with 128 */
2351 w >>= dst_pix->x_chroma_shift;
2352 h >>= dst_pix->y_chroma_shift;
2353 for(i = 1; i <= 2; i++) {
2355 for(y = 0; y< h; y++) {
2357 d += dst->linesize[i];
2364 if (is_yuv_planar(src_pix) &&
2365 dst_pix_fmt == PIX_FMT_GRAY8) {
2366 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2367 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2368 src->data[0], src->linesize[0],
2369 dst_width, dst_height);
2371 img_apply_table(dst->data[0], dst->linesize[0],
2372 src->data[0], src->linesize[0],
2373 dst_width, dst_height,
2379 /* YUV to YUV planar */
2380 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2381 int x_shift, y_shift, w, h, xy_shift;
2382 void (*resize_func)(uint8_t *dst, int dst_wrap,
2383 const uint8_t *src, int src_wrap,
2384 int width, int height);
2386 /* compute chroma size of the smallest dimensions */
2389 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2390 w >>= dst_pix->x_chroma_shift;
2392 w >>= src_pix->x_chroma_shift;
2393 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2394 h >>= dst_pix->y_chroma_shift;
2396 h >>= src_pix->y_chroma_shift;
2398 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2399 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2400 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2401 /* there must be filters for conversion at least from and to
2405 resize_func = ff_img_copy_plane;
2408 resize_func = shrink21;
2411 resize_func = shrink41;
2414 resize_func = shrink12;
2417 resize_func = ff_shrink22;
2420 resize_func = ff_shrink44;
2423 resize_func = grow21;
2426 resize_func = grow12;
2429 resize_func = grow41;
2432 resize_func = grow22;
2435 resize_func = grow44;
2438 resize_func = conv411;
2441 /* currently not handled */
2442 goto no_chroma_filter;
2445 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2446 src->data[0], src->linesize[0],
2447 dst_width, dst_height);
2449 for(i = 1;i <= 2; i++)
2450 resize_func(dst->data[i], dst->linesize[i],
2451 src->data[i], src->linesize[i],
2452 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2453 /* if yuv color space conversion is needed, we do it here on
2454 the destination image */
2455 if (dst_pix->color_type != src_pix->color_type) {
2456 const uint8_t *y_table, *c_table;
2457 if (dst_pix->color_type == FF_COLOR_YUV) {
2458 y_table = y_jpeg_to_ccir;
2459 c_table = c_jpeg_to_ccir;
2461 y_table = y_ccir_to_jpeg;
2462 c_table = c_ccir_to_jpeg;
2464 img_apply_table(dst->data[0], dst->linesize[0],
2465 dst->data[0], dst->linesize[0],
2466 dst_width, dst_height,
2469 for(i = 1;i <= 2; i++)
2470 img_apply_table(dst->data[i], dst->linesize[i],
2471 dst->data[i], dst->linesize[i],
2472 dst_width>>dst_pix->x_chroma_shift,
2473 dst_height>>dst_pix->y_chroma_shift,
2480 /* try to use an intermediate format */
2481 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2482 dst_pix_fmt == PIX_FMT_YUYV422) {
2483 /* specific case: convert to YUV422P first */
2484 int_pix_fmt = PIX_FMT_YUV422P;
2485 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2486 dst_pix_fmt == PIX_FMT_UYVY422) {
2487 /* specific case: convert to YUV422P first */
2488 int_pix_fmt = PIX_FMT_YUV422P;
2489 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2490 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2491 /* specific case: convert to YUV411P first */
2492 int_pix_fmt = PIX_FMT_YUV411P;
2493 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2494 src_pix_fmt != PIX_FMT_GRAY8) ||
2495 (dst_pix->color_type == FF_COLOR_GRAY &&
2496 dst_pix_fmt != PIX_FMT_GRAY8)) {
2497 /* gray8 is the normalized format */
2498 int_pix_fmt = PIX_FMT_GRAY8;
2499 } else if ((is_yuv_planar(src_pix) &&
2500 src_pix_fmt != PIX_FMT_YUV444P &&
2501 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2502 /* yuv444 is the normalized format */
2503 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2504 int_pix_fmt = PIX_FMT_YUVJ444P;
2506 int_pix_fmt = PIX_FMT_YUV444P;
2507 } else if ((is_yuv_planar(dst_pix) &&
2508 dst_pix_fmt != PIX_FMT_YUV444P &&
2509 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2510 /* yuv444 is the normalized format */
2511 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2512 int_pix_fmt = PIX_FMT_YUVJ444P;
2514 int_pix_fmt = PIX_FMT_YUV444P;
2516 /* the two formats are rgb or gray8 or yuv[j]444p */
2517 if (src_pix->is_alpha && dst_pix->is_alpha)
2518 int_pix_fmt = PIX_FMT_RGB32;
2520 int_pix_fmt = PIX_FMT_RGB24;
2522 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2525 if (img_convert(tmp, int_pix_fmt,
2526 src, src_pix_fmt, src_width, src_height) < 0)
2528 if (img_convert(dst, dst_pix_fmt,
2529 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2533 avpicture_free(tmp);
2538 /* NOTE: we scan all the pixels to have an exact information */
2539 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2541 const unsigned char *p;
2542 int src_wrap, ret, x, y;
2544 uint32_t *palette = (uint32_t *)src->data[1];
2547 src_wrap = src->linesize[0] - width;
2549 for(y=0;y<height;y++) {
2550 for(x=0;x<width;x++) {
2551 a = palette[p[0]] >> 24;
2553 ret |= FF_ALPHA_TRANSP;
2554 } else if (a != 0xff) {
2555 ret |= FF_ALPHA_SEMI_TRANSP;
2564 int img_get_alpha_info(const AVPicture *src,
2565 int pix_fmt, int width, int height)
2567 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2570 pf = &pix_fmt_info[pix_fmt];
2571 /* no alpha can be represented in format */
2576 ret = get_alpha_info_rgb32(src, width, height);
2579 ret = get_alpha_info_pal8(src, width, height);
2582 /* we do not know, so everything is indicated */
2583 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2590 #define DEINT_INPLACE_LINE_LUM \
2591 movd_m2r(lum_m4[0],mm0);\
2592 movd_m2r(lum_m3[0],mm1);\
2593 movd_m2r(lum_m2[0],mm2);\
2594 movd_m2r(lum_m1[0],mm3);\
2595 movd_m2r(lum[0],mm4);\
2596 punpcklbw_r2r(mm7,mm0);\
2597 movd_r2m(mm2,lum_m4[0]);\
2598 punpcklbw_r2r(mm7,mm1);\
2599 punpcklbw_r2r(mm7,mm2);\
2600 punpcklbw_r2r(mm7,mm3);\
2601 punpcklbw_r2r(mm7,mm4);\
2602 paddw_r2r(mm3,mm1);\
2604 paddw_r2r(mm4,mm0);\
2606 paddw_r2r(mm6,mm2);\
2607 paddw_r2r(mm2,mm1);\
2608 psubusw_r2r(mm0,mm1);\
2610 packuswb_r2r(mm7,mm1);\
2611 movd_r2m(mm1,lum_m2[0]);
2613 #define DEINT_LINE_LUM \
2614 movd_m2r(lum_m4[0],mm0);\
2615 movd_m2r(lum_m3[0],mm1);\
2616 movd_m2r(lum_m2[0],mm2);\
2617 movd_m2r(lum_m1[0],mm3);\
2618 movd_m2r(lum[0],mm4);\
2619 punpcklbw_r2r(mm7,mm0);\
2620 punpcklbw_r2r(mm7,mm1);\
2621 punpcklbw_r2r(mm7,mm2);\
2622 punpcklbw_r2r(mm7,mm3);\
2623 punpcklbw_r2r(mm7,mm4);\
2624 paddw_r2r(mm3,mm1);\
2626 paddw_r2r(mm4,mm0);\
2628 paddw_r2r(mm6,mm2);\
2629 paddw_r2r(mm2,mm1);\
2630 psubusw_r2r(mm0,mm1);\
2632 packuswb_r2r(mm7,mm1);\
2633 movd_r2m(mm1,dst[0]);
2636 /* filter parameters: [-1 4 2 4 -1] // 8 */
2637 static void deinterlace_line(uint8_t *dst,
2638 const uint8_t *lum_m4, const uint8_t *lum_m3,
2639 const uint8_t *lum_m2, const uint8_t *lum_m1,
2644 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2647 for(;size > 0;size--) {
2649 sum += lum_m3[0] << 2;
2650 sum += lum_m2[0] << 1;
2651 sum += lum_m1[0] << 2;
2653 dst[0] = cm[(sum + 4) >> 3];
2670 movq_m2r(rounder,mm6);
2672 for (;size > 3; size-=4) {
2683 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2687 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2690 for(;size > 0;size--) {
2692 sum += lum_m3[0] << 2;
2693 sum += lum_m2[0] << 1;
2694 lum_m4[0]=lum_m2[0];
2695 sum += lum_m1[0] << 2;
2697 lum_m2[0] = cm[(sum + 4) >> 3];
2713 movq_m2r(rounder,mm6);
2715 for (;size > 3; size-=4) {
2716 DEINT_INPLACE_LINE_LUM
2726 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2727 top field is copied as is, but the bottom field is deinterlaced
2728 against the top field. */
2729 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2730 const uint8_t *src1, int src_wrap,
2731 int width, int height)
2733 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2738 src_0=&src_m1[src_wrap];
2739 src_p1=&src_0[src_wrap];
2740 src_p2=&src_p1[src_wrap];
2741 for(y=0;y<(height-2);y+=2) {
2742 memcpy(dst,src_m1,width);
2744 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2748 src_p1 += 2*src_wrap;
2749 src_p2 += 2*src_wrap;
2752 memcpy(dst,src_m1,width);
2755 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2758 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2759 int width, int height)
2761 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2764 buf = (uint8_t*)av_malloc(width);
2767 memcpy(buf,src_m1,width);
2768 src_0=&src_m1[src_wrap];
2769 src_p1=&src_0[src_wrap];
2770 src_p2=&src_p1[src_wrap];
2771 for(y=0;y<(height-2);y+=2) {
2772 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2775 src_p1 += 2*src_wrap;
2776 src_p2 += 2*src_wrap;
2779 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2783 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2784 int pix_fmt, int width, int height)
2788 if (pix_fmt != PIX_FMT_YUV420P &&
2789 pix_fmt != PIX_FMT_YUV422P &&
2790 pix_fmt != PIX_FMT_YUV444P &&
2791 pix_fmt != PIX_FMT_YUV411P)
2793 if ((width & 3) != 0 || (height & 3) != 0)
2799 case PIX_FMT_YUV420P:
2803 case PIX_FMT_YUV422P:
2806 case PIX_FMT_YUV411P:
2814 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2817 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2818 src->data[i], src->linesize[i],