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"
43 #include "x86/dsputil_mmx.h"
46 #define xglue(x, y) x ## y
47 #define glue(x, y) xglue(x, y)
49 #define FF_COLOR_RGB 0 /**< RGB color space */
50 #define FF_COLOR_GRAY 1 /**< gray color space */
51 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
52 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
54 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
55 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
56 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
58 typedef struct PixFmtInfo {
59 uint8_t nb_channels; /**< number of channels (including alpha) */
60 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
61 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
62 uint8_t is_alpha : 1; /**< true if alpha can be specified */
63 uint8_t depth; /**< bit depth of the color components */
66 /* this table gives more information about formats */
67 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
71 .color_type = FF_COLOR_YUV,
72 .pixel_type = FF_PIXEL_PLANAR,
77 .color_type = FF_COLOR_YUV,
78 .pixel_type = FF_PIXEL_PLANAR,
83 .color_type = FF_COLOR_YUV,
84 .pixel_type = FF_PIXEL_PLANAR,
89 .color_type = FF_COLOR_YUV,
90 .pixel_type = FF_PIXEL_PACKED,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
101 .color_type = FF_COLOR_YUV,
102 .pixel_type = FF_PIXEL_PLANAR,
105 [PIX_FMT_YUV411P] = {
107 .color_type = FF_COLOR_YUV,
108 .pixel_type = FF_PIXEL_PLANAR,
111 [PIX_FMT_YUV440P] = {
113 .color_type = FF_COLOR_YUV,
114 .pixel_type = FF_PIXEL_PLANAR,
117 [PIX_FMT_YUV420P16LE] = {
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
123 [PIX_FMT_YUV422P16LE] = {
125 .color_type = FF_COLOR_YUV,
126 .pixel_type = FF_PIXEL_PLANAR,
129 [PIX_FMT_YUV444P16LE] = {
131 .color_type = FF_COLOR_YUV,
132 .pixel_type = FF_PIXEL_PLANAR,
135 [PIX_FMT_YUV420P16BE] = {
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
141 [PIX_FMT_YUV422P16BE] = {
143 .color_type = FF_COLOR_YUV,
144 .pixel_type = FF_PIXEL_PLANAR,
147 [PIX_FMT_YUV444P16BE] = {
149 .color_type = FF_COLOR_YUV,
150 .pixel_type = FF_PIXEL_PLANAR,
155 /* YUV formats with alpha plane */
156 [PIX_FMT_YUVA420P] = {
158 .color_type = FF_COLOR_YUV,
159 .pixel_type = FF_PIXEL_PLANAR,
164 [PIX_FMT_YUVJ420P] = {
166 .color_type = FF_COLOR_YUV_JPEG,
167 .pixel_type = FF_PIXEL_PLANAR,
170 [PIX_FMT_YUVJ422P] = {
172 .color_type = FF_COLOR_YUV_JPEG,
173 .pixel_type = FF_PIXEL_PLANAR,
176 [PIX_FMT_YUVJ444P] = {
178 .color_type = FF_COLOR_YUV_JPEG,
179 .pixel_type = FF_PIXEL_PLANAR,
182 [PIX_FMT_YUVJ440P] = {
184 .color_type = FF_COLOR_YUV_JPEG,
185 .pixel_type = FF_PIXEL_PLANAR,
192 .color_type = FF_COLOR_RGB,
193 .pixel_type = FF_PIXEL_PACKED,
198 .color_type = FF_COLOR_RGB,
199 .pixel_type = FF_PIXEL_PACKED,
203 .nb_channels = 4, .is_alpha = 1,
204 .color_type = FF_COLOR_RGB,
205 .pixel_type = FF_PIXEL_PACKED,
208 [PIX_FMT_RGB48BE] = {
210 .color_type = FF_COLOR_RGB,
211 .pixel_type = FF_PIXEL_PACKED,
214 [PIX_FMT_RGB48LE] = {
216 .color_type = FF_COLOR_RGB,
217 .pixel_type = FF_PIXEL_PACKED,
220 [PIX_FMT_RGB565BE] = {
222 .color_type = FF_COLOR_RGB,
223 .pixel_type = FF_PIXEL_PACKED,
226 [PIX_FMT_RGB565LE] = {
228 .color_type = FF_COLOR_RGB,
229 .pixel_type = FF_PIXEL_PACKED,
232 [PIX_FMT_RGB555BE] = {
234 .color_type = FF_COLOR_RGB,
235 .pixel_type = FF_PIXEL_PACKED,
238 [PIX_FMT_RGB555LE] = {
240 .color_type = FF_COLOR_RGB,
241 .pixel_type = FF_PIXEL_PACKED,
244 [PIX_FMT_RGB444BE] = {
246 .color_type = FF_COLOR_RGB,
247 .pixel_type = FF_PIXEL_PACKED,
250 [PIX_FMT_RGB444LE] = {
252 .color_type = FF_COLOR_RGB,
253 .pixel_type = FF_PIXEL_PACKED,
257 /* gray / mono formats */
258 [PIX_FMT_GRAY16BE] = {
260 .color_type = FF_COLOR_GRAY,
261 .pixel_type = FF_PIXEL_PLANAR,
264 [PIX_FMT_GRAY16LE] = {
266 .color_type = FF_COLOR_GRAY,
267 .pixel_type = FF_PIXEL_PLANAR,
272 .color_type = FF_COLOR_GRAY,
273 .pixel_type = FF_PIXEL_PLANAR,
276 [PIX_FMT_MONOWHITE] = {
278 .color_type = FF_COLOR_GRAY,
279 .pixel_type = FF_PIXEL_PLANAR,
282 [PIX_FMT_MONOBLACK] = {
284 .color_type = FF_COLOR_GRAY,
285 .pixel_type = FF_PIXEL_PLANAR,
289 /* paletted formats */
291 .nb_channels = 4, .is_alpha = 1,
292 .color_type = FF_COLOR_RGB,
293 .pixel_type = FF_PIXEL_PALETTE,
296 [PIX_FMT_UYYVYY411] = {
298 .color_type = FF_COLOR_YUV,
299 .pixel_type = FF_PIXEL_PACKED,
303 .nb_channels = 4, .is_alpha = 1,
304 .color_type = FF_COLOR_RGB,
305 .pixel_type = FF_PIXEL_PACKED,
308 [PIX_FMT_BGR565BE] = {
310 .color_type = FF_COLOR_RGB,
311 .pixel_type = FF_PIXEL_PACKED,
314 [PIX_FMT_BGR565LE] = {
316 .color_type = FF_COLOR_RGB,
317 .pixel_type = FF_PIXEL_PACKED,
320 [PIX_FMT_BGR555BE] = {
322 .color_type = FF_COLOR_RGB,
323 .pixel_type = FF_PIXEL_PACKED,
326 [PIX_FMT_BGR555LE] = {
328 .color_type = FF_COLOR_RGB,
329 .pixel_type = FF_PIXEL_PACKED,
332 [PIX_FMT_BGR444BE] = {
334 .color_type = FF_COLOR_RGB,
335 .pixel_type = FF_PIXEL_PACKED,
338 [PIX_FMT_BGR444LE] = {
340 .color_type = FF_COLOR_RGB,
341 .pixel_type = FF_PIXEL_PACKED,
346 .color_type = FF_COLOR_RGB,
347 .pixel_type = FF_PIXEL_PACKED,
352 .color_type = FF_COLOR_RGB,
353 .pixel_type = FF_PIXEL_PACKED,
356 [PIX_FMT_RGB4_BYTE] = {
358 .color_type = FF_COLOR_RGB,
359 .pixel_type = FF_PIXEL_PACKED,
364 .color_type = FF_COLOR_RGB,
365 .pixel_type = FF_PIXEL_PACKED,
370 .color_type = FF_COLOR_RGB,
371 .pixel_type = FF_PIXEL_PACKED,
374 [PIX_FMT_BGR4_BYTE] = {
376 .color_type = FF_COLOR_RGB,
377 .pixel_type = FF_PIXEL_PACKED,
382 .color_type = FF_COLOR_YUV,
383 .pixel_type = FF_PIXEL_PLANAR,
388 .color_type = FF_COLOR_YUV,
389 .pixel_type = FF_PIXEL_PLANAR,
394 .nb_channels = 4, .is_alpha = 1,
395 .color_type = FF_COLOR_RGB,
396 .pixel_type = FF_PIXEL_PACKED,
400 .nb_channels = 4, .is_alpha = 1,
401 .color_type = FF_COLOR_RGB,
402 .pixel_type = FF_PIXEL_PACKED,
407 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
409 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
410 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
413 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
415 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
418 return av_pix_fmt_descriptors[pix_fmt].name;
421 #if LIBAVCODEC_VERSION_MAJOR < 53
422 enum PixelFormat avcodec_get_pix_fmt(const char *name)
424 return av_get_pix_fmt(name);
428 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
432 snprintf (buf, buf_size,
433 "name " " nb_channels" " depth" " is_alpha"
436 PixFmtInfo info= pix_fmt_info[pix_fmt];
438 char is_alpha_char= info.is_alpha ? 'y' : 'n';
440 snprintf (buf, buf_size,
442 av_pix_fmt_descriptors[pix_fmt].name,
450 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
452 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
455 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
458 for(i=0; i<256; i++){
472 case PIX_FMT_RGB4_BYTE:
477 case PIX_FMT_BGR4_BYTE:
488 pal[i] = b + (g<<8) + (r<<16);
494 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
496 return av_fill_image_linesizes(picture->linesize, pix_fmt, width);
499 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
502 return av_fill_image_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
505 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
506 enum PixelFormat pix_fmt, int width, int height)
509 if(avcodec_check_dimensions(NULL, width, height))
512 if (av_fill_image_linesizes(picture->linesize, pix_fmt, width))
515 return av_fill_image_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
518 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
519 unsigned char *dest, int dest_size)
521 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
522 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
523 int i, j, w, ow, h, oh, data_planes;
524 const unsigned char* s;
525 int size = avpicture_get_size(pix_fmt, width, height);
527 if (size > dest_size || size < 0)
530 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
531 if (pix_fmt == PIX_FMT_YUYV422 ||
532 pix_fmt == PIX_FMT_UYVY422 ||
533 pix_fmt == PIX_FMT_BGR565BE ||
534 pix_fmt == PIX_FMT_BGR565LE ||
535 pix_fmt == PIX_FMT_BGR555BE ||
536 pix_fmt == PIX_FMT_BGR555LE ||
537 pix_fmt == PIX_FMT_BGR444BE ||
538 pix_fmt == PIX_FMT_BGR444LE ||
539 pix_fmt == PIX_FMT_RGB565BE ||
540 pix_fmt == PIX_FMT_RGB565LE ||
541 pix_fmt == PIX_FMT_RGB555BE ||
542 pix_fmt == PIX_FMT_RGB555LE ||
543 pix_fmt == PIX_FMT_RGB444BE ||
544 pix_fmt == PIX_FMT_RGB444LE)
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;
564 for (i=0; i<data_planes; i++) {
566 w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
567 h = -((-height) >> desc->log2_chroma_h);
568 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
578 s += src->linesize[i];
582 if (pf->pixel_type == FF_PIXEL_PALETTE)
583 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
588 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
590 AVPicture dummy_pict;
591 if(avcodec_check_dimensions(NULL, width, height))
596 case PIX_FMT_RGB4_BYTE:
597 case PIX_FMT_BGR4_BYTE:
599 // do not include palette for these pseudo-paletted formats
600 return width * height;
602 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
605 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
608 const PixFmtInfo *pf, *ps;
609 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
610 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
613 ps = &pix_fmt_info[src_pix_fmt];
617 pf = &pix_fmt_info[dst_pix_fmt];
618 if (pf->depth < ps->depth ||
619 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
620 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
621 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
622 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
623 loss |= FF_LOSS_DEPTH;
624 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
625 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
626 loss |= FF_LOSS_RESOLUTION;
627 switch(pf->color_type) {
629 if (ps->color_type != FF_COLOR_RGB &&
630 ps->color_type != FF_COLOR_GRAY)
631 loss |= FF_LOSS_COLORSPACE;
634 if (ps->color_type != FF_COLOR_GRAY)
635 loss |= FF_LOSS_COLORSPACE;
638 if (ps->color_type != FF_COLOR_YUV)
639 loss |= FF_LOSS_COLORSPACE;
641 case FF_COLOR_YUV_JPEG:
642 if (ps->color_type != FF_COLOR_YUV_JPEG &&
643 ps->color_type != FF_COLOR_YUV &&
644 ps->color_type != FF_COLOR_GRAY)
645 loss |= FF_LOSS_COLORSPACE;
649 if (ps->color_type != pf->color_type)
650 loss |= FF_LOSS_COLORSPACE;
653 if (pf->color_type == FF_COLOR_GRAY &&
654 ps->color_type != FF_COLOR_GRAY)
655 loss |= FF_LOSS_CHROMA;
656 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
657 loss |= FF_LOSS_ALPHA;
658 if (pf->pixel_type == FF_PIXEL_PALETTE &&
659 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
660 loss |= FF_LOSS_COLORQUANT;
664 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
667 const PixFmtInfo *pf;
668 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
670 pf = &pix_fmt_info[pix_fmt];
671 switch(pf->pixel_type) {
672 case FF_PIXEL_PACKED:
674 case PIX_FMT_YUYV422:
675 case PIX_FMT_UYVY422:
676 case PIX_FMT_RGB565BE:
677 case PIX_FMT_RGB565LE:
678 case PIX_FMT_RGB555BE:
679 case PIX_FMT_RGB555LE:
680 case PIX_FMT_RGB444BE:
681 case PIX_FMT_RGB444LE:
682 case PIX_FMT_BGR565BE:
683 case PIX_FMT_BGR565LE:
684 case PIX_FMT_BGR555BE:
685 case PIX_FMT_BGR555LE:
686 case PIX_FMT_BGR444BE:
687 case PIX_FMT_BGR444LE:
690 case PIX_FMT_UYYVYY411:
694 bits = pf->depth * pf->nb_channels;
698 case FF_PIXEL_PLANAR:
699 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
700 bits = pf->depth * pf->nb_channels;
702 bits = pf->depth + ((2 * pf->depth) >>
703 (desc->log2_chroma_w + desc->log2_chroma_h));
706 case FF_PIXEL_PALETTE:
716 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
717 enum PixelFormat src_pix_fmt,
721 int dist, i, loss, min_dist;
722 enum PixelFormat dst_pix_fmt;
724 /* find exact color match with smallest size */
725 dst_pix_fmt = PIX_FMT_NONE;
726 min_dist = 0x7fffffff;
727 for(i = 0;i < PIX_FMT_NB; i++) {
728 if (pix_fmt_mask & (1ULL << i)) {
729 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
731 dist = avg_bits_per_pixel(i);
732 if (dist < min_dist) {
742 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
743 int has_alpha, int *loss_ptr)
745 enum PixelFormat dst_pix_fmt;
747 static const int loss_mask_order[] = {
748 ~0, /* no loss first */
751 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
757 /* try with successive loss */
760 loss_mask = loss_mask_order[i++];
761 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
762 has_alpha, loss_mask);
763 if (dst_pix_fmt >= 0)
771 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
775 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
776 const uint8_t *src, int src_wrap,
777 int width, int height)
781 for(;height > 0; height--) {
782 memcpy(dst, src, width);
788 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
791 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
792 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
794 pf = &pix_fmt_info[pix_fmt];
795 switch(pf->pixel_type) {
796 case FF_PIXEL_PACKED:
798 case PIX_FMT_YUYV422:
799 case PIX_FMT_UYVY422:
800 case PIX_FMT_RGB565BE:
801 case PIX_FMT_RGB565LE:
802 case PIX_FMT_RGB555BE:
803 case PIX_FMT_RGB555LE:
804 case PIX_FMT_RGB444BE:
805 case PIX_FMT_RGB444LE:
806 case PIX_FMT_BGR565BE:
807 case PIX_FMT_BGR565LE:
808 case PIX_FMT_BGR555BE:
809 case PIX_FMT_BGR555LE:
810 case PIX_FMT_BGR444BE:
811 case PIX_FMT_BGR444LE:
814 case PIX_FMT_UYYVYY411:
818 bits = pf->depth * pf->nb_channels;
821 return (width * bits + 7) >> 3;
823 case FF_PIXEL_PLANAR:
824 if ((pix_fmt != PIX_FMT_NV12 && pix_fmt != PIX_FMT_NV21) &&
825 (plane == 1 || plane == 2))
826 width= -((-width)>>desc->log2_chroma_w);
828 return (width * pf->depth + 7) >> 3;
830 case FF_PIXEL_PALETTE:
839 void av_picture_copy(AVPicture *dst, const AVPicture *src,
840 enum PixelFormat pix_fmt, int width, int height)
843 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
844 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
846 switch(pf->pixel_type) {
847 case FF_PIXEL_PACKED:
848 case FF_PIXEL_PLANAR:
849 for(i = 0; i < pf->nb_channels; i++) {
851 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
853 if (i == 1 || i == 2) {
854 h= -((-height)>>desc->log2_chroma_h);
856 ff_img_copy_plane(dst->data[i], dst->linesize[i],
857 src->data[i], src->linesize[i],
861 case FF_PIXEL_PALETTE:
862 ff_img_copy_plane(dst->data[0], dst->linesize[0],
863 src->data[0], src->linesize[0],
865 /* copy the palette */
866 memcpy(dst->data[1], src->data[1], 4*256);
872 void ff_shrink22(uint8_t *dst, int dst_wrap,
873 const uint8_t *src, int src_wrap,
874 int width, int height)
877 const uint8_t *s1, *s2;
880 for(;height > 0; height--) {
884 for(w = width;w >= 4; w-=4) {
885 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
886 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
887 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
888 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
894 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
905 void ff_shrink44(uint8_t *dst, int dst_wrap,
906 const uint8_t *src, int src_wrap,
907 int width, int height)
910 const uint8_t *s1, *s2, *s3, *s4;
913 for(;height > 0; height--) {
919 for(w = width;w > 0; w--) {
920 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
921 s2[0] + s2[1] + s2[2] + s2[3] +
922 s3[0] + s3[1] + s3[2] + s3[3] +
923 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
936 void ff_shrink88(uint8_t *dst, int dst_wrap,
937 const uint8_t *src, int src_wrap,
938 int width, int height)
942 for(;height > 0; height--) {
943 for(w = width;w > 0; w--) {
946 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
949 *(dst++) = (tmp + 32)>>6;
950 src += 8 - 8*src_wrap;
952 src += 8*src_wrap - 8*width;
953 dst += dst_wrap - width;
958 int avpicture_alloc(AVPicture *picture,
959 enum PixelFormat pix_fmt, int width, int height)
964 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
967 ptr = av_malloc(size);
970 avpicture_fill(picture, ptr, pix_fmt, width, height);
971 if(picture->data[1] && !picture->data[2])
972 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
976 memset(picture, 0, sizeof(AVPicture));
980 void avpicture_free(AVPicture *picture)
982 av_free(picture->data[0]);
985 /* return true if yuv planar */
986 static inline int is_yuv_planar(const PixFmtInfo *ps)
988 return (ps->color_type == FF_COLOR_YUV ||
989 ps->color_type == FF_COLOR_YUV_JPEG) &&
990 ps->pixel_type == FF_PIXEL_PLANAR;
993 int av_picture_crop(AVPicture *dst, const AVPicture *src,
994 enum PixelFormat pix_fmt, int top_band, int left_band)
999 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1002 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1003 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1005 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1006 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1007 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1009 dst->linesize[0] = src->linesize[0];
1010 dst->linesize[1] = src->linesize[1];
1011 dst->linesize[2] = src->linesize[2];
1015 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1016 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1025 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1026 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1028 for (i = 0; i < 3; i++) {
1029 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1030 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1032 if (padtop || padleft) {
1033 memset(dst->data[i], color[i],
1034 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1037 if (padleft || padright) {
1038 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1039 (dst->linesize[i] - (padright >> x_shift));
1040 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1041 for (y = 0; y < yheight; y++) {
1042 memset(optr, color[i], (padleft + padright) >> x_shift);
1043 optr += dst->linesize[i];
1047 if (src) { /* first line */
1048 uint8_t *iptr = src->data[i];
1049 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1050 (padleft >> x_shift);
1051 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1052 iptr += src->linesize[i];
1053 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1054 (dst->linesize[i] - (padright >> x_shift));
1055 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1056 for (y = 0; y < yheight; y++) {
1057 memset(optr, color[i], (padleft + padright) >> x_shift);
1058 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1059 (width - padleft - padright) >> x_shift);
1060 iptr += src->linesize[i];
1061 optr += dst->linesize[i];
1065 if (padbottom || padright) {
1066 optr = dst->data[i] + dst->linesize[i] *
1067 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1068 memset(optr, color[i],dst->linesize[i] *
1069 (padbottom >> y_shift) + (padright >> x_shift));
1075 /* NOTE: we scan all the pixels to have an exact information */
1076 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1078 const unsigned char *p;
1079 int src_wrap, ret, x, y;
1081 uint32_t *palette = (uint32_t *)src->data[1];
1084 src_wrap = src->linesize[0] - width;
1086 for(y=0;y<height;y++) {
1087 for(x=0;x<width;x++) {
1088 a = palette[p[0]] >> 24;
1090 ret |= FF_ALPHA_TRANSP;
1091 } else if (a != 0xff) {
1092 ret |= FF_ALPHA_SEMI_TRANSP;
1101 int img_get_alpha_info(const AVPicture *src,
1102 enum PixelFormat pix_fmt, int width, int height)
1104 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1107 /* no alpha can be represented in format */
1112 ret = get_alpha_info_pal8(src, width, height);
1115 /* we do not know, so everything is indicated */
1116 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1123 #define DEINT_INPLACE_LINE_LUM \
1124 movd_m2r(lum_m4[0],mm0);\
1125 movd_m2r(lum_m3[0],mm1);\
1126 movd_m2r(lum_m2[0],mm2);\
1127 movd_m2r(lum_m1[0],mm3);\
1128 movd_m2r(lum[0],mm4);\
1129 punpcklbw_r2r(mm7,mm0);\
1130 movd_r2m(mm2,lum_m4[0]);\
1131 punpcklbw_r2r(mm7,mm1);\
1132 punpcklbw_r2r(mm7,mm2);\
1133 punpcklbw_r2r(mm7,mm3);\
1134 punpcklbw_r2r(mm7,mm4);\
1135 paddw_r2r(mm3,mm1);\
1137 paddw_r2r(mm4,mm0);\
1139 paddw_r2r(mm6,mm2);\
1140 paddw_r2r(mm2,mm1);\
1141 psubusw_r2r(mm0,mm1);\
1143 packuswb_r2r(mm7,mm1);\
1144 movd_r2m(mm1,lum_m2[0]);
1146 #define DEINT_LINE_LUM \
1147 movd_m2r(lum_m4[0],mm0);\
1148 movd_m2r(lum_m3[0],mm1);\
1149 movd_m2r(lum_m2[0],mm2);\
1150 movd_m2r(lum_m1[0],mm3);\
1151 movd_m2r(lum[0],mm4);\
1152 punpcklbw_r2r(mm7,mm0);\
1153 punpcklbw_r2r(mm7,mm1);\
1154 punpcklbw_r2r(mm7,mm2);\
1155 punpcklbw_r2r(mm7,mm3);\
1156 punpcklbw_r2r(mm7,mm4);\
1157 paddw_r2r(mm3,mm1);\
1159 paddw_r2r(mm4,mm0);\
1161 paddw_r2r(mm6,mm2);\
1162 paddw_r2r(mm2,mm1);\
1163 psubusw_r2r(mm0,mm1);\
1165 packuswb_r2r(mm7,mm1);\
1166 movd_r2m(mm1,dst[0]);
1169 /* filter parameters: [-1 4 2 4 -1] // 8 */
1170 static void deinterlace_line(uint8_t *dst,
1171 const uint8_t *lum_m4, const uint8_t *lum_m3,
1172 const uint8_t *lum_m2, const uint8_t *lum_m1,
1177 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1180 for(;size > 0;size--) {
1182 sum += lum_m3[0] << 2;
1183 sum += lum_m2[0] << 1;
1184 sum += lum_m1[0] << 2;
1186 dst[0] = cm[(sum + 4) >> 3];
1198 movq_m2r(ff_pw_4,mm6);
1200 for (;size > 3; size-=4) {
1211 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1215 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1218 for(;size > 0;size--) {
1220 sum += lum_m3[0] << 2;
1221 sum += lum_m2[0] << 1;
1222 lum_m4[0]=lum_m2[0];
1223 sum += lum_m1[0] << 2;
1225 lum_m2[0] = cm[(sum + 4) >> 3];
1236 movq_m2r(ff_pw_4,mm6);
1238 for (;size > 3; size-=4) {
1239 DEINT_INPLACE_LINE_LUM
1249 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1250 top field is copied as is, but the bottom field is deinterlaced
1251 against the top field. */
1252 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1253 const uint8_t *src1, int src_wrap,
1254 int width, int height)
1256 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1261 src_0=&src_m1[src_wrap];
1262 src_p1=&src_0[src_wrap];
1263 src_p2=&src_p1[src_wrap];
1264 for(y=0;y<(height-2);y+=2) {
1265 memcpy(dst,src_m1,width);
1267 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1271 src_p1 += 2*src_wrap;
1272 src_p2 += 2*src_wrap;
1275 memcpy(dst,src_m1,width);
1278 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1281 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1282 int width, int height)
1284 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1287 buf = (uint8_t*)av_malloc(width);
1290 memcpy(buf,src_m1,width);
1291 src_0=&src_m1[src_wrap];
1292 src_p1=&src_0[src_wrap];
1293 src_p2=&src_p1[src_wrap];
1294 for(y=0;y<(height-2);y+=2) {
1295 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1298 src_p1 += 2*src_wrap;
1299 src_p2 += 2*src_wrap;
1302 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1306 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1307 enum PixelFormat pix_fmt, int width, int height)
1311 if (pix_fmt != PIX_FMT_YUV420P &&
1312 pix_fmt != PIX_FMT_YUV422P &&
1313 pix_fmt != PIX_FMT_YUV444P &&
1314 pix_fmt != PIX_FMT_YUV411P &&
1315 pix_fmt != PIX_FMT_GRAY8)
1317 if ((width & 3) != 0 || (height & 3) != 0)
1323 case PIX_FMT_YUV420P:
1327 case PIX_FMT_YUV422P:
1330 case PIX_FMT_YUV411P:
1336 if (pix_fmt == PIX_FMT_GRAY8) {
1341 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1344 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1345 src->data[i], src->linesize[i],