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
34 #include "imgconvert.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/colorspace.h"
39 #include "libavutil/common.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/imgutils.h"
43 #define FF_COLOR_NA -1
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 */
50 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
51 #define deinterlace_line ff_deinterlace_line_mmx
53 #define deinterlace_line_inplace deinterlace_line_inplace_c
54 #define deinterlace_line deinterlace_line_c
57 #define pixdesc_has_alpha(pixdesc) \
58 ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & AV_PIX_FMT_FLAG_PAL)
61 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
63 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
65 *h_shift = desc->log2_chroma_w;
66 *v_shift = desc->log2_chroma_h;
69 static int get_color_type(const AVPixFmtDescriptor *desc) {
70 if (desc->flags & AV_PIX_FMT_FLAG_PAL)
73 if(desc->nb_components == 1 || desc->nb_components == 2)
76 if(desc->name && !strncmp(desc->name, "yuvj", 4))
77 return FF_COLOR_YUV_JPEG;
79 if(desc->flags & AV_PIX_FMT_FLAG_RGB)
82 if(desc->nb_components == 0)
88 static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
90 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
93 if (!desc || !desc->nb_components) {
95 return AVERROR(EINVAL);
98 *min = INT_MAX, *max = -INT_MAX;
99 for (i = 0; i < desc->nb_components; i++) {
100 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
101 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
106 static int get_pix_fmt_score(enum AVPixelFormat dst_pix_fmt,
107 enum AVPixelFormat src_pix_fmt,
108 unsigned *lossp, unsigned consider)
110 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
111 const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
112 int src_color, dst_color;
113 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
114 int ret, loss, i, nb_components;
115 int score = INT_MAX - 1;
117 if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
123 if (dst_pix_fmt == src_pix_fmt)
126 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
128 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
131 src_color = get_color_type(src_desc);
132 dst_color = get_color_type(dst_desc);
133 nb_components = FFMIN(src_desc->nb_components, dst_desc->nb_components);
135 for (i = 0; i < nb_components; i++)
136 if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1 && (consider & FF_LOSS_DEPTH)) {
137 loss |= FF_LOSS_DEPTH;
138 score -= 65536 >> dst_desc->comp[i].depth_minus1;
141 if (consider & FF_LOSS_RESOLUTION) {
142 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w) {
143 loss |= FF_LOSS_RESOLUTION;
144 score -= 256 << dst_desc->log2_chroma_w;
146 if (dst_desc->log2_chroma_h > src_desc->log2_chroma_h) {
147 loss |= FF_LOSS_RESOLUTION;
148 score -= 256 << dst_desc->log2_chroma_h;
150 // don't favor 422 over 420 if downsampling is needed, because 420 has much better support on the decoder side
151 if (dst_desc->log2_chroma_w == 1 && src_desc->log2_chroma_w == 0 &&
152 dst_desc->log2_chroma_h == 1 && src_desc->log2_chroma_h == 0 ) {
157 if(consider & FF_LOSS_COLORSPACE)
160 if (src_color != FF_COLOR_RGB &&
161 src_color != FF_COLOR_GRAY)
162 loss |= FF_LOSS_COLORSPACE;
165 if (src_color != FF_COLOR_GRAY)
166 loss |= FF_LOSS_COLORSPACE;
169 if (src_color != FF_COLOR_YUV)
170 loss |= FF_LOSS_COLORSPACE;
172 case FF_COLOR_YUV_JPEG:
173 if (src_color != FF_COLOR_YUV_JPEG &&
174 src_color != FF_COLOR_YUV &&
175 src_color != FF_COLOR_GRAY)
176 loss |= FF_LOSS_COLORSPACE;
180 if (src_color != dst_color)
181 loss |= FF_LOSS_COLORSPACE;
184 if(loss & FF_LOSS_COLORSPACE)
185 score -= (nb_components * 65536) >> FFMIN(dst_desc->comp[0].depth_minus1, src_desc->comp[0].depth_minus1);
187 if (dst_color == FF_COLOR_GRAY &&
188 src_color != FF_COLOR_GRAY && (consider & FF_LOSS_CHROMA)) {
189 loss |= FF_LOSS_CHROMA;
192 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && (consider & FF_LOSS_ALPHA))) {
193 loss |= FF_LOSS_ALPHA;
196 if (dst_pix_fmt == AV_PIX_FMT_PAL8 && (consider & FF_LOSS_COLORQUANT) &&
197 (src_pix_fmt != AV_PIX_FMT_PAL8 && (src_color != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && (consider & FF_LOSS_ALPHA))))) {
198 loss |= FF_LOSS_COLORQUANT;
206 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
207 enum AVPixelFormat src_pix_fmt,
211 int ret = get_pix_fmt_score(dst_pix_fmt, src_pix_fmt, &loss, has_alpha ? ~0 : ~FF_LOSS_ALPHA);
217 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
218 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
220 enum AVPixelFormat dst_pix_fmt;
221 int loss1, loss2, loss_mask;
222 const AVPixFmtDescriptor *desc1 = av_pix_fmt_desc_get(dst_pix_fmt1);
223 const AVPixFmtDescriptor *desc2 = av_pix_fmt_desc_get(dst_pix_fmt2);
226 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
228 loss_mask &= ~FF_LOSS_ALPHA;
230 dst_pix_fmt = AV_PIX_FMT_NONE;
231 score1 = get_pix_fmt_score(dst_pix_fmt1, src_pix_fmt, &loss1, loss_mask);
232 score2 = get_pix_fmt_score(dst_pix_fmt2, src_pix_fmt, &loss2, loss_mask);
234 if (score1 == score2) {
235 if(av_get_padded_bits_per_pixel(desc2) != av_get_padded_bits_per_pixel(desc1)) {
236 dst_pix_fmt = av_get_padded_bits_per_pixel(desc2) < av_get_padded_bits_per_pixel(desc1) ? dst_pix_fmt2 : dst_pix_fmt1;
238 dst_pix_fmt = desc2->nb_components < desc1->nb_components ? dst_pix_fmt2 : dst_pix_fmt1;
241 dst_pix_fmt = score1 < score2 ? dst_pix_fmt2 : dst_pix_fmt1;
245 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
249 #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
250 enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
251 enum AVPixelFormat src_pix_fmt,
252 int has_alpha, int *loss_ptr){
253 return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
256 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
257 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
259 return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
263 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
264 enum AVPixelFormat src_pix_fmt,
265 int has_alpha, int *loss_ptr){
268 enum AVPixelFormat best = AV_PIX_FMT_NONE;
270 for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
271 best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
277 void ff_shrink22(uint8_t *dst, int dst_wrap,
278 const uint8_t *src, int src_wrap,
279 int width, int height)
282 const uint8_t *s1, *s2;
285 for(;height > 0; height--) {
289 for(w = width;w >= 4; w-=4) {
290 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
291 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
292 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
293 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
299 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
310 void ff_shrink44(uint8_t *dst, int dst_wrap,
311 const uint8_t *src, int src_wrap,
312 int width, int height)
315 const uint8_t *s1, *s2, *s3, *s4;
318 for(;height > 0; height--) {
324 for(w = width;w > 0; w--) {
325 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
326 s2[0] + s2[1] + s2[2] + s2[3] +
327 s3[0] + s3[1] + s3[2] + s3[3] +
328 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
341 void ff_shrink88(uint8_t *dst, int dst_wrap,
342 const uint8_t *src, int src_wrap,
343 int width, int height)
347 for(;height > 0; height--) {
348 for(w = width;w > 0; w--) {
351 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
354 *(dst++) = (tmp + 32)>>6;
355 src += 8 - 8*src_wrap;
357 src += 8*src_wrap - 8*width;
358 dst += dst_wrap - width;
362 /* return true if yuv planar */
363 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
366 int planes[4] = { 0 };
368 if ( desc->flags & AV_PIX_FMT_FLAG_RGB
369 || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
372 /* set the used planes */
373 for (i = 0; i < desc->nb_components; i++)
374 planes[desc->comp[i].plane] = 1;
376 /* if there is an unused plane, the format is not planar */
377 for (i = 0; i < desc->nb_components; i++)
383 int av_picture_crop(AVPicture *dst, const AVPicture *src,
384 enum AVPixelFormat pix_fmt, int top_band, int left_band)
386 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
390 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
393 y_shift = desc->log2_chroma_h;
394 x_shift = desc->log2_chroma_w;
396 if (is_yuv_planar(desc)) {
397 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
398 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
399 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
401 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
403 if(left_band) //FIXME add support for this too
405 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
408 dst->linesize[0] = src->linesize[0];
409 dst->linesize[1] = src->linesize[1];
410 dst->linesize[2] = src->linesize[2];
414 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
415 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
418 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
425 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
426 !is_yuv_planar(desc)) return -1;
428 for (i = 0; i < 3; i++) {
429 x_shift = i ? desc->log2_chroma_w : 0;
430 y_shift = i ? desc->log2_chroma_h : 0;
432 if (padtop || padleft) {
433 memset(dst->data[i], color[i],
434 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
437 if (padleft || padright) {
438 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
439 (dst->linesize[i] - (padright >> x_shift));
440 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
441 for (y = 0; y < yheight; y++) {
442 memset(optr, color[i], (padleft + padright) >> x_shift);
443 optr += dst->linesize[i];
447 if (src) { /* first line */
448 uint8_t *iptr = src->data[i];
449 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
450 (padleft >> x_shift);
451 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
452 iptr += src->linesize[i];
453 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
454 (dst->linesize[i] - (padright >> x_shift));
455 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
456 for (y = 0; y < yheight; y++) {
457 memset(optr, color[i], (padleft + padright) >> x_shift);
458 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
459 (width - padleft - padright) >> x_shift);
460 iptr += src->linesize[i];
461 optr += dst->linesize[i];
465 if (padbottom || padright) {
466 optr = dst->data[i] + dst->linesize[i] *
467 ((height - padbottom) >> y_shift) - (padright >> x_shift);
468 memset(optr, color[i],dst->linesize[i] *
469 (padbottom >> y_shift) + (padright >> x_shift));
475 #if FF_API_DEINTERLACE
477 #if !HAVE_MMX_EXTERNAL
478 /* filter parameters: [-1 4 2 4 -1] // 8 */
479 static void deinterlace_line_c(uint8_t *dst,
480 const uint8_t *lum_m4, const uint8_t *lum_m3,
481 const uint8_t *lum_m2, const uint8_t *lum_m1,
485 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
488 for(;size > 0;size--) {
490 sum += lum_m3[0] << 2;
491 sum += lum_m2[0] << 1;
492 sum += lum_m1[0] << 2;
494 dst[0] = cm[(sum + 4) >> 3];
504 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
505 uint8_t *lum_m2, uint8_t *lum_m1,
506 uint8_t *lum, int size)
508 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
511 for(;size > 0;size--) {
513 sum += lum_m3[0] << 2;
514 sum += lum_m2[0] << 1;
516 sum += lum_m1[0] << 2;
518 lum_m2[0] = cm[(sum + 4) >> 3];
526 #endif /* !HAVE_MMX_EXTERNAL */
528 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
529 top field is copied as is, but the bottom field is deinterlaced
530 against the top field. */
531 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
532 const uint8_t *src1, int src_wrap,
533 int width, int height)
535 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
540 src_0=&src_m1[src_wrap];
541 src_p1=&src_0[src_wrap];
542 src_p2=&src_p1[src_wrap];
543 for(y=0;y<(height-2);y+=2) {
544 memcpy(dst,src_m1,width);
546 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
550 src_p1 += 2*src_wrap;
551 src_p2 += 2*src_wrap;
554 memcpy(dst,src_m1,width);
557 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
560 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
561 int width, int height)
563 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
566 buf = av_malloc(width);
569 memcpy(buf,src_m1,width);
570 src_0=&src_m1[src_wrap];
571 src_p1=&src_0[src_wrap];
572 src_p2=&src_p1[src_wrap];
573 for(y=0;y<(height-2);y+=2) {
574 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
577 src_p1 += 2*src_wrap;
578 src_p2 += 2*src_wrap;
581 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
585 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
586 enum AVPixelFormat pix_fmt, int width, int height)
590 if (pix_fmt != AV_PIX_FMT_YUV420P &&
591 pix_fmt != AV_PIX_FMT_YUVJ420P &&
592 pix_fmt != AV_PIX_FMT_YUV422P &&
593 pix_fmt != AV_PIX_FMT_YUVJ422P &&
594 pix_fmt != AV_PIX_FMT_YUV444P &&
595 pix_fmt != AV_PIX_FMT_YUV411P &&
596 pix_fmt != AV_PIX_FMT_GRAY8)
598 if ((width & 3) != 0 || (height & 3) != 0)
604 case AV_PIX_FMT_YUVJ420P:
605 case AV_PIX_FMT_YUV420P:
609 case AV_PIX_FMT_YUV422P:
610 case AV_PIX_FMT_YUVJ422P:
613 case AV_PIX_FMT_YUV411P:
619 if (pix_fmt == AV_PIX_FMT_GRAY8) {
624 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
627 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
628 src->data[i], src->linesize[i],
636 #endif /* FF_API_DEINTERLACE */
645 for (i=0; i<AV_PIX_FMT_NB*2; i++) {
646 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
647 if(!desc || !desc->name) {
652 av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
655 av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d colortype:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc), get_color_type(desc));
656 if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
657 av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");