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
36 #include "imgconvert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavcore/imgutils.h"
41 #if HAVE_MMX && HAVE_YASM
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 #if HAVE_MMX && HAVE_YASM
58 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
59 #define deinterlace_line ff_deinterlace_line_mmx
61 #define deinterlace_line_inplace deinterlace_line_inplace_c
62 #define deinterlace_line deinterlace_line_c
65 typedef struct PixFmtInfo
{
66 uint8_t nb_channels
; /**< number of channels (including alpha) */
67 uint8_t color_type
; /**< color type (see FF_COLOR_xxx constants) */
68 uint8_t pixel_type
; /**< pixel storage type (see FF_PIXEL_xxx constants) */
69 uint8_t is_alpha
: 1; /**< true if alpha can be specified */
70 uint8_t depth
; /**< bit depth of the color components */
73 /* this table gives more information about formats */
74 static const PixFmtInfo pix_fmt_info
[PIX_FMT_NB
] = {
78 .color_type
= FF_COLOR_YUV
,
79 .pixel_type
= FF_PIXEL_PLANAR
,
84 .color_type
= FF_COLOR_YUV
,
85 .pixel_type
= FF_PIXEL_PLANAR
,
90 .color_type
= FF_COLOR_YUV
,
91 .pixel_type
= FF_PIXEL_PLANAR
,
96 .color_type
= FF_COLOR_YUV
,
97 .pixel_type
= FF_PIXEL_PACKED
,
100 [PIX_FMT_UYVY422
] = {
102 .color_type
= FF_COLOR_YUV
,
103 .pixel_type
= FF_PIXEL_PACKED
,
106 [PIX_FMT_YUV410P
] = {
108 .color_type
= FF_COLOR_YUV
,
109 .pixel_type
= FF_PIXEL_PLANAR
,
112 [PIX_FMT_YUV411P
] = {
114 .color_type
= FF_COLOR_YUV
,
115 .pixel_type
= FF_PIXEL_PLANAR
,
118 [PIX_FMT_YUV440P
] = {
120 .color_type
= FF_COLOR_YUV
,
121 .pixel_type
= FF_PIXEL_PLANAR
,
124 [PIX_FMT_YUV420P16LE
] = {
126 .color_type
= FF_COLOR_YUV
,
127 .pixel_type
= FF_PIXEL_PLANAR
,
130 [PIX_FMT_YUV422P16LE
] = {
132 .color_type
= FF_COLOR_YUV
,
133 .pixel_type
= FF_PIXEL_PLANAR
,
136 [PIX_FMT_YUV444P16LE
] = {
138 .color_type
= FF_COLOR_YUV
,
139 .pixel_type
= FF_PIXEL_PLANAR
,
142 [PIX_FMT_YUV420P16BE
] = {
144 .color_type
= FF_COLOR_YUV
,
145 .pixel_type
= FF_PIXEL_PLANAR
,
148 [PIX_FMT_YUV422P16BE
] = {
150 .color_type
= FF_COLOR_YUV
,
151 .pixel_type
= FF_PIXEL_PLANAR
,
154 [PIX_FMT_YUV444P16BE
] = {
156 .color_type
= FF_COLOR_YUV
,
157 .pixel_type
= FF_PIXEL_PLANAR
,
162 /* YUV formats with alpha plane */
163 [PIX_FMT_YUVA420P
] = {
165 .color_type
= FF_COLOR_YUV
,
166 .pixel_type
= FF_PIXEL_PLANAR
,
171 [PIX_FMT_YUVJ420P
] = {
173 .color_type
= FF_COLOR_YUV_JPEG
,
174 .pixel_type
= FF_PIXEL_PLANAR
,
177 [PIX_FMT_YUVJ422P
] = {
179 .color_type
= FF_COLOR_YUV_JPEG
,
180 .pixel_type
= FF_PIXEL_PLANAR
,
183 [PIX_FMT_YUVJ444P
] = {
185 .color_type
= FF_COLOR_YUV_JPEG
,
186 .pixel_type
= FF_PIXEL_PLANAR
,
189 [PIX_FMT_YUVJ440P
] = {
191 .color_type
= FF_COLOR_YUV_JPEG
,
192 .pixel_type
= FF_PIXEL_PLANAR
,
199 .color_type
= FF_COLOR_RGB
,
200 .pixel_type
= FF_PIXEL_PACKED
,
205 .color_type
= FF_COLOR_RGB
,
206 .pixel_type
= FF_PIXEL_PACKED
,
210 .nb_channels
= 4, .is_alpha
= 1,
211 .color_type
= FF_COLOR_RGB
,
212 .pixel_type
= FF_PIXEL_PACKED
,
215 [PIX_FMT_RGB48BE
] = {
217 .color_type
= FF_COLOR_RGB
,
218 .pixel_type
= FF_PIXEL_PACKED
,
221 [PIX_FMT_RGB48LE
] = {
223 .color_type
= FF_COLOR_RGB
,
224 .pixel_type
= FF_PIXEL_PACKED
,
227 [PIX_FMT_RGB565BE
] = {
229 .color_type
= FF_COLOR_RGB
,
230 .pixel_type
= FF_PIXEL_PACKED
,
233 [PIX_FMT_RGB565LE
] = {
235 .color_type
= FF_COLOR_RGB
,
236 .pixel_type
= FF_PIXEL_PACKED
,
239 [PIX_FMT_RGB555BE
] = {
241 .color_type
= FF_COLOR_RGB
,
242 .pixel_type
= FF_PIXEL_PACKED
,
245 [PIX_FMT_RGB555LE
] = {
247 .color_type
= FF_COLOR_RGB
,
248 .pixel_type
= FF_PIXEL_PACKED
,
251 [PIX_FMT_RGB444BE
] = {
253 .color_type
= FF_COLOR_RGB
,
254 .pixel_type
= FF_PIXEL_PACKED
,
257 [PIX_FMT_RGB444LE
] = {
259 .color_type
= FF_COLOR_RGB
,
260 .pixel_type
= FF_PIXEL_PACKED
,
264 /* gray / mono formats */
265 [PIX_FMT_GRAY16BE
] = {
267 .color_type
= FF_COLOR_GRAY
,
268 .pixel_type
= FF_PIXEL_PLANAR
,
271 [PIX_FMT_GRAY16LE
] = {
273 .color_type
= FF_COLOR_GRAY
,
274 .pixel_type
= FF_PIXEL_PLANAR
,
279 .color_type
= FF_COLOR_GRAY
,
280 .pixel_type
= FF_PIXEL_PLANAR
,
283 [PIX_FMT_MONOWHITE
] = {
285 .color_type
= FF_COLOR_GRAY
,
286 .pixel_type
= FF_PIXEL_PLANAR
,
289 [PIX_FMT_MONOBLACK
] = {
291 .color_type
= FF_COLOR_GRAY
,
292 .pixel_type
= FF_PIXEL_PLANAR
,
296 /* paletted formats */
298 .nb_channels
= 4, .is_alpha
= 1,
299 .color_type
= FF_COLOR_RGB
,
300 .pixel_type
= FF_PIXEL_PALETTE
,
303 [PIX_FMT_UYYVYY411
] = {
305 .color_type
= FF_COLOR_YUV
,
306 .pixel_type
= FF_PIXEL_PACKED
,
310 .nb_channels
= 4, .is_alpha
= 1,
311 .color_type
= FF_COLOR_RGB
,
312 .pixel_type
= FF_PIXEL_PACKED
,
315 [PIX_FMT_BGR565BE
] = {
317 .color_type
= FF_COLOR_RGB
,
318 .pixel_type
= FF_PIXEL_PACKED
,
321 [PIX_FMT_BGR565LE
] = {
323 .color_type
= FF_COLOR_RGB
,
324 .pixel_type
= FF_PIXEL_PACKED
,
327 [PIX_FMT_BGR555BE
] = {
329 .color_type
= FF_COLOR_RGB
,
330 .pixel_type
= FF_PIXEL_PACKED
,
333 [PIX_FMT_BGR555LE
] = {
335 .color_type
= FF_COLOR_RGB
,
336 .pixel_type
= FF_PIXEL_PACKED
,
339 [PIX_FMT_BGR444BE
] = {
341 .color_type
= FF_COLOR_RGB
,
342 .pixel_type
= FF_PIXEL_PACKED
,
345 [PIX_FMT_BGR444LE
] = {
347 .color_type
= FF_COLOR_RGB
,
348 .pixel_type
= FF_PIXEL_PACKED
,
353 .color_type
= FF_COLOR_RGB
,
354 .pixel_type
= FF_PIXEL_PACKED
,
359 .color_type
= FF_COLOR_RGB
,
360 .pixel_type
= FF_PIXEL_PACKED
,
363 [PIX_FMT_RGB4_BYTE
] = {
365 .color_type
= FF_COLOR_RGB
,
366 .pixel_type
= FF_PIXEL_PACKED
,
371 .color_type
= FF_COLOR_RGB
,
372 .pixel_type
= FF_PIXEL_PACKED
,
377 .color_type
= FF_COLOR_RGB
,
378 .pixel_type
= FF_PIXEL_PACKED
,
381 [PIX_FMT_BGR4_BYTE
] = {
383 .color_type
= FF_COLOR_RGB
,
384 .pixel_type
= FF_PIXEL_PACKED
,
389 .color_type
= FF_COLOR_YUV
,
390 .pixel_type
= FF_PIXEL_PLANAR
,
395 .color_type
= FF_COLOR_YUV
,
396 .pixel_type
= FF_PIXEL_PLANAR
,
401 .nb_channels
= 4, .is_alpha
= 1,
402 .color_type
= FF_COLOR_RGB
,
403 .pixel_type
= FF_PIXEL_PACKED
,
407 .nb_channels
= 4, .is_alpha
= 1,
408 .color_type
= FF_COLOR_RGB
,
409 .pixel_type
= FF_PIXEL_PACKED
,
414 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt
, int *h_shift
, int *v_shift
)
416 *h_shift
= av_pix_fmt_descriptors
[pix_fmt
].log2_chroma_w
;
417 *v_shift
= av_pix_fmt_descriptors
[pix_fmt
].log2_chroma_h
;
420 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt
)
422 if (pix_fmt
< 0 || pix_fmt
>= PIX_FMT_NB
)
425 return av_pix_fmt_descriptors
[pix_fmt
].name
;
428 #if LIBAVCODEC_VERSION_MAJOR < 53
429 enum PixelFormat
avcodec_get_pix_fmt(const char *name
)
431 return av_get_pix_fmt(name
);
435 void avcodec_pix_fmt_string (char *buf
, int buf_size
, enum PixelFormat pix_fmt
)
439 snprintf (buf
, buf_size
,
440 "name " " nb_channels" " depth" " is_alpha"
443 PixFmtInfo info
= pix_fmt_info
[pix_fmt
];
445 char is_alpha_char
= info
.is_alpha ?
'y' : 'n';
447 snprintf (buf
, buf_size
,
449 av_pix_fmt_descriptors
[pix_fmt
].name
,
457 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt
)
459 return av_pix_fmt_descriptors
[pix_fmt
].flags
& PIX_FMT_HWACCEL
;
462 int ff_set_systematic_pal(uint32_t pal
[256], enum PixelFormat pix_fmt
){
465 for(i
=0; i
<256; i
++){
479 case PIX_FMT_RGB4_BYTE
:
484 case PIX_FMT_BGR4_BYTE
:
495 pal
[i
] = b
+ (g
<<8) + (r
<<16);
501 #if LIBAVCODEC_VERSION_MAJOR < 53
502 int ff_fill_linesize(AVPicture
*picture
, enum PixelFormat pix_fmt
, int width
)
504 return av_fill_image_linesizes(picture
->linesize
, pix_fmt
, width
);
507 int ff_fill_pointer(AVPicture
*picture
, uint8_t *ptr
, enum PixelFormat pix_fmt
,
510 return av_fill_image_pointers(picture
->data
, pix_fmt
, height
, ptr
, picture
->linesize
);
514 int avpicture_fill(AVPicture
*picture
, uint8_t *ptr
,
515 enum PixelFormat pix_fmt
, int width
, int height
)
518 if(avcodec_check_dimensions(NULL
, width
, height
))
521 if (av_fill_image_linesizes(picture
->linesize
, pix_fmt
, width
))
524 return av_fill_image_pointers(picture
->data
, pix_fmt
, height
, ptr
, picture
->linesize
);
527 int avpicture_layout(const AVPicture
* src
, enum PixelFormat pix_fmt
, int width
, int height
,
528 unsigned char *dest
, int dest_size
)
530 const PixFmtInfo
* pf
= &pix_fmt_info
[pix_fmt
];
531 const AVPixFmtDescriptor
*desc
= &av_pix_fmt_descriptors
[pix_fmt
];
532 int i
, j
, w
, ow
, h
, oh
, data_planes
;
533 const unsigned char* s
;
534 int size
= avpicture_get_size(pix_fmt
, width
, height
);
536 if (size
> dest_size
|| size
< 0)
539 if (pf
->pixel_type
== FF_PIXEL_PACKED
|| pf
->pixel_type
== FF_PIXEL_PALETTE
) {
540 if (pix_fmt
== PIX_FMT_YUYV422
||
541 pix_fmt
== PIX_FMT_UYVY422
||
542 pix_fmt
== PIX_FMT_BGR565BE
||
543 pix_fmt
== PIX_FMT_BGR565LE
||
544 pix_fmt
== PIX_FMT_BGR555BE
||
545 pix_fmt
== PIX_FMT_BGR555LE
||
546 pix_fmt
== PIX_FMT_BGR444BE
||
547 pix_fmt
== PIX_FMT_BGR444LE
||
548 pix_fmt
== PIX_FMT_RGB565BE
||
549 pix_fmt
== PIX_FMT_RGB565LE
||
550 pix_fmt
== PIX_FMT_RGB555BE
||
551 pix_fmt
== PIX_FMT_RGB555LE
||
552 pix_fmt
== PIX_FMT_RGB444BE
||
553 pix_fmt
== PIX_FMT_RGB444LE
)
555 else if (pix_fmt
== PIX_FMT_UYYVYY411
)
557 else if (pix_fmt
== PIX_FMT_PAL8
)
560 w
= width
* (pf
->depth
* pf
->nb_channels
/ 8);
565 data_planes
= pf
->nb_channels
;
566 w
= (width
*pf
->depth
+ 7)/8;
573 for (i
=0; i
<data_planes
; i
++) {
575 w
= (- ((-width
) >> desc
->log2_chroma_w
) * pf
->depth
+ 7) / 8;
576 h
= -((-height
) >> desc
->log2_chroma_h
);
577 if (pix_fmt
== PIX_FMT_NV12
|| pix_fmt
== PIX_FMT_NV21
)
587 s
+= src
->linesize
[i
];
591 if (pf
->pixel_type
== FF_PIXEL_PALETTE
)
592 memcpy((unsigned char *)(((size_t)dest
+ 3) & ~3), src
->data
[1], 256 * 4);
597 int avpicture_get_size(enum PixelFormat pix_fmt
, int width
, int height
)
599 AVPicture dummy_pict
;
600 if(avcodec_check_dimensions(NULL
, width
, height
))
605 case PIX_FMT_RGB4_BYTE
:
606 case PIX_FMT_BGR4_BYTE
:
608 // do not include palette for these pseudo-paletted formats
609 return width
* height
;
611 return avpicture_fill(&dummy_pict
, NULL
, pix_fmt
, width
, height
);
614 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt
, enum PixelFormat src_pix_fmt
,
617 const PixFmtInfo
*pf
, *ps
;
618 const AVPixFmtDescriptor
*src_desc
= &av_pix_fmt_descriptors
[src_pix_fmt
];
619 const AVPixFmtDescriptor
*dst_desc
= &av_pix_fmt_descriptors
[dst_pix_fmt
];
622 ps
= &pix_fmt_info
[src_pix_fmt
];
626 pf
= &pix_fmt_info
[dst_pix_fmt
];
627 if (pf
->depth
< ps
->depth
||
628 ((dst_pix_fmt
== PIX_FMT_RGB555BE
|| dst_pix_fmt
== PIX_FMT_RGB555LE
||
629 dst_pix_fmt
== PIX_FMT_BGR555BE
|| dst_pix_fmt
== PIX_FMT_BGR555LE
) &&
630 (src_pix_fmt
== PIX_FMT_RGB565BE
|| src_pix_fmt
== PIX_FMT_RGB565LE
||
631 src_pix_fmt
== PIX_FMT_BGR565BE
|| src_pix_fmt
== PIX_FMT_BGR565LE
)))
632 loss
|= FF_LOSS_DEPTH
;
633 if (dst_desc
->log2_chroma_w
> src_desc
->log2_chroma_w
||
634 dst_desc
->log2_chroma_h
> src_desc
->log2_chroma_h
)
635 loss
|= FF_LOSS_RESOLUTION
;
636 switch(pf
->color_type
) {
638 if (ps
->color_type
!= FF_COLOR_RGB
&&
639 ps
->color_type
!= FF_COLOR_GRAY
)
640 loss
|= FF_LOSS_COLORSPACE
;
643 if (ps
->color_type
!= FF_COLOR_GRAY
)
644 loss
|= FF_LOSS_COLORSPACE
;
647 if (ps
->color_type
!= FF_COLOR_YUV
)
648 loss
|= FF_LOSS_COLORSPACE
;
650 case FF_COLOR_YUV_JPEG
:
651 if (ps
->color_type
!= FF_COLOR_YUV_JPEG
&&
652 ps
->color_type
!= FF_COLOR_YUV
&&
653 ps
->color_type
!= FF_COLOR_GRAY
)
654 loss
|= FF_LOSS_COLORSPACE
;
658 if (ps
->color_type
!= pf
->color_type
)
659 loss
|= FF_LOSS_COLORSPACE
;
662 if (pf
->color_type
== FF_COLOR_GRAY
&&
663 ps
->color_type
!= FF_COLOR_GRAY
)
664 loss
|= FF_LOSS_CHROMA
;
665 if (!pf
->is_alpha
&& (ps
->is_alpha
&& has_alpha
))
666 loss
|= FF_LOSS_ALPHA
;
667 if (pf
->pixel_type
== FF_PIXEL_PALETTE
&&
668 (ps
->pixel_type
!= FF_PIXEL_PALETTE
&& ps
->color_type
!= FF_COLOR_GRAY
))
669 loss
|= FF_LOSS_COLORQUANT
;
673 static int avg_bits_per_pixel(enum PixelFormat pix_fmt
)
676 const PixFmtInfo
*pf
;
677 const AVPixFmtDescriptor
*desc
= &av_pix_fmt_descriptors
[pix_fmt
];
679 pf
= &pix_fmt_info
[pix_fmt
];
680 switch(pf
->pixel_type
) {
681 case FF_PIXEL_PACKED
:
683 case PIX_FMT_YUYV422
:
684 case PIX_FMT_UYVY422
:
685 case PIX_FMT_RGB565BE
:
686 case PIX_FMT_RGB565LE
:
687 case PIX_FMT_RGB555BE
:
688 case PIX_FMT_RGB555LE
:
689 case PIX_FMT_RGB444BE
:
690 case PIX_FMT_RGB444LE
:
691 case PIX_FMT_BGR565BE
:
692 case PIX_FMT_BGR565LE
:
693 case PIX_FMT_BGR555BE
:
694 case PIX_FMT_BGR555LE
:
695 case PIX_FMT_BGR444BE
:
696 case PIX_FMT_BGR444LE
:
699 case PIX_FMT_UYYVYY411
:
703 bits
= pf
->depth
* pf
->nb_channels
;
707 case FF_PIXEL_PLANAR
:
708 if (desc
->log2_chroma_w
== 0 && desc
->log2_chroma_h
== 0) {
709 bits
= pf
->depth
* pf
->nb_channels
;
711 bits
= pf
->depth
+ ((2 * pf
->depth
) >>
712 (desc
->log2_chroma_w
+ desc
->log2_chroma_h
));
715 case FF_PIXEL_PALETTE
:
725 static enum PixelFormat
avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask
,
726 enum PixelFormat src_pix_fmt
,
730 int dist
, i
, loss
, min_dist
;
731 enum PixelFormat dst_pix_fmt
;
733 /* find exact color match with smallest size */
734 dst_pix_fmt
= PIX_FMT_NONE
;
735 min_dist
= 0x7fffffff;
736 for(i
= 0;i
< PIX_FMT_NB
; i
++) {
737 if (pix_fmt_mask
& (1ULL << i
)) {
738 loss
= avcodec_get_pix_fmt_loss(i
, src_pix_fmt
, has_alpha
) & loss_mask
;
740 dist
= avg_bits_per_pixel(i
);
741 if (dist
< min_dist
) {
751 enum PixelFormat
avcodec_find_best_pix_fmt(int64_t pix_fmt_mask
, enum PixelFormat src_pix_fmt
,
752 int has_alpha
, int *loss_ptr
)
754 enum PixelFormat dst_pix_fmt
;
756 static const int loss_mask_order
[] = {
757 ~0, /* no loss first */
760 ~(FF_LOSS_COLORSPACE
| FF_LOSS_RESOLUTION
),
766 /* try with successive loss */
769 loss_mask
= loss_mask_order
[i
++];
770 dst_pix_fmt
= avcodec_find_best_pix_fmt1(pix_fmt_mask
, src_pix_fmt
,
771 has_alpha
, loss_mask
);
772 if (dst_pix_fmt
>= 0)
780 *loss_ptr
= avcodec_get_pix_fmt_loss(dst_pix_fmt
, src_pix_fmt
, has_alpha
);
784 void ff_img_copy_plane(uint8_t *dst
, int dst_wrap
,
785 const uint8_t *src
, int src_wrap
,
786 int width
, int height
)
790 for(;height
> 0; height
--) {
791 memcpy(dst
, src
, width
);
797 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt
, int width
, int plane
)
800 const PixFmtInfo
*pf
= &pix_fmt_info
[pix_fmt
];
801 const AVPixFmtDescriptor
*desc
= &av_pix_fmt_descriptors
[pix_fmt
];
803 pf
= &pix_fmt_info
[pix_fmt
];
804 switch(pf
->pixel_type
) {
805 case FF_PIXEL_PACKED
:
807 case PIX_FMT_YUYV422
:
808 case PIX_FMT_UYVY422
:
809 case PIX_FMT_RGB565BE
:
810 case PIX_FMT_RGB565LE
:
811 case PIX_FMT_RGB555BE
:
812 case PIX_FMT_RGB555LE
:
813 case PIX_FMT_RGB444BE
:
814 case PIX_FMT_RGB444LE
:
815 case PIX_FMT_BGR565BE
:
816 case PIX_FMT_BGR565LE
:
817 case PIX_FMT_BGR555BE
:
818 case PIX_FMT_BGR555LE
:
819 case PIX_FMT_BGR444BE
:
820 case PIX_FMT_BGR444LE
:
823 case PIX_FMT_UYYVYY411
:
827 bits
= pf
->depth
* pf
->nb_channels
;
830 return (width
* bits
+ 7) >> 3;
832 case FF_PIXEL_PLANAR
:
833 if ((pix_fmt
!= PIX_FMT_NV12
&& pix_fmt
!= PIX_FMT_NV21
) &&
834 (plane
== 1 || plane
== 2))
835 width
= -((-width
)>>desc
->log2_chroma_w
);
837 return (width
* pf
->depth
+ 7) >> 3;
839 case FF_PIXEL_PALETTE
:
848 void av_picture_copy(AVPicture
*dst
, const AVPicture
*src
,
849 enum PixelFormat pix_fmt
, int width
, int height
)
852 const PixFmtInfo
*pf
= &pix_fmt_info
[pix_fmt
];
853 const AVPixFmtDescriptor
*desc
= &av_pix_fmt_descriptors
[pix_fmt
];
855 switch(pf
->pixel_type
) {
856 case FF_PIXEL_PACKED
:
857 case FF_PIXEL_PLANAR
:
858 for(i
= 0; i
< pf
->nb_channels
; i
++) {
860 int bwidth
= ff_get_plane_bytewidth(pix_fmt
, width
, i
);
862 if (i
== 1 || i
== 2) {
863 h
= -((-height
)>>desc
->log2_chroma_h
);
865 ff_img_copy_plane(dst
->data
[i
], dst
->linesize
[i
],
866 src
->data
[i
], src
->linesize
[i
],
870 case FF_PIXEL_PALETTE
:
871 ff_img_copy_plane(dst
->data
[0], dst
->linesize
[0],
872 src
->data
[0], src
->linesize
[0],
874 /* copy the palette */
875 memcpy(dst
->data
[1], src
->data
[1], 4*256);
881 void ff_shrink22(uint8_t *dst
, int dst_wrap
,
882 const uint8_t *src
, int src_wrap
,
883 int width
, int height
)
886 const uint8_t *s1
, *s2
;
889 for(;height
> 0; height
--) {
893 for(w
= width
;w
>= 4; w
-=4) {
894 d
[0] = (s1
[0] + s1
[1] + s2
[0] + s2
[1] + 2) >> 2;
895 d
[1] = (s1
[2] + s1
[3] + s2
[2] + s2
[3] + 2) >> 2;
896 d
[2] = (s1
[4] + s1
[5] + s2
[4] + s2
[5] + 2) >> 2;
897 d
[3] = (s1
[6] + s1
[7] + s2
[6] + s2
[7] + 2) >> 2;
903 d
[0] = (s1
[0] + s1
[1] + s2
[0] + s2
[1] + 2) >> 2;
914 void ff_shrink44(uint8_t *dst
, int dst_wrap
,
915 const uint8_t *src
, int src_wrap
,
916 int width
, int height
)
919 const uint8_t *s1
, *s2
, *s3
, *s4
;
922 for(;height
> 0; height
--) {
928 for(w
= width
;w
> 0; w
--) {
929 d
[0] = (s1
[0] + s1
[1] + s1
[2] + s1
[3] +
930 s2
[0] + s2
[1] + s2
[2] + s2
[3] +
931 s3
[0] + s3
[1] + s3
[2] + s3
[3] +
932 s4
[0] + s4
[1] + s4
[2] + s4
[3] + 8) >> 4;
945 void ff_shrink88(uint8_t *dst
, int dst_wrap
,
946 const uint8_t *src
, int src_wrap
,
947 int width
, int height
)
951 for(;height
> 0; height
--) {
952 for(w
= width
;w
> 0; w
--) {
955 tmp
+= src
[0] + src
[1] + src
[2] + src
[3] + src
[4] + src
[5] + src
[6] + src
[7];
958 *(dst
++) = (tmp
+ 32)>>6;
959 src
+= 8 - 8*src_wrap
;
961 src
+= 8*src_wrap
- 8*width
;
962 dst
+= dst_wrap
- width
;
967 int avpicture_alloc(AVPicture
*picture
,
968 enum PixelFormat pix_fmt
, int width
, int height
)
973 size
= avpicture_fill(picture
, NULL
, pix_fmt
, width
, height
);
976 ptr
= av_malloc(size
);
979 avpicture_fill(picture
, ptr
, pix_fmt
, width
, height
);
980 if(picture
->data
[1] && !picture
->data
[2])
981 ff_set_systematic_pal((uint32_t*)picture
->data
[1], pix_fmt
);
985 memset(picture
, 0, sizeof(AVPicture
));
989 void avpicture_free(AVPicture
*picture
)
991 av_free(picture
->data
[0]);
994 /* return true if yuv planar */
995 static inline int is_yuv_planar(const PixFmtInfo
*ps
)
997 return (ps
->color_type
== FF_COLOR_YUV
||
998 ps
->color_type
== FF_COLOR_YUV_JPEG
) &&
999 ps
->pixel_type
== FF_PIXEL_PLANAR
;
1002 int av_picture_crop(AVPicture
*dst
, const AVPicture
*src
,
1003 enum PixelFormat pix_fmt
, int top_band
, int left_band
)
1008 if (pix_fmt
< 0 || pix_fmt
>= PIX_FMT_NB
|| !is_yuv_planar(&pix_fmt_info
[pix_fmt
]))
1011 y_shift
= av_pix_fmt_descriptors
[pix_fmt
].log2_chroma_h
;
1012 x_shift
= av_pix_fmt_descriptors
[pix_fmt
].log2_chroma_w
;
1014 dst
->data
[0] = src
->data
[0] + (top_band
* src
->linesize
[0]) + left_band
;
1015 dst
->data
[1] = src
->data
[1] + ((top_band
>> y_shift
) * src
->linesize
[1]) + (left_band
>> x_shift
);
1016 dst
->data
[2] = src
->data
[2] + ((top_band
>> y_shift
) * src
->linesize
[2]) + (left_band
>> x_shift
);
1018 dst
->linesize
[0] = src
->linesize
[0];
1019 dst
->linesize
[1] = src
->linesize
[1];
1020 dst
->linesize
[2] = src
->linesize
[2];
1024 int av_picture_pad(AVPicture
*dst
, const AVPicture
*src
, int height
, int width
,
1025 enum PixelFormat pix_fmt
, int padtop
, int padbottom
, int padleft
, int padright
,
1034 if (pix_fmt
< 0 || pix_fmt
>= PIX_FMT_NB
||
1035 !is_yuv_planar(&pix_fmt_info
[pix_fmt
])) return -1;
1037 for (i
= 0; i
< 3; i
++) {
1038 x_shift
= i ? av_pix_fmt_descriptors
[pix_fmt
].log2_chroma_w
: 0;
1039 y_shift
= i ? av_pix_fmt_descriptors
[pix_fmt
].log2_chroma_h
: 0;
1041 if (padtop
|| padleft
) {
1042 memset(dst
->data
[i
], color
[i
],
1043 dst
->linesize
[i
] * (padtop
>> y_shift
) + (padleft
>> x_shift
));
1046 if (padleft
|| padright
) {
1047 optr
= dst
->data
[i
] + dst
->linesize
[i
] * (padtop
>> y_shift
) +
1048 (dst
->linesize
[i
] - (padright
>> x_shift
));
1049 yheight
= (height
- 1 - (padtop
+ padbottom
)) >> y_shift
;
1050 for (y
= 0; y
< yheight
; y
++) {
1051 memset(optr
, color
[i
], (padleft
+ padright
) >> x_shift
);
1052 optr
+= dst
->linesize
[i
];
1056 if (src
) { /* first line */
1057 uint8_t *iptr
= src
->data
[i
];
1058 optr
= dst
->data
[i
] + dst
->linesize
[i
] * (padtop
>> y_shift
) +
1059 (padleft
>> x_shift
);
1060 memcpy(optr
, iptr
, (width
- padleft
- padright
) >> x_shift
);
1061 iptr
+= src
->linesize
[i
];
1062 optr
= dst
->data
[i
] + dst
->linesize
[i
] * (padtop
>> y_shift
) +
1063 (dst
->linesize
[i
] - (padright
>> x_shift
));
1064 yheight
= (height
- 1 - (padtop
+ padbottom
)) >> y_shift
;
1065 for (y
= 0; y
< yheight
; y
++) {
1066 memset(optr
, color
[i
], (padleft
+ padright
) >> x_shift
);
1067 memcpy(optr
+ ((padleft
+ padright
) >> x_shift
), iptr
,
1068 (width
- padleft
- padright
) >> x_shift
);
1069 iptr
+= src
->linesize
[i
];
1070 optr
+= dst
->linesize
[i
];
1074 if (padbottom
|| padright
) {
1075 optr
= dst
->data
[i
] + dst
->linesize
[i
] *
1076 ((height
- padbottom
) >> y_shift
) - (padright
>> x_shift
);
1077 memset(optr
, color
[i
],dst
->linesize
[i
] *
1078 (padbottom
>> y_shift
) + (padright
>> x_shift
));
1084 /* NOTE: we scan all the pixels to have an exact information */
1085 static int get_alpha_info_pal8(const AVPicture
*src
, int width
, int height
)
1087 const unsigned char *p
;
1088 int src_wrap
, ret
, x
, y
;
1090 uint32_t *palette
= (uint32_t *)src
->data
[1];
1093 src_wrap
= src
->linesize
[0] - width
;
1095 for(y
=0;y
<height
;y
++) {
1096 for(x
=0;x
<width
;x
++) {
1097 a
= palette
[p
[0]] >> 24;
1099 ret
|= FF_ALPHA_TRANSP
;
1100 } else if (a
!= 0xff) {
1101 ret
|= FF_ALPHA_SEMI_TRANSP
;
1110 int img_get_alpha_info(const AVPicture
*src
,
1111 enum PixelFormat pix_fmt
, int width
, int height
)
1113 const PixFmtInfo
*pf
= &pix_fmt_info
[pix_fmt
];
1116 /* no alpha can be represented in format */
1121 ret
= get_alpha_info_pal8(src
, width
, height
);
1124 /* we do not know, so everything is indicated */
1125 ret
= FF_ALPHA_TRANSP
| FF_ALPHA_SEMI_TRANSP
;
1131 #if !(HAVE_MMX && HAVE_YASM)
1132 /* filter parameters: [-1 4 2 4 -1] // 8 */
1133 static void deinterlace_line_c(uint8_t *dst
,
1134 const uint8_t *lum_m4
, const uint8_t *lum_m3
,
1135 const uint8_t *lum_m2
, const uint8_t *lum_m1
,
1139 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
1142 for(;size
> 0;size
--) {
1144 sum
+= lum_m3
[0] << 2;
1145 sum
+= lum_m2
[0] << 1;
1146 sum
+= lum_m1
[0] << 2;
1148 dst
[0] = cm
[(sum
+ 4) >> 3];
1158 static void deinterlace_line_inplace_c(uint8_t *lum_m4
, uint8_t *lum_m3
,
1159 uint8_t *lum_m2
, uint8_t *lum_m1
,
1160 uint8_t *lum
, int size
)
1162 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
1165 for(;size
> 0;size
--) {
1167 sum
+= lum_m3
[0] << 2;
1168 sum
+= lum_m2
[0] << 1;
1169 lum_m4
[0]=lum_m2
[0];
1170 sum
+= lum_m1
[0] << 2;
1172 lum_m2
[0] = cm
[(sum
+ 4) >> 3];
1182 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1183 top field is copied as is, but the bottom field is deinterlaced
1184 against the top field. */
1185 static void deinterlace_bottom_field(uint8_t *dst
, int dst_wrap
,
1186 const uint8_t *src1
, int src_wrap
,
1187 int width
, int height
)
1189 const uint8_t *src_m2
, *src_m1
, *src_0
, *src_p1
, *src_p2
;
1194 src_0
=&src_m1
[src_wrap
];
1195 src_p1
=&src_0
[src_wrap
];
1196 src_p2
=&src_p1
[src_wrap
];
1197 for(y
=0;y
<(height
-2);y
+=2) {
1198 memcpy(dst
,src_m1
,width
);
1200 deinterlace_line(dst
,src_m2
,src_m1
,src_0
,src_p1
,src_p2
,width
);
1204 src_p1
+= 2*src_wrap
;
1205 src_p2
+= 2*src_wrap
;
1208 memcpy(dst
,src_m1
,width
);
1211 deinterlace_line(dst
,src_m2
,src_m1
,src_0
,src_0
,src_0
,width
);
1214 static void deinterlace_bottom_field_inplace(uint8_t *src1
, int src_wrap
,
1215 int width
, int height
)
1217 uint8_t *src_m1
, *src_0
, *src_p1
, *src_p2
;
1220 buf
= (uint8_t*)av_malloc(width
);
1223 memcpy(buf
,src_m1
,width
);
1224 src_0
=&src_m1
[src_wrap
];
1225 src_p1
=&src_0
[src_wrap
];
1226 src_p2
=&src_p1
[src_wrap
];
1227 for(y
=0;y
<(height
-2);y
+=2) {
1228 deinterlace_line_inplace(buf
,src_m1
,src_0
,src_p1
,src_p2
,width
);
1231 src_p1
+= 2*src_wrap
;
1232 src_p2
+= 2*src_wrap
;
1235 deinterlace_line_inplace(buf
,src_m1
,src_0
,src_0
,src_0
,width
);
1239 int avpicture_deinterlace(AVPicture
*dst
, const AVPicture
*src
,
1240 enum PixelFormat pix_fmt
, int width
, int height
)
1244 if (pix_fmt
!= PIX_FMT_YUV420P
&&
1245 pix_fmt
!= PIX_FMT_YUV422P
&&
1246 pix_fmt
!= PIX_FMT_YUV444P
&&
1247 pix_fmt
!= PIX_FMT_YUV411P
&&
1248 pix_fmt
!= PIX_FMT_GRAY8
)
1250 if ((width
& 3) != 0 || (height
& 3) != 0)
1256 case PIX_FMT_YUV420P
:
1260 case PIX_FMT_YUV422P
:
1263 case PIX_FMT_YUV411P
:
1269 if (pix_fmt
== PIX_FMT_GRAY8
) {
1274 deinterlace_bottom_field_inplace(dst
->data
[i
], dst
->linesize
[i
],
1277 deinterlace_bottom_field(dst
->data
[i
],dst
->linesize
[i
],
1278 src
->data
[i
], src
->linesize
[i
],