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
23 * @file libavcodec/imgconvert.c
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"
37 #include "imgconvert.h"
38 #include "libavutil/pixdesc.h"
42 #include "x86/dsputil_mmx.h"
45 #define xglue(x, y) x ## y
46 #define glue(x, y) xglue(x, y)
48 #define FF_COLOR_RGB 0 /**< RGB color space */
49 #define FF_COLOR_GRAY 1 /**< gray color space */
50 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
51 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
53 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
54 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
55 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
57 typedef struct PixFmtInfo {
58 uint8_t nb_channels; /**< number of channels (including alpha) */
59 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
60 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
61 uint8_t is_alpha : 1; /**< true if alpha can be specified */
62 uint8_t depth; /**< bit depth of the color components */
65 /* this table gives more information about formats */
66 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
70 .color_type = FF_COLOR_YUV,
71 .pixel_type = FF_PIXEL_PLANAR,
76 .color_type = FF_COLOR_YUV,
77 .pixel_type = FF_PIXEL_PLANAR,
82 .color_type = FF_COLOR_YUV,
83 .pixel_type = FF_PIXEL_PLANAR,
88 .color_type = FF_COLOR_YUV,
89 .pixel_type = FF_PIXEL_PACKED,
94 .color_type = FF_COLOR_YUV,
95 .pixel_type = FF_PIXEL_PACKED,
100 .color_type = FF_COLOR_YUV,
101 .pixel_type = FF_PIXEL_PLANAR,
104 [PIX_FMT_YUV411P] = {
106 .color_type = FF_COLOR_YUV,
107 .pixel_type = FF_PIXEL_PLANAR,
110 [PIX_FMT_YUV440P] = {
112 .color_type = FF_COLOR_YUV,
113 .pixel_type = FF_PIXEL_PLANAR,
116 [PIX_FMT_YUV420P16LE] = {
118 .color_type = FF_COLOR_YUV,
119 .pixel_type = FF_PIXEL_PLANAR,
122 [PIX_FMT_YUV422P16LE] = {
124 .color_type = FF_COLOR_YUV,
125 .pixel_type = FF_PIXEL_PLANAR,
128 [PIX_FMT_YUV444P16LE] = {
130 .color_type = FF_COLOR_YUV,
131 .pixel_type = FF_PIXEL_PLANAR,
134 [PIX_FMT_YUV420P16BE] = {
136 .color_type = FF_COLOR_YUV,
137 .pixel_type = FF_PIXEL_PLANAR,
140 [PIX_FMT_YUV422P16BE] = {
142 .color_type = FF_COLOR_YUV,
143 .pixel_type = FF_PIXEL_PLANAR,
146 [PIX_FMT_YUV444P16BE] = {
148 .color_type = FF_COLOR_YUV,
149 .pixel_type = FF_PIXEL_PLANAR,
154 /* YUV formats with alpha plane */
155 [PIX_FMT_YUVA420P] = {
157 .color_type = FF_COLOR_YUV,
158 .pixel_type = FF_PIXEL_PLANAR,
163 [PIX_FMT_YUVJ420P] = {
165 .color_type = FF_COLOR_YUV_JPEG,
166 .pixel_type = FF_PIXEL_PLANAR,
169 [PIX_FMT_YUVJ422P] = {
171 .color_type = FF_COLOR_YUV_JPEG,
172 .pixel_type = FF_PIXEL_PLANAR,
175 [PIX_FMT_YUVJ444P] = {
177 .color_type = FF_COLOR_YUV_JPEG,
178 .pixel_type = FF_PIXEL_PLANAR,
181 [PIX_FMT_YUVJ440P] = {
183 .color_type = FF_COLOR_YUV_JPEG,
184 .pixel_type = FF_PIXEL_PLANAR,
191 .color_type = FF_COLOR_RGB,
192 .pixel_type = FF_PIXEL_PACKED,
197 .color_type = FF_COLOR_RGB,
198 .pixel_type = FF_PIXEL_PACKED,
202 .nb_channels = 4, .is_alpha = 1,
203 .color_type = FF_COLOR_RGB,
204 .pixel_type = FF_PIXEL_PACKED,
207 [PIX_FMT_RGB48BE] = {
209 .color_type = FF_COLOR_RGB,
210 .pixel_type = FF_PIXEL_PACKED,
213 [PIX_FMT_RGB48LE] = {
215 .color_type = FF_COLOR_RGB,
216 .pixel_type = FF_PIXEL_PACKED,
219 [PIX_FMT_RGB565BE] = {
221 .color_type = FF_COLOR_RGB,
222 .pixel_type = FF_PIXEL_PACKED,
225 [PIX_FMT_RGB565LE] = {
227 .color_type = FF_COLOR_RGB,
228 .pixel_type = FF_PIXEL_PACKED,
231 [PIX_FMT_RGB555BE] = {
233 .color_type = FF_COLOR_RGB,
234 .pixel_type = FF_PIXEL_PACKED,
237 [PIX_FMT_RGB555LE] = {
239 .color_type = FF_COLOR_RGB,
240 .pixel_type = FF_PIXEL_PACKED,
243 [PIX_FMT_RGB444BE] = {
245 .color_type = FF_COLOR_RGB,
246 .pixel_type = FF_PIXEL_PACKED,
249 [PIX_FMT_RGB444LE] = {
251 .color_type = FF_COLOR_RGB,
252 .pixel_type = FF_PIXEL_PACKED,
256 /* gray / mono formats */
257 [PIX_FMT_GRAY16BE] = {
259 .color_type = FF_COLOR_GRAY,
260 .pixel_type = FF_PIXEL_PLANAR,
263 [PIX_FMT_GRAY16LE] = {
265 .color_type = FF_COLOR_GRAY,
266 .pixel_type = FF_PIXEL_PLANAR,
271 .color_type = FF_COLOR_GRAY,
272 .pixel_type = FF_PIXEL_PLANAR,
275 [PIX_FMT_MONOWHITE] = {
277 .color_type = FF_COLOR_GRAY,
278 .pixel_type = FF_PIXEL_PLANAR,
281 [PIX_FMT_MONOBLACK] = {
283 .color_type = FF_COLOR_GRAY,
284 .pixel_type = FF_PIXEL_PLANAR,
288 /* paletted formats */
290 .nb_channels = 4, .is_alpha = 1,
291 .color_type = FF_COLOR_RGB,
292 .pixel_type = FF_PIXEL_PALETTE,
295 [PIX_FMT_UYYVYY411] = {
297 .color_type = FF_COLOR_YUV,
298 .pixel_type = FF_PIXEL_PACKED,
302 .nb_channels = 4, .is_alpha = 1,
303 .color_type = FF_COLOR_RGB,
304 .pixel_type = FF_PIXEL_PACKED,
307 [PIX_FMT_BGR565BE] = {
309 .color_type = FF_COLOR_RGB,
310 .pixel_type = FF_PIXEL_PACKED,
313 [PIX_FMT_BGR565LE] = {
315 .color_type = FF_COLOR_RGB,
316 .pixel_type = FF_PIXEL_PACKED,
319 [PIX_FMT_BGR555BE] = {
321 .color_type = FF_COLOR_RGB,
322 .pixel_type = FF_PIXEL_PACKED,
325 [PIX_FMT_BGR555LE] = {
327 .color_type = FF_COLOR_RGB,
328 .pixel_type = FF_PIXEL_PACKED,
331 [PIX_FMT_BGR444BE] = {
333 .color_type = FF_COLOR_RGB,
334 .pixel_type = FF_PIXEL_PACKED,
337 [PIX_FMT_BGR444LE] = {
339 .color_type = FF_COLOR_RGB,
340 .pixel_type = FF_PIXEL_PACKED,
345 .color_type = FF_COLOR_RGB,
346 .pixel_type = FF_PIXEL_PACKED,
351 .color_type = FF_COLOR_RGB,
352 .pixel_type = FF_PIXEL_PACKED,
355 [PIX_FMT_RGB4_BYTE] = {
357 .color_type = FF_COLOR_RGB,
358 .pixel_type = FF_PIXEL_PACKED,
363 .color_type = FF_COLOR_RGB,
364 .pixel_type = FF_PIXEL_PACKED,
369 .color_type = FF_COLOR_RGB,
370 .pixel_type = FF_PIXEL_PACKED,
373 [PIX_FMT_BGR4_BYTE] = {
375 .color_type = FF_COLOR_RGB,
376 .pixel_type = FF_PIXEL_PACKED,
381 .color_type = FF_COLOR_YUV,
382 .pixel_type = FF_PIXEL_PLANAR,
387 .color_type = FF_COLOR_YUV,
388 .pixel_type = FF_PIXEL_PLANAR,
393 .nb_channels = 4, .is_alpha = 1,
394 .color_type = FF_COLOR_RGB,
395 .pixel_type = FF_PIXEL_PACKED,
399 .nb_channels = 4, .is_alpha = 1,
400 .color_type = FF_COLOR_RGB,
401 .pixel_type = FF_PIXEL_PACKED,
406 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
408 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
409 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
412 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
414 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
417 return av_pix_fmt_descriptors[pix_fmt].name;
420 #if LIBAVCODEC_VERSION_MAJOR < 53
421 enum PixelFormat avcodec_get_pix_fmt(const char *name)
423 return av_get_pix_fmt(name);
427 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
431 snprintf (buf, buf_size,
432 "name " " nb_channels" " depth" " is_alpha"
435 PixFmtInfo info= pix_fmt_info[pix_fmt];
437 char is_alpha_char= info.is_alpha ? 'y' : 'n';
439 snprintf (buf, buf_size,
441 av_pix_fmt_descriptors[pix_fmt].name,
449 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
451 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
454 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
457 for(i=0; i<256; i++){
471 case PIX_FMT_RGB4_BYTE:
476 case PIX_FMT_BGR4_BYTE:
487 pal[i] = b + (g<<8) + (r<<16);
493 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
496 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
497 int max_plane_step [4];
498 int max_plane_step_comp[4];
500 memset(picture->linesize, 0, sizeof(picture->linesize));
502 if (desc->flags & PIX_FMT_HWACCEL)
505 if (desc->flags & PIX_FMT_BITSTREAM) {
506 picture->linesize[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
510 memset(max_plane_step , 0, sizeof(max_plane_step ));
511 memset(max_plane_step_comp, 0, sizeof(max_plane_step_comp));
512 for (i = 0; i < 4; i++) {
513 const AVComponentDescriptor *comp = &(desc->comp[i]);
514 if ((comp->step_minus1+1) > max_plane_step[comp->plane]) {
515 max_plane_step [comp->plane] = comp->step_minus1+1;
516 max_plane_step_comp[comp->plane] = i;
520 for (i = 0; i < 4; i++) {
521 int s = (max_plane_step_comp[i] == 1 || max_plane_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
522 picture->linesize[i] = max_plane_step[i] * (((width + (1 << s) - 1)) >> s);
528 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
532 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
534 size = picture->linesize[0] * height;
536 case PIX_FMT_YUV420P:
537 case PIX_FMT_YUV422P:
538 case PIX_FMT_YUV444P:
539 case PIX_FMT_YUV410P:
540 case PIX_FMT_YUV411P:
541 case PIX_FMT_YUV440P:
542 case PIX_FMT_YUVJ420P:
543 case PIX_FMT_YUVJ422P:
544 case PIX_FMT_YUVJ444P:
545 case PIX_FMT_YUVJ440P:
546 case PIX_FMT_YUV420P16LE:
547 case PIX_FMT_YUV422P16LE:
548 case PIX_FMT_YUV444P16LE:
549 case PIX_FMT_YUV420P16BE:
550 case PIX_FMT_YUV422P16BE:
551 case PIX_FMT_YUV444P16BE:
552 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
553 size2 = picture->linesize[1] * h2;
554 picture->data[0] = ptr;
555 picture->data[1] = picture->data[0] + size;
556 picture->data[2] = picture->data[1] + size2;
557 picture->data[3] = NULL;
558 return size + 2 * size2;
559 case PIX_FMT_YUVA420P:
560 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
561 size2 = picture->linesize[1] * h2;
562 picture->data[0] = ptr;
563 picture->data[1] = picture->data[0] + size;
564 picture->data[2] = picture->data[1] + size2;
565 picture->data[3] = picture->data[1] + size2 + size2;
566 return 2 * size + 2 * size2;
569 h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
570 size2 = picture->linesize[1] * h2;
571 picture->data[0] = ptr;
572 picture->data[1] = picture->data[0] + size;
573 picture->data[2] = NULL;
574 picture->data[3] = NULL;
582 case PIX_FMT_RGB48BE:
583 case PIX_FMT_RGB48LE:
584 case PIX_FMT_GRAY16BE:
585 case PIX_FMT_GRAY16LE:
586 case PIX_FMT_BGR444BE:
587 case PIX_FMT_BGR444LE:
588 case PIX_FMT_BGR555BE:
589 case PIX_FMT_BGR555LE:
590 case PIX_FMT_BGR565BE:
591 case PIX_FMT_BGR565LE:
592 case PIX_FMT_RGB444BE:
593 case PIX_FMT_RGB444LE:
594 case PIX_FMT_RGB555BE:
595 case PIX_FMT_RGB555LE:
596 case PIX_FMT_RGB565BE:
597 case PIX_FMT_RGB565LE:
598 case PIX_FMT_YUYV422:
599 case PIX_FMT_UYVY422:
600 case PIX_FMT_UYYVYY411:
603 case PIX_FMT_MONOWHITE:
604 case PIX_FMT_MONOBLACK:
605 picture->data[0] = ptr;
606 picture->data[1] = NULL;
607 picture->data[2] = NULL;
608 picture->data[3] = NULL;
613 case PIX_FMT_RGB4_BYTE:
614 case PIX_FMT_BGR4_BYTE:
616 size2 = (size + 3) & ~3;
617 picture->data[0] = ptr;
618 picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
619 picture->data[2] = NULL;
620 picture->data[3] = NULL;
621 return size2 + 256 * 4;
623 picture->data[0] = NULL;
624 picture->data[1] = NULL;
625 picture->data[2] = NULL;
626 picture->data[3] = NULL;
631 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
632 enum PixelFormat pix_fmt, int width, int height)
635 if(avcodec_check_dimensions(NULL, width, height))
638 if (ff_fill_linesize(picture, pix_fmt, width))
641 return ff_fill_pointer(picture, ptr, pix_fmt, height);
644 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
645 unsigned char *dest, int dest_size)
647 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
648 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
649 int i, j, w, ow, h, oh, data_planes;
650 const unsigned char* s;
651 int size = avpicture_get_size(pix_fmt, width, height);
653 if (size > dest_size || size < 0)
656 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
657 if (pix_fmt == PIX_FMT_YUYV422 ||
658 pix_fmt == PIX_FMT_UYVY422 ||
659 pix_fmt == PIX_FMT_BGR565BE ||
660 pix_fmt == PIX_FMT_BGR565LE ||
661 pix_fmt == PIX_FMT_BGR555BE ||
662 pix_fmt == PIX_FMT_BGR555LE ||
663 pix_fmt == PIX_FMT_BGR444BE ||
664 pix_fmt == PIX_FMT_BGR444LE ||
665 pix_fmt == PIX_FMT_RGB565BE ||
666 pix_fmt == PIX_FMT_RGB565LE ||
667 pix_fmt == PIX_FMT_RGB555BE ||
668 pix_fmt == PIX_FMT_RGB555LE ||
669 pix_fmt == PIX_FMT_RGB444BE ||
670 pix_fmt == PIX_FMT_RGB444LE)
672 else if (pix_fmt == PIX_FMT_UYYVYY411)
674 else if (pix_fmt == PIX_FMT_PAL8)
677 w = width * (pf->depth * pf->nb_channels / 8);
682 data_planes = pf->nb_channels;
683 w = (width*pf->depth + 7)/8;
690 for (i=0; i<data_planes; i++) {
692 w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
693 h = -((-height) >> desc->log2_chroma_h);
694 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
704 s += src->linesize[i];
708 if (pf->pixel_type == FF_PIXEL_PALETTE)
709 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
714 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
716 AVPicture dummy_pict;
717 if(avcodec_check_dimensions(NULL, width, height))
722 case PIX_FMT_RGB4_BYTE:
723 case PIX_FMT_BGR4_BYTE:
725 // do not include palette for these pseudo-paletted formats
726 return width * height;
728 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
731 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
734 const PixFmtInfo *pf, *ps;
735 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
736 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
739 ps = &pix_fmt_info[src_pix_fmt];
743 pf = &pix_fmt_info[dst_pix_fmt];
744 if (pf->depth < ps->depth ||
745 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
746 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
747 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
748 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
749 loss |= FF_LOSS_DEPTH;
750 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
751 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
752 loss |= FF_LOSS_RESOLUTION;
753 switch(pf->color_type) {
755 if (ps->color_type != FF_COLOR_RGB &&
756 ps->color_type != FF_COLOR_GRAY)
757 loss |= FF_LOSS_COLORSPACE;
760 if (ps->color_type != FF_COLOR_GRAY)
761 loss |= FF_LOSS_COLORSPACE;
764 if (ps->color_type != FF_COLOR_YUV)
765 loss |= FF_LOSS_COLORSPACE;
767 case FF_COLOR_YUV_JPEG:
768 if (ps->color_type != FF_COLOR_YUV_JPEG &&
769 ps->color_type != FF_COLOR_YUV &&
770 ps->color_type != FF_COLOR_GRAY)
771 loss |= FF_LOSS_COLORSPACE;
775 if (ps->color_type != pf->color_type)
776 loss |= FF_LOSS_COLORSPACE;
779 if (pf->color_type == FF_COLOR_GRAY &&
780 ps->color_type != FF_COLOR_GRAY)
781 loss |= FF_LOSS_CHROMA;
782 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
783 loss |= FF_LOSS_ALPHA;
784 if (pf->pixel_type == FF_PIXEL_PALETTE &&
785 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
786 loss |= FF_LOSS_COLORQUANT;
790 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
793 const PixFmtInfo *pf;
794 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
796 pf = &pix_fmt_info[pix_fmt];
797 switch(pf->pixel_type) {
798 case FF_PIXEL_PACKED:
800 case PIX_FMT_YUYV422:
801 case PIX_FMT_UYVY422:
802 case PIX_FMT_RGB565BE:
803 case PIX_FMT_RGB565LE:
804 case PIX_FMT_RGB555BE:
805 case PIX_FMT_RGB555LE:
806 case PIX_FMT_RGB444BE:
807 case PIX_FMT_RGB444LE:
808 case PIX_FMT_BGR565BE:
809 case PIX_FMT_BGR565LE:
810 case PIX_FMT_BGR555BE:
811 case PIX_FMT_BGR555LE:
812 case PIX_FMT_BGR444BE:
813 case PIX_FMT_BGR444LE:
816 case PIX_FMT_UYYVYY411:
820 bits = pf->depth * pf->nb_channels;
824 case FF_PIXEL_PLANAR:
825 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
826 bits = pf->depth * pf->nb_channels;
828 bits = pf->depth + ((2 * pf->depth) >>
829 (desc->log2_chroma_w + desc->log2_chroma_h));
832 case FF_PIXEL_PALETTE:
842 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
843 enum PixelFormat src_pix_fmt,
847 int dist, i, loss, min_dist;
848 enum PixelFormat dst_pix_fmt;
850 /* find exact color match with smallest size */
851 dst_pix_fmt = PIX_FMT_NONE;
852 min_dist = 0x7fffffff;
853 for(i = 0;i < PIX_FMT_NB; i++) {
854 if (pix_fmt_mask & (1ULL << i)) {
855 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
857 dist = avg_bits_per_pixel(i);
858 if (dist < min_dist) {
868 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
869 int has_alpha, int *loss_ptr)
871 enum PixelFormat dst_pix_fmt;
873 static const int loss_mask_order[] = {
874 ~0, /* no loss first */
877 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
883 /* try with successive loss */
886 loss_mask = loss_mask_order[i++];
887 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
888 has_alpha, loss_mask);
889 if (dst_pix_fmt >= 0)
897 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
901 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
902 const uint8_t *src, int src_wrap,
903 int width, int height)
907 for(;height > 0; height--) {
908 memcpy(dst, src, width);
914 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
917 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
918 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
920 pf = &pix_fmt_info[pix_fmt];
921 switch(pf->pixel_type) {
922 case FF_PIXEL_PACKED:
924 case PIX_FMT_YUYV422:
925 case PIX_FMT_UYVY422:
926 case PIX_FMT_RGB565BE:
927 case PIX_FMT_RGB565LE:
928 case PIX_FMT_RGB555BE:
929 case PIX_FMT_RGB555LE:
930 case PIX_FMT_RGB444BE:
931 case PIX_FMT_RGB444LE:
932 case PIX_FMT_BGR565BE:
933 case PIX_FMT_BGR565LE:
934 case PIX_FMT_BGR555BE:
935 case PIX_FMT_BGR555LE:
936 case PIX_FMT_BGR444BE:
937 case PIX_FMT_BGR444LE:
940 case PIX_FMT_UYYVYY411:
944 bits = pf->depth * pf->nb_channels;
947 return (width * bits + 7) >> 3;
949 case FF_PIXEL_PLANAR:
950 if (plane == 1 || plane == 2)
951 width= -((-width)>>desc->log2_chroma_w);
953 return (width * pf->depth + 7) >> 3;
955 case FF_PIXEL_PALETTE:
964 void av_picture_copy(AVPicture *dst, const AVPicture *src,
965 enum PixelFormat pix_fmt, int width, int height)
968 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
969 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
971 switch(pf->pixel_type) {
972 case FF_PIXEL_PACKED:
973 case FF_PIXEL_PLANAR:
974 for(i = 0; i < pf->nb_channels; i++) {
976 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
978 if (i == 1 || i == 2) {
979 h= -((-height)>>desc->log2_chroma_h);
981 ff_img_copy_plane(dst->data[i], dst->linesize[i],
982 src->data[i], src->linesize[i],
986 case FF_PIXEL_PALETTE:
987 ff_img_copy_plane(dst->data[0], dst->linesize[0],
988 src->data[0], src->linesize[0],
990 /* copy the palette */
991 memcpy(dst->data[1], src->data[1], 4*256);
997 void ff_shrink22(uint8_t *dst, int dst_wrap,
998 const uint8_t *src, int src_wrap,
999 int width, int height)
1002 const uint8_t *s1, *s2;
1005 for(;height > 0; height--) {
1009 for(w = width;w >= 4; w-=4) {
1010 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1011 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1012 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1013 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1019 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1024 src += 2 * src_wrap;
1030 void ff_shrink44(uint8_t *dst, int dst_wrap,
1031 const uint8_t *src, int src_wrap,
1032 int width, int height)
1035 const uint8_t *s1, *s2, *s3, *s4;
1038 for(;height > 0; height--) {
1044 for(w = width;w > 0; w--) {
1045 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1046 s2[0] + s2[1] + s2[2] + s2[3] +
1047 s3[0] + s3[1] + s3[2] + s3[3] +
1048 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1055 src += 4 * src_wrap;
1061 void ff_shrink88(uint8_t *dst, int dst_wrap,
1062 const uint8_t *src, int src_wrap,
1063 int width, int height)
1067 for(;height > 0; height--) {
1068 for(w = width;w > 0; w--) {
1071 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1074 *(dst++) = (tmp + 32)>>6;
1075 src += 8 - 8*src_wrap;
1077 src += 8*src_wrap - 8*width;
1078 dst += dst_wrap - width;
1083 int avpicture_alloc(AVPicture *picture,
1084 enum PixelFormat pix_fmt, int width, int height)
1089 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1092 ptr = av_malloc(size);
1095 avpicture_fill(picture, ptr, pix_fmt, width, height);
1096 if(picture->data[1] && !picture->data[2])
1097 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1101 memset(picture, 0, sizeof(AVPicture));
1105 void avpicture_free(AVPicture *picture)
1107 av_free(picture->data[0]);
1110 /* return true if yuv planar */
1111 static inline int is_yuv_planar(const PixFmtInfo *ps)
1113 return (ps->color_type == FF_COLOR_YUV ||
1114 ps->color_type == FF_COLOR_YUV_JPEG) &&
1115 ps->pixel_type == FF_PIXEL_PLANAR;
1118 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1119 enum PixelFormat pix_fmt, int top_band, int left_band)
1124 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1127 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1128 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1130 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1131 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1132 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1134 dst->linesize[0] = src->linesize[0];
1135 dst->linesize[1] = src->linesize[1];
1136 dst->linesize[2] = src->linesize[2];
1140 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1141 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1150 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1151 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1153 for (i = 0; i < 3; i++) {
1154 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1155 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1157 if (padtop || padleft) {
1158 memset(dst->data[i], color[i],
1159 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1162 if (padleft || padright) {
1163 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1164 (dst->linesize[i] - (padright >> x_shift));
1165 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1166 for (y = 0; y < yheight; y++) {
1167 memset(optr, color[i], (padleft + padright) >> x_shift);
1168 optr += dst->linesize[i];
1172 if (src) { /* first line */
1173 uint8_t *iptr = src->data[i];
1174 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1175 (padleft >> x_shift);
1176 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1177 iptr += src->linesize[i];
1178 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1179 (dst->linesize[i] - (padright >> x_shift));
1180 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1181 for (y = 0; y < yheight; y++) {
1182 memset(optr, color[i], (padleft + padright) >> x_shift);
1183 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1184 (width - padleft - padright) >> x_shift);
1185 iptr += src->linesize[i];
1186 optr += dst->linesize[i];
1190 if (padbottom || padright) {
1191 optr = dst->data[i] + dst->linesize[i] *
1192 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1193 memset(optr, color[i],dst->linesize[i] *
1194 (padbottom >> y_shift) + (padright >> x_shift));
1200 /* NOTE: we scan all the pixels to have an exact information */
1201 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1203 const unsigned char *p;
1204 int src_wrap, ret, x, y;
1206 uint32_t *palette = (uint32_t *)src->data[1];
1209 src_wrap = src->linesize[0] - width;
1211 for(y=0;y<height;y++) {
1212 for(x=0;x<width;x++) {
1213 a = palette[p[0]] >> 24;
1215 ret |= FF_ALPHA_TRANSP;
1216 } else if (a != 0xff) {
1217 ret |= FF_ALPHA_SEMI_TRANSP;
1226 int img_get_alpha_info(const AVPicture *src,
1227 enum PixelFormat pix_fmt, int width, int height)
1229 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1232 /* no alpha can be represented in format */
1237 ret = get_alpha_info_pal8(src, width, height);
1240 /* we do not know, so everything is indicated */
1241 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1248 #define DEINT_INPLACE_LINE_LUM \
1249 movd_m2r(lum_m4[0],mm0);\
1250 movd_m2r(lum_m3[0],mm1);\
1251 movd_m2r(lum_m2[0],mm2);\
1252 movd_m2r(lum_m1[0],mm3);\
1253 movd_m2r(lum[0],mm4);\
1254 punpcklbw_r2r(mm7,mm0);\
1255 movd_r2m(mm2,lum_m4[0]);\
1256 punpcklbw_r2r(mm7,mm1);\
1257 punpcklbw_r2r(mm7,mm2);\
1258 punpcklbw_r2r(mm7,mm3);\
1259 punpcklbw_r2r(mm7,mm4);\
1260 paddw_r2r(mm3,mm1);\
1262 paddw_r2r(mm4,mm0);\
1264 paddw_r2r(mm6,mm2);\
1265 paddw_r2r(mm2,mm1);\
1266 psubusw_r2r(mm0,mm1);\
1268 packuswb_r2r(mm7,mm1);\
1269 movd_r2m(mm1,lum_m2[0]);
1271 #define DEINT_LINE_LUM \
1272 movd_m2r(lum_m4[0],mm0);\
1273 movd_m2r(lum_m3[0],mm1);\
1274 movd_m2r(lum_m2[0],mm2);\
1275 movd_m2r(lum_m1[0],mm3);\
1276 movd_m2r(lum[0],mm4);\
1277 punpcklbw_r2r(mm7,mm0);\
1278 punpcklbw_r2r(mm7,mm1);\
1279 punpcklbw_r2r(mm7,mm2);\
1280 punpcklbw_r2r(mm7,mm3);\
1281 punpcklbw_r2r(mm7,mm4);\
1282 paddw_r2r(mm3,mm1);\
1284 paddw_r2r(mm4,mm0);\
1286 paddw_r2r(mm6,mm2);\
1287 paddw_r2r(mm2,mm1);\
1288 psubusw_r2r(mm0,mm1);\
1290 packuswb_r2r(mm7,mm1);\
1291 movd_r2m(mm1,dst[0]);
1294 /* filter parameters: [-1 4 2 4 -1] // 8 */
1295 static void deinterlace_line(uint8_t *dst,
1296 const uint8_t *lum_m4, const uint8_t *lum_m3,
1297 const uint8_t *lum_m2, const uint8_t *lum_m1,
1302 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1305 for(;size > 0;size--) {
1307 sum += lum_m3[0] << 2;
1308 sum += lum_m2[0] << 1;
1309 sum += lum_m1[0] << 2;
1311 dst[0] = cm[(sum + 4) >> 3];
1323 movq_m2r(ff_pw_4,mm6);
1325 for (;size > 3; size-=4) {
1336 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1340 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1343 for(;size > 0;size--) {
1345 sum += lum_m3[0] << 2;
1346 sum += lum_m2[0] << 1;
1347 lum_m4[0]=lum_m2[0];
1348 sum += lum_m1[0] << 2;
1350 lum_m2[0] = cm[(sum + 4) >> 3];
1361 movq_m2r(ff_pw_4,mm6);
1363 for (;size > 3; size-=4) {
1364 DEINT_INPLACE_LINE_LUM
1374 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1375 top field is copied as is, but the bottom field is deinterlaced
1376 against the top field. */
1377 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1378 const uint8_t *src1, int src_wrap,
1379 int width, int height)
1381 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1386 src_0=&src_m1[src_wrap];
1387 src_p1=&src_0[src_wrap];
1388 src_p2=&src_p1[src_wrap];
1389 for(y=0;y<(height-2);y+=2) {
1390 memcpy(dst,src_m1,width);
1392 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1396 src_p1 += 2*src_wrap;
1397 src_p2 += 2*src_wrap;
1400 memcpy(dst,src_m1,width);
1403 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1406 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1407 int width, int height)
1409 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1412 buf = (uint8_t*)av_malloc(width);
1415 memcpy(buf,src_m1,width);
1416 src_0=&src_m1[src_wrap];
1417 src_p1=&src_0[src_wrap];
1418 src_p2=&src_p1[src_wrap];
1419 for(y=0;y<(height-2);y+=2) {
1420 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1423 src_p1 += 2*src_wrap;
1424 src_p2 += 2*src_wrap;
1427 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1431 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1432 enum PixelFormat pix_fmt, int width, int height)
1436 if (pix_fmt != PIX_FMT_YUV420P &&
1437 pix_fmt != PIX_FMT_YUV422P &&
1438 pix_fmt != PIX_FMT_YUV444P &&
1439 pix_fmt != PIX_FMT_YUV411P &&
1440 pix_fmt != PIX_FMT_GRAY8)
1442 if ((width & 3) != 0 || (height & 3) != 0)
1448 case PIX_FMT_YUV420P:
1452 case PIX_FMT_YUV422P:
1455 case PIX_FMT_YUV411P:
1461 if (pix_fmt == PIX_FMT_GRAY8) {
1466 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1469 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1470 src->data[i], src->linesize[i],