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 "libavutil/avassert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
43 #include "x86/dsputil_mmx.h"
46 #define FF_COLOR_NA -1
47 #define FF_COLOR_RGB 0 /**< RGB color space */
48 #define FF_COLOR_GRAY 1 /**< gray color space */
49 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
50 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
53 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
54 #define deinterlace_line ff_deinterlace_line_mmx
56 #define deinterlace_line_inplace deinterlace_line_inplace_c
57 #define deinterlace_line deinterlace_line_c
60 #define pixdesc_has_alpha(pixdesc) \
61 ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
64 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
66 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
68 *h_shift = desc->log2_chroma_w;
69 *v_shift = desc->log2_chroma_h;
72 static get_color_type(AVPixFmtDescriptor *desc) {
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 & PIX_FMT_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 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
107 enum AVPixelFormat src_pix_fmt,
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;
116 if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
122 if (dst_pix_fmt == src_pix_fmt)
125 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
127 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
130 src_color = get_color_type(src_desc);
131 dst_color = get_color_type(dst_desc);
132 nb_components = FFMIN(src_desc->nb_components, dst_desc->nb_components);
134 for (i = 0; i < nb_components; i++)
135 if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1)
136 loss |= FF_LOSS_DEPTH;
138 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
139 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
140 loss |= FF_LOSS_RESOLUTION;
144 if (src_color != FF_COLOR_RGB &&
145 src_color != FF_COLOR_GRAY)
146 loss |= FF_LOSS_COLORSPACE;
149 if (src_color != FF_COLOR_GRAY)
150 loss |= FF_LOSS_COLORSPACE;
153 if (src_color != FF_COLOR_YUV)
154 loss |= FF_LOSS_COLORSPACE;
156 case FF_COLOR_YUV_JPEG:
157 if (src_color != FF_COLOR_YUV_JPEG &&
158 src_color != FF_COLOR_YUV &&
159 src_color != FF_COLOR_GRAY)
160 loss |= FF_LOSS_COLORSPACE;
164 if (src_color != dst_color)
165 loss |= FF_LOSS_COLORSPACE;
168 if (dst_color == FF_COLOR_GRAY &&
169 src_color != FF_COLOR_GRAY)
170 loss |= FF_LOSS_CHROMA;
171 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
172 loss |= FF_LOSS_ALPHA;
173 if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
174 (src_pix_fmt != AV_PIX_FMT_PAL8 && (src_color != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
175 loss |= FF_LOSS_COLORQUANT;
180 #if FF_API_FIND_BEST_PIX_FMT
181 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
182 int has_alpha, int *loss_ptr)
184 enum AVPixelFormat dst_pix_fmt;
187 if (loss_ptr) /* all losses count (for backward compatibility) */
190 dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
191 for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
192 if (pix_fmt_mask & (1ULL << i))
193 dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
197 #endif /* FF_API_FIND_BEST_PIX_FMT */
199 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
200 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
202 enum AVPixelFormat dst_pix_fmt;
203 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
204 const AVPixFmtDescriptor *desc1 = av_pix_fmt_desc_get(dst_pix_fmt1);
205 const AVPixFmtDescriptor *desc2 = av_pix_fmt_desc_get(dst_pix_fmt2);
206 static const int loss_mask_order[] = {
207 ~0, /* no loss first */
211 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
214 ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
215 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
216 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
217 0x80000, //non zero entry that combines all loss variants including future additions
221 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
222 dst_pix_fmt = AV_PIX_FMT_NONE;
223 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
224 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
226 /* try with successive loss */
227 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
228 loss_order1 = loss1 & loss_mask_order[i];
229 loss_order2 = loss2 & loss_mask_order[i];
231 if (loss_order1 == 0 && loss_order2 == 0 && dst_pix_fmt2 != AV_PIX_FMT_NONE && dst_pix_fmt1 != AV_PIX_FMT_NONE){ /* use format with smallest depth */
232 if(av_get_padded_bits_per_pixel(desc2) != av_get_padded_bits_per_pixel(desc1)) {
233 dst_pix_fmt = av_get_padded_bits_per_pixel(desc2) < av_get_padded_bits_per_pixel(desc1) ? dst_pix_fmt2 : dst_pix_fmt1;
235 dst_pix_fmt = desc2->nb_components < desc1->nb_components ? dst_pix_fmt2 : dst_pix_fmt1;
237 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
238 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
243 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
247 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
248 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
249 enum AVPixelFormat src_pix_fmt,
250 int has_alpha, int *loss_ptr){
251 return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
254 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
255 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
257 return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
261 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
262 enum AVPixelFormat src_pix_fmt,
263 int has_alpha, int *loss_ptr){
266 enum AVPixelFormat best = AV_PIX_FMT_NONE;
268 for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
269 best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
275 void ff_shrink22(uint8_t *dst, int dst_wrap,
276 const uint8_t *src, int src_wrap,
277 int width, int height)
280 const uint8_t *s1, *s2;
283 for(;height > 0; height--) {
287 for(w = width;w >= 4; w-=4) {
288 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
289 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
290 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
291 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
297 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
308 void ff_shrink44(uint8_t *dst, int dst_wrap,
309 const uint8_t *src, int src_wrap,
310 int width, int height)
313 const uint8_t *s1, *s2, *s3, *s4;
316 for(;height > 0; height--) {
322 for(w = width;w > 0; w--) {
323 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
324 s2[0] + s2[1] + s2[2] + s2[3] +
325 s3[0] + s3[1] + s3[2] + s3[3] +
326 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
339 void ff_shrink88(uint8_t *dst, int dst_wrap,
340 const uint8_t *src, int src_wrap,
341 int width, int height)
345 for(;height > 0; height--) {
346 for(w = width;w > 0; w--) {
349 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
352 *(dst++) = (tmp + 32)>>6;
353 src += 8 - 8*src_wrap;
355 src += 8*src_wrap - 8*width;
356 dst += dst_wrap - width;
360 /* return true if yuv planar */
361 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
364 int planes[4] = { 0 };
366 if ( desc->flags & PIX_FMT_RGB
367 || !(desc->flags & PIX_FMT_PLANAR))
370 /* set the used planes */
371 for (i = 0; i < desc->nb_components; i++)
372 planes[desc->comp[i].plane] = 1;
374 /* if there is an unused plane, the format is not planar */
375 for (i = 0; i < desc->nb_components; i++)
381 int av_picture_crop(AVPicture *dst, const AVPicture *src,
382 enum AVPixelFormat pix_fmt, int top_band, int left_band)
384 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
388 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
391 y_shift = desc->log2_chroma_h;
392 x_shift = desc->log2_chroma_w;
394 if (is_yuv_planar(desc)) {
395 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
396 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
397 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
399 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
401 if(left_band) //FIXME add support for this too
403 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
406 dst->linesize[0] = src->linesize[0];
407 dst->linesize[1] = src->linesize[1];
408 dst->linesize[2] = src->linesize[2];
412 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
413 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
416 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
423 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
424 !is_yuv_planar(desc)) return -1;
426 for (i = 0; i < 3; i++) {
427 x_shift = i ? desc->log2_chroma_w : 0;
428 y_shift = i ? desc->log2_chroma_h : 0;
430 if (padtop || padleft) {
431 memset(dst->data[i], color[i],
432 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
435 if (padleft || padright) {
436 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
437 (dst->linesize[i] - (padright >> x_shift));
438 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
439 for (y = 0; y < yheight; y++) {
440 memset(optr, color[i], (padleft + padright) >> x_shift);
441 optr += dst->linesize[i];
445 if (src) { /* first line */
446 uint8_t *iptr = src->data[i];
447 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
448 (padleft >> x_shift);
449 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
450 iptr += src->linesize[i];
451 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
452 (dst->linesize[i] - (padright >> x_shift));
453 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
454 for (y = 0; y < yheight; y++) {
455 memset(optr, color[i], (padleft + padright) >> x_shift);
456 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
457 (width - padleft - padright) >> x_shift);
458 iptr += src->linesize[i];
459 optr += dst->linesize[i];
463 if (padbottom || padright) {
464 optr = dst->data[i] + dst->linesize[i] *
465 ((height - padbottom) >> y_shift) - (padright >> x_shift);
466 memset(optr, color[i],dst->linesize[i] *
467 (padbottom >> y_shift) + (padright >> x_shift));
473 #if !HAVE_MMX_EXTERNAL
474 /* filter parameters: [-1 4 2 4 -1] // 8 */
475 static void deinterlace_line_c(uint8_t *dst,
476 const uint8_t *lum_m4, const uint8_t *lum_m3,
477 const uint8_t *lum_m2, const uint8_t *lum_m1,
481 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
484 for(;size > 0;size--) {
486 sum += lum_m3[0] << 2;
487 sum += lum_m2[0] << 1;
488 sum += lum_m1[0] << 2;
490 dst[0] = cm[(sum + 4) >> 3];
500 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
501 uint8_t *lum_m2, uint8_t *lum_m1,
502 uint8_t *lum, int size)
504 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
507 for(;size > 0;size--) {
509 sum += lum_m3[0] << 2;
510 sum += lum_m2[0] << 1;
512 sum += lum_m1[0] << 2;
514 lum_m2[0] = cm[(sum + 4) >> 3];
522 #endif /* !HAVE_MMX_EXTERNAL */
524 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
525 top field is copied as is, but the bottom field is deinterlaced
526 against the top field. */
527 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
528 const uint8_t *src1, int src_wrap,
529 int width, int height)
531 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
536 src_0=&src_m1[src_wrap];
537 src_p1=&src_0[src_wrap];
538 src_p2=&src_p1[src_wrap];
539 for(y=0;y<(height-2);y+=2) {
540 memcpy(dst,src_m1,width);
542 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
546 src_p1 += 2*src_wrap;
547 src_p2 += 2*src_wrap;
550 memcpy(dst,src_m1,width);
553 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
556 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
557 int width, int height)
559 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
562 buf = av_malloc(width);
565 memcpy(buf,src_m1,width);
566 src_0=&src_m1[src_wrap];
567 src_p1=&src_0[src_wrap];
568 src_p2=&src_p1[src_wrap];
569 for(y=0;y<(height-2);y+=2) {
570 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
573 src_p1 += 2*src_wrap;
574 src_p2 += 2*src_wrap;
577 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
581 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
582 enum AVPixelFormat pix_fmt, int width, int height)
586 if (pix_fmt != AV_PIX_FMT_YUV420P &&
587 pix_fmt != AV_PIX_FMT_YUVJ420P &&
588 pix_fmt != AV_PIX_FMT_YUV422P &&
589 pix_fmt != AV_PIX_FMT_YUVJ422P &&
590 pix_fmt != AV_PIX_FMT_YUV444P &&
591 pix_fmt != AV_PIX_FMT_YUV411P &&
592 pix_fmt != AV_PIX_FMT_GRAY8)
594 if ((width & 3) != 0 || (height & 3) != 0)
600 case AV_PIX_FMT_YUVJ420P:
601 case AV_PIX_FMT_YUV420P:
605 case AV_PIX_FMT_YUV422P:
606 case AV_PIX_FMT_YUVJ422P:
609 case AV_PIX_FMT_YUV411P:
615 if (pix_fmt == AV_PIX_FMT_GRAY8) {
620 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
623 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
624 src->data[i], src->linesize[i],
639 for (i=0; i<AV_PIX_FMT_NB*2; i++) {
640 AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
641 if(!desc || !desc->name) {
646 av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
649 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));
650 if ((!(desc->flags & PIX_FMT_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
651 av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");