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
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,
125 [PIX_FMT_YUVJ420P] = {
128 .color_type = FF_COLOR_YUV_JPEG,
129 .pixel_type = FF_PIXEL_PLANAR,
131 .x_chroma_shift = 1, .y_chroma_shift = 1,
133 [PIX_FMT_YUVJ422P] = {
136 .color_type = FF_COLOR_YUV_JPEG,
137 .pixel_type = FF_PIXEL_PLANAR,
139 .x_chroma_shift = 1, .y_chroma_shift = 0,
141 [PIX_FMT_YUVJ444P] = {
144 .color_type = FF_COLOR_YUV_JPEG,
145 .pixel_type = FF_PIXEL_PLANAR,
147 .x_chroma_shift = 0, .y_chroma_shift = 0,
154 .color_type = FF_COLOR_RGB,
155 .pixel_type = FF_PIXEL_PACKED,
157 .x_chroma_shift = 0, .y_chroma_shift = 0,
162 .color_type = FF_COLOR_RGB,
163 .pixel_type = FF_PIXEL_PACKED,
165 .x_chroma_shift = 0, .y_chroma_shift = 0,
169 .nb_channels = 4, .is_alpha = 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,
186 .color_type = FF_COLOR_RGB,
187 .pixel_type = FF_PIXEL_PACKED,
189 .x_chroma_shift = 0, .y_chroma_shift = 0,
192 /* gray / mono formats */
193 [PIX_FMT_GRAY16BE] = {
196 .color_type = FF_COLOR_GRAY,
197 .pixel_type = FF_PIXEL_PLANAR,
200 [PIX_FMT_GRAY16LE] = {
203 .color_type = FF_COLOR_GRAY,
204 .pixel_type = FF_PIXEL_PLANAR,
210 .color_type = FF_COLOR_GRAY,
211 .pixel_type = FF_PIXEL_PLANAR,
214 [PIX_FMT_MONOWHITE] = {
217 .color_type = FF_COLOR_GRAY,
218 .pixel_type = FF_PIXEL_PLANAR,
221 [PIX_FMT_MONOBLACK] = {
224 .color_type = FF_COLOR_GRAY,
225 .pixel_type = FF_PIXEL_PLANAR,
229 /* paletted formats */
232 .nb_channels = 4, .is_alpha = 1,
233 .color_type = FF_COLOR_RGB,
234 .pixel_type = FF_PIXEL_PALETTE,
237 [PIX_FMT_XVMC_MPEG2_MC] = {
240 [PIX_FMT_XVMC_MPEG2_IDCT] = {
243 [PIX_FMT_UYYVYY411] = {
246 .color_type = FF_COLOR_YUV,
247 .pixel_type = FF_PIXEL_PACKED,
249 .x_chroma_shift = 2, .y_chroma_shift = 0,
253 .nb_channels = 4, .is_alpha = 1,
254 .color_type = FF_COLOR_RGB,
255 .pixel_type = FF_PIXEL_PACKED,
257 .x_chroma_shift = 0, .y_chroma_shift = 0,
262 .color_type = FF_COLOR_RGB,
263 .pixel_type = FF_PIXEL_PACKED,
265 .x_chroma_shift = 0, .y_chroma_shift = 0,
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,
291 [PIX_FMT_RGB4_BYTE] = {
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,
310 .color_type = FF_COLOR_RGB,
311 .pixel_type = FF_PIXEL_PACKED,
313 .x_chroma_shift = 0, .y_chroma_shift = 0,
315 [PIX_FMT_BGR4_BYTE] = {
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_YUV,
327 .pixel_type = FF_PIXEL_PLANAR,
329 .x_chroma_shift = 1, .y_chroma_shift = 1,
334 .color_type = FF_COLOR_YUV,
335 .pixel_type = FF_PIXEL_PLANAR,
337 .x_chroma_shift = 1, .y_chroma_shift = 1,
340 [PIX_FMT_BGR32_1] = {
342 .nb_channels = 4, .is_alpha = 1,
343 .color_type = FF_COLOR_RGB,
344 .pixel_type = FF_PIXEL_PACKED,
346 .x_chroma_shift = 0, .y_chroma_shift = 0,
348 [PIX_FMT_RGB32_1] = {
350 .nb_channels = 4, .is_alpha = 1,
351 .color_type = FF_COLOR_RGB,
352 .pixel_type = FF_PIXEL_PACKED,
354 .x_chroma_shift = 0, .y_chroma_shift = 0,
358 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
360 *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
361 *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
364 const char *avcodec_get_pix_fmt_name(int pix_fmt)
366 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
369 return pix_fmt_info[pix_fmt].name;
372 enum PixelFormat avcodec_get_pix_fmt(const char* name)
376 for (i=0; i < PIX_FMT_NB; i++)
377 if (!strcmp(pix_fmt_info[i].name, name))
382 void avcodec_pix_fmt_string (char *buf, int buf_size, int pix_fmt)
384 PixFmtInfo info= pix_fmt_info[pix_fmt];
386 char is_alpha_char= info.is_alpha ? 'y' : 'n';
390 snprintf (buf, buf_size,
391 "name " " nb_channels" " depth" " is_alpha"
394 snprintf (buf, buf_size,
395 "%-10s" " %1d " " %2d " " %c ",
403 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
404 int pix_fmt, int width, int height)
406 int size, w2, h2, size2;
407 const PixFmtInfo *pinfo;
409 if(avcodec_check_dimensions(NULL, width, height))
412 pinfo = &pix_fmt_info[pix_fmt];
413 size = width * height;
415 case PIX_FMT_YUV420P:
416 case PIX_FMT_YUV422P:
417 case PIX_FMT_YUV444P:
418 case PIX_FMT_YUV410P:
419 case PIX_FMT_YUV411P:
420 case PIX_FMT_YUVJ420P:
421 case PIX_FMT_YUVJ422P:
422 case PIX_FMT_YUVJ444P:
423 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
424 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
426 picture->data[0] = ptr;
427 picture->data[1] = picture->data[0] + size;
428 picture->data[2] = picture->data[1] + size2;
429 picture->linesize[0] = width;
430 picture->linesize[1] = w2;
431 picture->linesize[2] = w2;
432 return size + 2 * size2;
435 w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
436 h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
438 picture->data[0] = ptr;
439 picture->data[1] = picture->data[0] + size;
440 picture->data[2] = NULL;
441 picture->linesize[0] = width;
442 picture->linesize[1] = w2;
443 picture->linesize[2] = 0;
444 return size + 2 * size2;
447 picture->data[0] = ptr;
448 picture->data[1] = NULL;
449 picture->data[2] = NULL;
450 picture->linesize[0] = width * 3;
454 case PIX_FMT_RGB32_1:
455 case PIX_FMT_BGR32_1:
456 picture->data[0] = ptr;
457 picture->data[1] = NULL;
458 picture->data[2] = NULL;
459 picture->linesize[0] = width * 4;
461 case PIX_FMT_GRAY16BE:
462 case PIX_FMT_GRAY16LE:
467 case PIX_FMT_YUYV422:
468 picture->data[0] = ptr;
469 picture->data[1] = NULL;
470 picture->data[2] = NULL;
471 picture->linesize[0] = width * 2;
473 case PIX_FMT_UYVY422:
474 picture->data[0] = ptr;
475 picture->data[1] = NULL;
476 picture->data[2] = NULL;
477 picture->linesize[0] = width * 2;
479 case PIX_FMT_UYYVYY411:
480 picture->data[0] = ptr;
481 picture->data[1] = NULL;
482 picture->data[2] = NULL;
483 picture->linesize[0] = width + width/2;
484 return size + size/2;
487 case PIX_FMT_RGB4_BYTE:
488 case PIX_FMT_BGR4_BYTE:
490 picture->data[0] = ptr;
491 picture->data[1] = NULL;
492 picture->data[2] = NULL;
493 picture->linesize[0] = width;
497 picture->data[0] = ptr;
498 picture->data[1] = NULL;
499 picture->data[2] = NULL;
500 picture->linesize[0] = width / 2;
502 case PIX_FMT_MONOWHITE:
503 case PIX_FMT_MONOBLACK:
504 picture->data[0] = ptr;
505 picture->data[1] = NULL;
506 picture->data[2] = NULL;
507 picture->linesize[0] = (width + 7) >> 3;
508 return picture->linesize[0] * height;
510 size2 = (size + 3) & ~3;
511 picture->data[0] = ptr;
512 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
513 picture->data[2] = NULL;
514 picture->linesize[0] = width;
515 picture->linesize[1] = 4;
516 return size2 + 256 * 4;
519 picture->data[0] = NULL;
520 picture->data[1] = NULL;
521 picture->data[2] = NULL;
522 picture->data[3] = NULL;
527 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
528 unsigned char *dest, int dest_size)
530 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
531 int i, j, w, h, data_planes;
532 const unsigned char* s;
533 int size = avpicture_get_size(pix_fmt, width, height);
535 if (size > dest_size || size < 0)
538 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
539 if (pix_fmt == PIX_FMT_YUYV422 ||
540 pix_fmt == PIX_FMT_UYVY422 ||
541 pix_fmt == PIX_FMT_BGR565 ||
542 pix_fmt == PIX_FMT_BGR555 ||
543 pix_fmt == PIX_FMT_RGB565 ||
544 pix_fmt == PIX_FMT_RGB555)
546 else if (pix_fmt == PIX_FMT_UYYVYY411)
548 else if (pix_fmt == PIX_FMT_PAL8)
551 w = width * (pf->depth * pf->nb_channels / 8);
556 data_planes = pf->nb_channels;
557 w = (width*pf->depth + 7)/8;
561 for (i=0; i<data_planes; i++) {
563 w = width >> pf->x_chroma_shift;
564 h = height >> pf->y_chroma_shift;
570 s += src->linesize[i];
574 if (pf->pixel_type == FF_PIXEL_PALETTE)
575 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
580 int avpicture_get_size(int pix_fmt, int width, int height)
582 AVPicture dummy_pict;
583 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
586 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
589 const PixFmtInfo *pf, *ps;
592 ps = &pix_fmt_info[src_pix_fmt];
593 pf = &pix_fmt_info[dst_pix_fmt];
597 pf = &pix_fmt_info[dst_pix_fmt];
598 if (pf->depth < ps->depth ||
599 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
600 loss |= FF_LOSS_DEPTH;
601 if (pf->x_chroma_shift > ps->x_chroma_shift ||
602 pf->y_chroma_shift > ps->y_chroma_shift)
603 loss |= FF_LOSS_RESOLUTION;
604 switch(pf->color_type) {
606 if (ps->color_type != FF_COLOR_RGB &&
607 ps->color_type != FF_COLOR_GRAY)
608 loss |= FF_LOSS_COLORSPACE;
611 if (ps->color_type != FF_COLOR_GRAY)
612 loss |= FF_LOSS_COLORSPACE;
615 if (ps->color_type != FF_COLOR_YUV)
616 loss |= FF_LOSS_COLORSPACE;
618 case FF_COLOR_YUV_JPEG:
619 if (ps->color_type != FF_COLOR_YUV_JPEG &&
620 ps->color_type != FF_COLOR_YUV &&
621 ps->color_type != FF_COLOR_GRAY)
622 loss |= FF_LOSS_COLORSPACE;
626 if (ps->color_type != pf->color_type)
627 loss |= FF_LOSS_COLORSPACE;
630 if (pf->color_type == FF_COLOR_GRAY &&
631 ps->color_type != FF_COLOR_GRAY)
632 loss |= FF_LOSS_CHROMA;
633 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
634 loss |= FF_LOSS_ALPHA;
635 if (pf->pixel_type == FF_PIXEL_PALETTE &&
636 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
637 loss |= FF_LOSS_COLORQUANT;
641 static int avg_bits_per_pixel(int pix_fmt)
644 const PixFmtInfo *pf;
646 pf = &pix_fmt_info[pix_fmt];
647 switch(pf->pixel_type) {
648 case FF_PIXEL_PACKED:
650 case PIX_FMT_YUYV422:
651 case PIX_FMT_UYVY422:
658 case PIX_FMT_UYYVYY411:
662 bits = pf->depth * pf->nb_channels;
666 case FF_PIXEL_PLANAR:
667 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
668 bits = pf->depth * pf->nb_channels;
670 bits = pf->depth + ((2 * pf->depth) >>
671 (pf->x_chroma_shift + pf->y_chroma_shift));
674 case FF_PIXEL_PALETTE:
684 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
689 int dist, i, loss, min_dist, dst_pix_fmt;
691 /* find exact color match with smallest size */
693 min_dist = 0x7fffffff;
694 for(i = 0;i < PIX_FMT_NB; i++) {
695 if (pix_fmt_mask & (1 << i)) {
696 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
698 dist = avg_bits_per_pixel(i);
699 if (dist < min_dist) {
709 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
710 int has_alpha, int *loss_ptr)
712 int dst_pix_fmt, loss_mask, i;
713 static const int loss_mask_order[] = {
714 ~0, /* no loss first */
717 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
723 /* try with successive loss */
726 loss_mask = loss_mask_order[i++];
727 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
728 has_alpha, loss_mask);
729 if (dst_pix_fmt >= 0)
737 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
741 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
742 const uint8_t *src, int src_wrap,
743 int width, int height)
747 for(;height > 0; height--) {
748 memcpy(dst, src, width);
754 void av_picture_copy(AVPicture *dst, const AVPicture *src,
755 int pix_fmt, int width, int height)
758 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
760 pf = &pix_fmt_info[pix_fmt];
761 switch(pf->pixel_type) {
762 case FF_PIXEL_PACKED:
764 case PIX_FMT_YUYV422:
765 case PIX_FMT_UYVY422:
772 case PIX_FMT_UYYVYY411:
776 bits = pf->depth * pf->nb_channels;
779 bwidth = (width * bits + 7) >> 3;
780 ff_img_copy_plane(dst->data[0], dst->linesize[0],
781 src->data[0], src->linesize[0],
784 case FF_PIXEL_PLANAR:
785 for(i = 0; i < pf->nb_channels; i++) {
789 if (i == 1 || i == 2) {
790 w >>= pf->x_chroma_shift;
791 h >>= pf->y_chroma_shift;
793 bwidth = (w * pf->depth + 7) >> 3;
794 ff_img_copy_plane(dst->data[i], dst->linesize[i],
795 src->data[i], src->linesize[i],
799 case FF_PIXEL_PALETTE:
800 ff_img_copy_plane(dst->data[0], dst->linesize[0],
801 src->data[0], src->linesize[0],
803 /* copy the palette */
804 ff_img_copy_plane(dst->data[1], dst->linesize[1],
805 src->data[1], src->linesize[1],
811 /* XXX: totally non optimized */
813 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
814 int width, int height)
816 const uint8_t *p, *p1;
817 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
825 for(;height >= 1; height -= 2) {
830 for(w = width; w >= 2; w -= 2) {
847 p1 += src->linesize[0];
848 lum1 += dst->linesize[0];
852 for(w = width; w >= 2; w -= 2) {
861 p1 += src->linesize[0];
862 lum1 += dst->linesize[0];
864 cb1 += dst->linesize[1];
865 cr1 += dst->linesize[2];
869 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
870 int width, int height)
872 const uint8_t *p, *p1;
873 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
882 for(;height >= 1; height -= 2) {
887 for(w = width; w >= 2; w -= 2) {
904 p1 += src->linesize[0];
905 lum1 += dst->linesize[0];
909 for(w = width; w >= 2; w -= 2) {
918 p1 += src->linesize[0];
919 lum1 += dst->linesize[0];
921 cb1 += dst->linesize[1];
922 cr1 += dst->linesize[2];
927 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
928 int width, int height)
930 const uint8_t *p, *p1;
931 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
938 for(;height > 0; height--) {
943 for(w = width; w >= 2; w -= 2) {
953 p1 += src->linesize[0];
954 lum1 += dst->linesize[0];
955 cb1 += dst->linesize[1];
956 cr1 += dst->linesize[2];
961 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
962 int width, int height)
964 const uint8_t *p, *p1;
965 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
972 for(;height > 0; height--) {
977 for(w = width; w >= 2; w -= 2) {
987 p1 += src->linesize[0];
988 lum1 += dst->linesize[0];
989 cb1 += dst->linesize[1];
990 cr1 += dst->linesize[2];
994 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
995 int width, int height)
998 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1002 lum1 = src->data[0];
1005 for(;height > 0; height--) {
1010 for(w = width; w >= 2; w -= 2) {
1020 p1 += dst->linesize[0];
1021 lum1 += src->linesize[0];
1022 cb1 += src->linesize[1];
1023 cr1 += src->linesize[2];
1027 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1028 int width, int height)
1031 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1035 lum1 = src->data[0];
1038 for(;height > 0; height--) {
1043 for(w = width; w >= 2; w -= 2) {
1053 p1 += dst->linesize[0];
1054 lum1 += src->linesize[0];
1055 cb1 += src->linesize[1];
1056 cr1 += src->linesize[2];
1060 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1061 int width, int height)
1063 const uint8_t *p, *p1;
1064 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1068 lum1 = dst->data[0];
1071 for(;height > 0; height--) {
1076 for(w = width; w >= 4; w -= 4) {
1088 p1 += src->linesize[0];
1089 lum1 += dst->linesize[0];
1090 cb1 += dst->linesize[1];
1091 cr1 += dst->linesize[2];
1096 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1097 int width, int height)
1100 uint8_t *line1, *line2, *linesrc = dst->data[0];
1101 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1102 uint8_t *cb1, *cb2 = src->data[1];
1103 uint8_t *cr1, *cr2 = src->data[2];
1105 for(h = height / 2; h--;) {
1107 line2 = linesrc + dst->linesize[0];
1110 lum2 = lumsrc + src->linesize[0];
1115 for(w = width / 2; w--;) {
1116 *line1++ = *lum1++; *line2++ = *lum2++;
1117 *line1++ = *line2++ = *cb1++;
1118 *line1++ = *lum1++; *line2++ = *lum2++;
1119 *line1++ = *line2++ = *cr1++;
1122 linesrc += dst->linesize[0] * 2;
1123 lumsrc += src->linesize[0] * 2;
1124 cb2 += src->linesize[1];
1125 cr2 += src->linesize[2];
1129 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1130 int width, int height)
1133 uint8_t *line1, *line2, *linesrc = dst->data[0];
1134 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1135 uint8_t *cb1, *cb2 = src->data[1];
1136 uint8_t *cr1, *cr2 = src->data[2];
1138 for(h = height / 2; h--;) {
1140 line2 = linesrc + dst->linesize[0];
1143 lum2 = lumsrc + src->linesize[0];
1148 for(w = width / 2; w--;) {
1149 *line1++ = *line2++ = *cb1++;
1150 *line1++ = *lum1++; *line2++ = *lum2++;
1151 *line1++ = *line2++ = *cr1++;
1152 *line1++ = *lum1++; *line2++ = *lum2++;
1155 linesrc += dst->linesize[0] * 2;
1156 lumsrc += src->linesize[0] * 2;
1157 cb2 += src->linesize[1];
1158 cr2 += src->linesize[2];
1162 static uint8_t y_ccir_to_jpeg[256];
1163 static uint8_t y_jpeg_to_ccir[256];
1164 static uint8_t c_ccir_to_jpeg[256];
1165 static uint8_t c_jpeg_to_ccir[256];
1167 /* init various conversion tables */
1168 static void img_convert_init(void)
1171 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1173 for(i = 0;i < 256; i++) {
1174 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1175 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1176 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1177 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1181 /* apply to each pixel the given table */
1182 static void img_apply_table(uint8_t *dst, int dst_wrap,
1183 const uint8_t *src, int src_wrap,
1184 int width, int height, const uint8_t *table1)
1189 const uint8_t *table;
1192 for(;height > 0; height--) {
1216 /* XXX: use generic filter ? */
1217 /* XXX: in most cases, the sampling position is incorrect */
1220 static void shrink41(uint8_t *dst, int dst_wrap,
1221 const uint8_t *src, int src_wrap,
1222 int width, int height)
1228 for(;height > 0; height--) {
1231 for(w = width;w > 0; w--) {
1232 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1242 static void shrink21(uint8_t *dst, int dst_wrap,
1243 const uint8_t *src, int src_wrap,
1244 int width, int height)
1250 for(;height > 0; height--) {
1253 for(w = width;w > 0; w--) {
1254 d[0] = (s[0] + s[1]) >> 1;
1264 static void shrink12(uint8_t *dst, int dst_wrap,
1265 const uint8_t *src, int src_wrap,
1266 int width, int height)
1270 const uint8_t *s1, *s2;
1272 for(;height > 0; height--) {
1276 for(w = width;w >= 4; w-=4) {
1277 d[0] = (s1[0] + s2[0]) >> 1;
1278 d[1] = (s1[1] + s2[1]) >> 1;
1279 d[2] = (s1[2] + s2[2]) >> 1;
1280 d[3] = (s1[3] + s2[3]) >> 1;
1286 d[0] = (s1[0] + s2[0]) >> 1;
1291 src += 2 * src_wrap;
1297 void ff_shrink22(uint8_t *dst, int dst_wrap,
1298 const uint8_t *src, int src_wrap,
1299 int width, int height)
1302 const uint8_t *s1, *s2;
1305 for(;height > 0; height--) {
1309 for(w = width;w >= 4; w-=4) {
1310 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1311 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1312 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1313 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1319 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1324 src += 2 * src_wrap;
1330 void ff_shrink44(uint8_t *dst, int dst_wrap,
1331 const uint8_t *src, int src_wrap,
1332 int width, int height)
1335 const uint8_t *s1, *s2, *s3, *s4;
1338 for(;height > 0; height--) {
1344 for(w = width;w > 0; w--) {
1345 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1346 s2[0] + s2[1] + s2[2] + s2[3] +
1347 s3[0] + s3[1] + s3[2] + s3[3] +
1348 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1355 src += 4 * src_wrap;
1361 void ff_shrink88(uint8_t *dst, int dst_wrap,
1362 const uint8_t *src, int src_wrap,
1363 int width, int height)
1367 for(;height > 0; height--) {
1368 for(w = width;w > 0; w--) {
1371 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1374 *(dst++) = (tmp + 32)>>6;
1375 src += 8 - 8*src_wrap;
1377 src += 8*src_wrap - 8*width;
1378 dst += dst_wrap - width;
1382 static void grow21_line(uint8_t *dst, const uint8_t *src,
1391 for(w = width;w >= 4; w-=4) {
1392 d[1] = d[0] = s1[0];
1393 d[3] = d[2] = s1[1];
1397 for(;w >= 2; w -= 2) {
1398 d[1] = d[0] = s1[0];
1402 /* only needed if width is not a multiple of two */
1403 /* XXX: veryfy that */
1409 static void grow41_line(uint8_t *dst, const uint8_t *src,
1418 for(w = width;w >= 4; w-=4) {
1430 static void grow21(uint8_t *dst, int dst_wrap,
1431 const uint8_t *src, int src_wrap,
1432 int width, int height)
1434 for(;height > 0; height--) {
1435 grow21_line(dst, src, width);
1442 static void grow22(uint8_t *dst, int dst_wrap,
1443 const uint8_t *src, int src_wrap,
1444 int width, int height)
1446 for(;height > 0; height--) {
1447 grow21_line(dst, src, width);
1455 static void grow41(uint8_t *dst, int dst_wrap,
1456 const uint8_t *src, int src_wrap,
1457 int width, int height)
1459 for(;height > 0; height--) {
1460 grow41_line(dst, src, width);
1467 static void grow44(uint8_t *dst, int dst_wrap,
1468 const uint8_t *src, int src_wrap,
1469 int width, int height)
1471 for(;height > 0; height--) {
1472 grow41_line(dst, src, width);
1473 if ((height & 3) == 1)
1480 static void conv411(uint8_t *dst, int dst_wrap,
1481 const uint8_t *src, int src_wrap,
1482 int width, int height)
1485 const uint8_t *s1, *s2;
1490 for(;height > 0; height--) {
1492 s2 = src + src_wrap;
1494 for(w = width;w > 0; w--) {
1495 c = (s1[0] + s2[0]) >> 1;
1502 src += src_wrap * 2;
1507 /* XXX: add jpeg quantize code */
1509 #define TRANSP_INDEX (6*6*6)
1511 /* this is maybe slow, but allows for extensions */
1512 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1514 return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1517 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1520 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1523 pal = (uint32_t *)palette;
1525 for(r = 0; r < 6; r++) {
1526 for(g = 0; g < 6; g++) {
1527 for(b = 0; b < 6; b++) {
1528 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1529 (pal_value[g] << 8) | pal_value[b];
1536 pal[i++] = 0xff000000;
1539 /* copy bit n to bits 0 ... n - 1 */
1540 static inline unsigned int bitcopy_n(unsigned int a, int n)
1543 mask = (1 << n) - 1;
1544 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1547 /* rgb555 handling */
1549 #define RGB_NAME rgb555
1551 #define RGB_IN(r, g, b, s)\
1553 unsigned int v = ((const uint16_t *)(s))[0];\
1554 r = bitcopy_n(v >> (10 - 3), 3);\
1555 g = bitcopy_n(v >> (5 - 3), 3);\
1556 b = bitcopy_n(v << 3, 3);\
1560 #define RGB_OUT(d, r, g, b)\
1562 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1567 #include "imgconvert_template.h"
1569 /* rgb565 handling */
1571 #define RGB_NAME rgb565
1573 #define RGB_IN(r, g, b, s)\
1575 unsigned int v = ((const uint16_t *)(s))[0];\
1576 r = bitcopy_n(v >> (11 - 3), 3);\
1577 g = bitcopy_n(v >> (5 - 2), 2);\
1578 b = bitcopy_n(v << 3, 3);\
1581 #define RGB_OUT(d, r, g, b)\
1583 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1588 #include "imgconvert_template.h"
1590 /* bgr24 handling */
1592 #define RGB_NAME bgr24
1594 #define RGB_IN(r, g, b, s)\
1601 #define RGB_OUT(d, r, g, b)\
1610 #include "imgconvert_template.h"
1616 /* rgb24 handling */
1618 #define RGB_NAME rgb24
1621 #define RGB_IN(r, g, b, s)\
1628 #define RGB_OUT(d, r, g, b)\
1637 #include "imgconvert_template.h"
1639 /* rgb32 handling */
1641 #define RGB_NAME rgb32
1644 #define RGB_IN(r, g, b, s)\
1646 unsigned int v = ((const uint32_t *)(s))[0];\
1647 r = (v >> 16) & 0xff;\
1648 g = (v >> 8) & 0xff;\
1652 #define RGBA_IN(r, g, b, a, s)\
1654 unsigned int v = ((const uint32_t *)(s))[0];\
1655 a = (v >> 24) & 0xff;\
1656 r = (v >> 16) & 0xff;\
1657 g = (v >> 8) & 0xff;\
1661 #define RGBA_OUT(d, r, g, b, a)\
1663 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1668 #include "imgconvert_template.h"
1670 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1671 int width, int height, int xor_mask)
1673 const unsigned char *p;
1675 int v, dst_wrap, src_wrap;
1679 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1682 dst_wrap = dst->linesize[0] - width;
1683 for(y=0;y<height;y++) {
1686 v = *p++ ^ xor_mask;
1688 q[1] = -((v >> 6) & 1);
1689 q[2] = -((v >> 5) & 1);
1690 q[3] = -((v >> 4) & 1);
1691 q[4] = -((v >> 3) & 1);
1692 q[5] = -((v >> 2) & 1);
1693 q[6] = -((v >> 1) & 1);
1694 q[7] = -((v >> 0) & 1);
1699 v = *p++ ^ xor_mask;
1701 q[0] = -((v >> 7) & 1);
1711 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1712 int width, int height)
1714 mono_to_gray(dst, src, width, height, 0xff);
1717 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1718 int width, int height)
1720 mono_to_gray(dst, src, width, height, 0x00);
1723 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1724 int width, int height, int xor_mask)
1729 int j, b, v, n1, src_wrap, dst_wrap, y;
1732 src_wrap = src->linesize[0] - width;
1735 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1737 for(y=0;y<height;y++) {
1744 v = (v << 1) | (b >> 7);
1746 d[0] = v ^ xor_mask;
1756 v = (v << 1) | (b >> 7);
1759 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1767 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1768 int width, int height)
1770 gray_to_mono(dst, src, width, height, 0xff);
1773 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1774 int width, int height)
1776 gray_to_mono(dst, src, width, height, 0x00);
1779 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1780 int width, int height)
1782 int x, y, src_wrap, dst_wrap;
1785 src_wrap = src->linesize[0] - width;
1787 dst_wrap = dst->linesize[0] - width * 2;
1788 for(y=0; y<height; y++){
1789 for(x=0; x<width; x++){
1798 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1799 int width, int height)
1801 int x, y, src_wrap, dst_wrap;
1804 src_wrap = src->linesize[0] - width * 2;
1806 dst_wrap = dst->linesize[0] - width;
1807 for(y=0; y<height; y++){
1808 for(x=0; x<width; x++){
1817 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1818 int width, int height)
1820 gray16_to_gray(dst, src, width, height);
1823 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1824 int width, int height)
1826 AVPicture tmpsrc = *src;
1828 gray16_to_gray(dst, &tmpsrc, width, height);
1831 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1832 int width, int height)
1834 int x, y, src_wrap, dst_wrap;
1837 src_wrap = (src->linesize[0] - width * 2)/2;
1839 dst_wrap = (dst->linesize[0] - width * 2)/2;
1840 for(y=0; y<height; y++){
1841 for(x=0; x<width; x++){
1842 *d++ = bswap_16(*s++);
1850 typedef struct ConvertEntry {
1851 void (*convert)(AVPicture *dst,
1852 const AVPicture *src, int width, int height);
1855 /* Add each new convertion function in this table. In order to be able
1856 to convert from any format to any format, the following constraints
1859 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1861 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1863 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1865 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1868 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1870 The other conversion functions are just optimisations for common cases.
1872 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1873 [PIX_FMT_YUV420P] = {
1874 [PIX_FMT_YUYV422] = {
1875 .convert = yuv420p_to_yuyv422,
1877 [PIX_FMT_RGB555] = {
1878 .convert = yuv420p_to_rgb555
1880 [PIX_FMT_RGB565] = {
1881 .convert = yuv420p_to_rgb565
1884 .convert = yuv420p_to_bgr24
1887 .convert = yuv420p_to_rgb24
1890 .convert = yuv420p_to_rgb32
1892 [PIX_FMT_UYVY422] = {
1893 .convert = yuv420p_to_uyvy422,
1896 [PIX_FMT_YUV422P] = {
1897 [PIX_FMT_YUYV422] = {
1898 .convert = yuv422p_to_yuyv422,
1900 [PIX_FMT_UYVY422] = {
1901 .convert = yuv422p_to_uyvy422,
1904 [PIX_FMT_YUV444P] = {
1906 .convert = yuv444p_to_rgb24
1909 [PIX_FMT_YUVJ420P] = {
1910 [PIX_FMT_RGB555] = {
1911 .convert = yuvj420p_to_rgb555
1913 [PIX_FMT_RGB565] = {
1914 .convert = yuvj420p_to_rgb565
1917 .convert = yuvj420p_to_bgr24
1920 .convert = yuvj420p_to_rgb24
1923 .convert = yuvj420p_to_rgb32
1926 [PIX_FMT_YUVJ444P] = {
1928 .convert = yuvj444p_to_rgb24
1931 [PIX_FMT_YUYV422] = {
1932 [PIX_FMT_YUV420P] = {
1933 .convert = yuyv422_to_yuv420p,
1935 [PIX_FMT_YUV422P] = {
1936 .convert = yuyv422_to_yuv422p,
1939 [PIX_FMT_UYVY422] = {
1940 [PIX_FMT_YUV420P] = {
1941 .convert = uyvy422_to_yuv420p,
1943 [PIX_FMT_YUV422P] = {
1944 .convert = uyvy422_to_yuv422p,
1948 [PIX_FMT_YUV420P] = {
1949 .convert = rgb24_to_yuv420p
1951 [PIX_FMT_RGB565] = {
1952 .convert = rgb24_to_rgb565
1954 [PIX_FMT_RGB555] = {
1955 .convert = rgb24_to_rgb555
1958 .convert = rgb24_to_rgb32
1961 .convert = rgb24_to_bgr24
1964 .convert = rgb24_to_gray
1967 .convert = rgb24_to_pal8
1969 [PIX_FMT_YUV444P] = {
1970 .convert = rgb24_to_yuv444p
1972 [PIX_FMT_YUVJ420P] = {
1973 .convert = rgb24_to_yuvj420p
1975 [PIX_FMT_YUVJ444P] = {
1976 .convert = rgb24_to_yuvj444p
1981 .convert = rgb32_to_rgb24
1984 .convert = rgb32_to_bgr24
1986 [PIX_FMT_RGB565] = {
1987 .convert = rgb32_to_rgb565
1989 [PIX_FMT_RGB555] = {
1990 .convert = rgb32_to_rgb555
1993 .convert = rgb32_to_pal8
1995 [PIX_FMT_YUV420P] = {
1996 .convert = rgb32_to_yuv420p
1999 .convert = rgb32_to_gray
2004 .convert = bgr24_to_rgb32
2007 .convert = bgr24_to_rgb24
2009 [PIX_FMT_YUV420P] = {
2010 .convert = bgr24_to_yuv420p
2013 .convert = bgr24_to_gray
2016 [PIX_FMT_RGB555] = {
2018 .convert = rgb555_to_rgb24
2021 .convert = rgb555_to_rgb32
2023 [PIX_FMT_YUV420P] = {
2024 .convert = rgb555_to_yuv420p
2027 .convert = rgb555_to_gray
2030 [PIX_FMT_RGB565] = {
2032 .convert = rgb565_to_rgb32
2035 .convert = rgb565_to_rgb24
2037 [PIX_FMT_YUV420P] = {
2038 .convert = rgb565_to_yuv420p
2041 .convert = rgb565_to_gray
2044 [PIX_FMT_GRAY16BE] = {
2046 .convert = gray16be_to_gray
2048 [PIX_FMT_GRAY16LE] = {
2049 .convert = gray16_to_gray16
2052 [PIX_FMT_GRAY16LE] = {
2054 .convert = gray16le_to_gray
2056 [PIX_FMT_GRAY16BE] = {
2057 .convert = gray16_to_gray16
2061 [PIX_FMT_RGB555] = {
2062 .convert = gray_to_rgb555
2064 [PIX_FMT_RGB565] = {
2065 .convert = gray_to_rgb565
2068 .convert = gray_to_rgb24
2071 .convert = gray_to_bgr24
2074 .convert = gray_to_rgb32
2076 [PIX_FMT_MONOWHITE] = {
2077 .convert = gray_to_monowhite
2079 [PIX_FMT_MONOBLACK] = {
2080 .convert = gray_to_monoblack
2082 [PIX_FMT_GRAY16LE] = {
2083 .convert = gray_to_gray16
2085 [PIX_FMT_GRAY16BE] = {
2086 .convert = gray_to_gray16
2089 [PIX_FMT_MONOWHITE] = {
2091 .convert = monowhite_to_gray
2094 [PIX_FMT_MONOBLACK] = {
2096 .convert = monoblack_to_gray
2100 [PIX_FMT_RGB555] = {
2101 .convert = pal8_to_rgb555
2103 [PIX_FMT_RGB565] = {
2104 .convert = pal8_to_rgb565
2107 .convert = pal8_to_bgr24
2110 .convert = pal8_to_rgb24
2113 .convert = pal8_to_rgb32
2116 [PIX_FMT_UYYVYY411] = {
2117 [PIX_FMT_YUV411P] = {
2118 .convert = uyyvyy411_to_yuv411p,
2124 int avpicture_alloc(AVPicture *picture,
2125 int pix_fmt, int width, int height)
2130 size = avpicture_get_size(pix_fmt, width, height);
2133 ptr = av_malloc(size);
2136 avpicture_fill(picture, ptr, pix_fmt, width, height);
2139 memset(picture, 0, sizeof(AVPicture));
2143 void avpicture_free(AVPicture *picture)
2145 av_free(picture->data[0]);
2148 /* return true if yuv planar */
2149 static inline int is_yuv_planar(const PixFmtInfo *ps)
2151 return (ps->color_type == FF_COLOR_YUV ||
2152 ps->color_type == FF_COLOR_YUV_JPEG) &&
2153 ps->pixel_type == FF_PIXEL_PLANAR;
2156 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2157 int pix_fmt, int top_band, int left_band)
2162 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2165 y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2166 x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2168 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2169 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2170 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2172 dst->linesize[0] = src->linesize[0];
2173 dst->linesize[1] = src->linesize[1];
2174 dst->linesize[2] = src->linesize[2];
2178 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2179 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2188 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2189 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2191 for (i = 0; i < 3; i++) {
2192 x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2193 y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2195 if (padtop || padleft) {
2196 memset(dst->data[i], color[i],
2197 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2200 if (padleft || padright) {
2201 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2202 (dst->linesize[i] - (padright >> x_shift));
2203 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2204 for (y = 0; y < yheight; y++) {
2205 memset(optr, color[i], (padleft + padright) >> x_shift);
2206 optr += dst->linesize[i];
2210 if (src) { /* first line */
2211 uint8_t *iptr = src->data[i];
2212 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2213 (padleft >> x_shift);
2214 memcpy(optr, iptr, src->linesize[i]);
2215 iptr += src->linesize[i];
2216 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2217 (dst->linesize[i] - (padright >> x_shift));
2218 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2219 for (y = 0; y < yheight; y++) {
2220 memset(optr, color[i], (padleft + padright) >> x_shift);
2221 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2223 iptr += src->linesize[i];
2224 optr += dst->linesize[i];
2228 if (padbottom || padright) {
2229 optr = dst->data[i] + dst->linesize[i] *
2230 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2231 memset(optr, color[i],dst->linesize[i] *
2232 (padbottom >> y_shift) + (padright >> x_shift));
2238 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2239 void img_copy(AVPicture *dst, const AVPicture *src,
2240 int pix_fmt, int width, int height)
2242 av_picture_copy(dst, src, pix_fmt, width, height);
2245 int img_crop(AVPicture *dst, const AVPicture *src,
2246 int pix_fmt, int top_band, int left_band)
2248 return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2251 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2252 int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2255 return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2259 #ifndef CONFIG_SWSCALER
2260 /* XXX: always use linesize. Return -1 if not supported */
2261 int img_convert(AVPicture *dst, int dst_pix_fmt,
2262 const AVPicture *src, int src_pix_fmt,
2263 int src_width, int src_height)
2266 int i, ret, dst_width, dst_height, int_pix_fmt;
2267 const PixFmtInfo *src_pix, *dst_pix;
2268 const ConvertEntry *ce;
2269 AVPicture tmp1, *tmp = &tmp1;
2271 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2272 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2274 if (src_width <= 0 || src_height <= 0)
2282 dst_width = src_width;
2283 dst_height = src_height;
2285 dst_pix = &pix_fmt_info[dst_pix_fmt];
2286 src_pix = &pix_fmt_info[src_pix_fmt];
2287 if (src_pix_fmt == dst_pix_fmt) {
2288 /* no conversion needed: just copy */
2289 av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2293 ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2295 /* specific conversion routine */
2296 ce->convert(dst, src, dst_width, dst_height);
2301 if (is_yuv_planar(dst_pix) &&
2302 src_pix_fmt == PIX_FMT_GRAY8) {
2306 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2307 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2308 src->data[0], src->linesize[0],
2309 dst_width, dst_height);
2311 img_apply_table(dst->data[0], dst->linesize[0],
2312 src->data[0], src->linesize[0],
2313 dst_width, dst_height,
2316 /* fill U and V with 128 */
2319 w >>= dst_pix->x_chroma_shift;
2320 h >>= dst_pix->y_chroma_shift;
2321 for(i = 1; i <= 2; i++) {
2323 for(y = 0; y< h; y++) {
2325 d += dst->linesize[i];
2332 if (is_yuv_planar(src_pix) &&
2333 dst_pix_fmt == PIX_FMT_GRAY8) {
2334 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2335 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2336 src->data[0], src->linesize[0],
2337 dst_width, dst_height);
2339 img_apply_table(dst->data[0], dst->linesize[0],
2340 src->data[0], src->linesize[0],
2341 dst_width, dst_height,
2347 /* YUV to YUV planar */
2348 if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2349 int x_shift, y_shift, w, h, xy_shift;
2350 void (*resize_func)(uint8_t *dst, int dst_wrap,
2351 const uint8_t *src, int src_wrap,
2352 int width, int height);
2354 /* compute chroma size of the smallest dimensions */
2357 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2358 w >>= dst_pix->x_chroma_shift;
2360 w >>= src_pix->x_chroma_shift;
2361 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2362 h >>= dst_pix->y_chroma_shift;
2364 h >>= src_pix->y_chroma_shift;
2366 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2367 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2368 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2369 /* there must be filters for conversion at least from and to
2373 resize_func = ff_img_copy_plane;
2376 resize_func = shrink21;
2379 resize_func = shrink41;
2382 resize_func = shrink12;
2385 resize_func = ff_shrink22;
2388 resize_func = ff_shrink44;
2391 resize_func = grow21;
2394 resize_func = grow41;
2397 resize_func = grow22;
2400 resize_func = grow44;
2403 resize_func = conv411;
2406 /* currently not handled */
2407 goto no_chroma_filter;
2410 ff_img_copy_plane(dst->data[0], dst->linesize[0],
2411 src->data[0], src->linesize[0],
2412 dst_width, dst_height);
2414 for(i = 1;i <= 2; i++)
2415 resize_func(dst->data[i], dst->linesize[i],
2416 src->data[i], src->linesize[i],
2417 dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2418 /* if yuv color space conversion is needed, we do it here on
2419 the destination image */
2420 if (dst_pix->color_type != src_pix->color_type) {
2421 const uint8_t *y_table, *c_table;
2422 if (dst_pix->color_type == FF_COLOR_YUV) {
2423 y_table = y_jpeg_to_ccir;
2424 c_table = c_jpeg_to_ccir;
2426 y_table = y_ccir_to_jpeg;
2427 c_table = c_ccir_to_jpeg;
2429 img_apply_table(dst->data[0], dst->linesize[0],
2430 dst->data[0], dst->linesize[0],
2431 dst_width, dst_height,
2434 for(i = 1;i <= 2; i++)
2435 img_apply_table(dst->data[i], dst->linesize[i],
2436 dst->data[i], dst->linesize[i],
2437 dst_width>>dst_pix->x_chroma_shift,
2438 dst_height>>dst_pix->y_chroma_shift,
2445 /* try to use an intermediate format */
2446 if (src_pix_fmt == PIX_FMT_YUYV422 ||
2447 dst_pix_fmt == PIX_FMT_YUYV422) {
2448 /* specific case: convert to YUV422P first */
2449 int_pix_fmt = PIX_FMT_YUV422P;
2450 } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2451 dst_pix_fmt == PIX_FMT_UYVY422) {
2452 /* specific case: convert to YUV422P first */
2453 int_pix_fmt = PIX_FMT_YUV422P;
2454 } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2455 dst_pix_fmt == PIX_FMT_UYYVYY411) {
2456 /* specific case: convert to YUV411P first */
2457 int_pix_fmt = PIX_FMT_YUV411P;
2458 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2459 src_pix_fmt != PIX_FMT_GRAY8) ||
2460 (dst_pix->color_type == FF_COLOR_GRAY &&
2461 dst_pix_fmt != PIX_FMT_GRAY8)) {
2462 /* gray8 is the normalized format */
2463 int_pix_fmt = PIX_FMT_GRAY8;
2464 } else if ((is_yuv_planar(src_pix) &&
2465 src_pix_fmt != PIX_FMT_YUV444P &&
2466 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2467 /* yuv444 is the normalized format */
2468 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2469 int_pix_fmt = PIX_FMT_YUVJ444P;
2471 int_pix_fmt = PIX_FMT_YUV444P;
2472 } else if ((is_yuv_planar(dst_pix) &&
2473 dst_pix_fmt != PIX_FMT_YUV444P &&
2474 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2475 /* yuv444 is the normalized format */
2476 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2477 int_pix_fmt = PIX_FMT_YUVJ444P;
2479 int_pix_fmt = PIX_FMT_YUV444P;
2481 /* the two formats are rgb or gray8 or yuv[j]444p */
2482 if (src_pix->is_alpha && dst_pix->is_alpha)
2483 int_pix_fmt = PIX_FMT_RGB32;
2485 int_pix_fmt = PIX_FMT_RGB24;
2487 if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2490 if (img_convert(tmp, int_pix_fmt,
2491 src, src_pix_fmt, src_width, src_height) < 0)
2493 if (img_convert(dst, dst_pix_fmt,
2494 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2498 avpicture_free(tmp);
2503 /* NOTE: we scan all the pixels to have an exact information */
2504 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2506 const unsigned char *p;
2507 int src_wrap, ret, x, y;
2509 uint32_t *palette = (uint32_t *)src->data[1];
2512 src_wrap = src->linesize[0] - width;
2514 for(y=0;y<height;y++) {
2515 for(x=0;x<width;x++) {
2516 a = palette[p[0]] >> 24;
2518 ret |= FF_ALPHA_TRANSP;
2519 } else if (a != 0xff) {
2520 ret |= FF_ALPHA_SEMI_TRANSP;
2529 int img_get_alpha_info(const AVPicture *src,
2530 int pix_fmt, int width, int height)
2532 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2535 pf = &pix_fmt_info[pix_fmt];
2536 /* no alpha can be represented in format */
2541 ret = get_alpha_info_rgb32(src, width, height);
2544 ret = get_alpha_info_pal8(src, width, height);
2547 /* we do not know, so everything is indicated */
2548 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2555 #define DEINT_INPLACE_LINE_LUM \
2556 movd_m2r(lum_m4[0],mm0);\
2557 movd_m2r(lum_m3[0],mm1);\
2558 movd_m2r(lum_m2[0],mm2);\
2559 movd_m2r(lum_m1[0],mm3);\
2560 movd_m2r(lum[0],mm4);\
2561 punpcklbw_r2r(mm7,mm0);\
2562 movd_r2m(mm2,lum_m4[0]);\
2563 punpcklbw_r2r(mm7,mm1);\
2564 punpcklbw_r2r(mm7,mm2);\
2565 punpcklbw_r2r(mm7,mm3);\
2566 punpcklbw_r2r(mm7,mm4);\
2567 paddw_r2r(mm3,mm1);\
2569 paddw_r2r(mm4,mm0);\
2571 paddw_r2r(mm6,mm2);\
2572 paddw_r2r(mm2,mm1);\
2573 psubusw_r2r(mm0,mm1);\
2575 packuswb_r2r(mm7,mm1);\
2576 movd_r2m(mm1,lum_m2[0]);
2578 #define DEINT_LINE_LUM \
2579 movd_m2r(lum_m4[0],mm0);\
2580 movd_m2r(lum_m3[0],mm1);\
2581 movd_m2r(lum_m2[0],mm2);\
2582 movd_m2r(lum_m1[0],mm3);\
2583 movd_m2r(lum[0],mm4);\
2584 punpcklbw_r2r(mm7,mm0);\
2585 punpcklbw_r2r(mm7,mm1);\
2586 punpcklbw_r2r(mm7,mm2);\
2587 punpcklbw_r2r(mm7,mm3);\
2588 punpcklbw_r2r(mm7,mm4);\
2589 paddw_r2r(mm3,mm1);\
2591 paddw_r2r(mm4,mm0);\
2593 paddw_r2r(mm6,mm2);\
2594 paddw_r2r(mm2,mm1);\
2595 psubusw_r2r(mm0,mm1);\
2597 packuswb_r2r(mm7,mm1);\
2598 movd_r2m(mm1,dst[0]);
2601 /* filter parameters: [-1 4 2 4 -1] // 8 */
2602 static void deinterlace_line(uint8_t *dst,
2603 const uint8_t *lum_m4, const uint8_t *lum_m3,
2604 const uint8_t *lum_m2, const uint8_t *lum_m1,
2609 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2612 for(;size > 0;size--) {
2614 sum += lum_m3[0] << 2;
2615 sum += lum_m2[0] << 1;
2616 sum += lum_m1[0] << 2;
2618 dst[0] = cm[(sum + 4) >> 3];
2635 movq_m2r(rounder,mm6);
2637 for (;size > 3; size-=4) {
2648 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2652 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2655 for(;size > 0;size--) {
2657 sum += lum_m3[0] << 2;
2658 sum += lum_m2[0] << 1;
2659 lum_m4[0]=lum_m2[0];
2660 sum += lum_m1[0] << 2;
2662 lum_m2[0] = cm[(sum + 4) >> 3];
2678 movq_m2r(rounder,mm6);
2680 for (;size > 3; size-=4) {
2681 DEINT_INPLACE_LINE_LUM
2691 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2692 top field is copied as is, but the bottom field is deinterlaced
2693 against the top field. */
2694 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2695 const uint8_t *src1, int src_wrap,
2696 int width, int height)
2698 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2703 src_0=&src_m1[src_wrap];
2704 src_p1=&src_0[src_wrap];
2705 src_p2=&src_p1[src_wrap];
2706 for(y=0;y<(height-2);y+=2) {
2707 memcpy(dst,src_m1,width);
2709 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2713 src_p1 += 2*src_wrap;
2714 src_p2 += 2*src_wrap;
2717 memcpy(dst,src_m1,width);
2720 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2723 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2724 int width, int height)
2726 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2729 buf = (uint8_t*)av_malloc(width);
2732 memcpy(buf,src_m1,width);
2733 src_0=&src_m1[src_wrap];
2734 src_p1=&src_0[src_wrap];
2735 src_p2=&src_p1[src_wrap];
2736 for(y=0;y<(height-2);y+=2) {
2737 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2740 src_p1 += 2*src_wrap;
2741 src_p2 += 2*src_wrap;
2744 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2748 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2749 int pix_fmt, int width, int height)
2753 if (pix_fmt != PIX_FMT_YUV420P &&
2754 pix_fmt != PIX_FMT_YUV422P &&
2755 pix_fmt != PIX_FMT_YUV444P &&
2756 pix_fmt != PIX_FMT_YUV411P)
2758 if ((width & 3) != 0 || (height & 3) != 0)
2764 case PIX_FMT_YUV420P:
2768 case PIX_FMT_YUV422P:
2771 case PIX_FMT_YUV411P:
2779 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2782 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2783 src->data[i], src->linesize[i],