lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / imgconvert.c
blob8f8b6e39143674936b6209002bf5436d0cb80c81
1 /*
2 * Misc image conversion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 /**
23 * @file
24 * misc image conversion routines
27 /* TODO:
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
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "imgconvert.h"
36 #include "internal.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
42 #if HAVE_MMX_EXTERNAL
43 #include "x86/dsputil_mmx.h"
44 #endif
46 #if HAVE_MMX_EXTERNAL
47 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
48 #define deinterlace_line ff_deinterlace_line_mmx
49 #else
50 #define deinterlace_line_inplace deinterlace_line_inplace_c
51 #define deinterlace_line deinterlace_line_c
52 #endif
54 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
56 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
57 *h_shift = desc->log2_chroma_w;
58 *v_shift = desc->log2_chroma_h;
61 static int is_gray(const AVPixFmtDescriptor *desc)
63 return desc->nb_components - (desc->flags & PIX_FMT_ALPHA) == 1;
66 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
67 enum AVPixelFormat src_pix_fmt,
68 int has_alpha)
70 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
71 const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
72 int loss, i, nb_components = FFMIN(src_desc->nb_components,
73 dst_desc->nb_components);
75 /* compute loss */
76 loss = 0;
78 if (dst_pix_fmt == src_pix_fmt)
79 return 0;
81 for (i = 0; i < nb_components; i++)
82 if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1)
83 loss |= FF_LOSS_DEPTH;
85 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
86 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
87 loss |= FF_LOSS_RESOLUTION;
89 if ((src_desc->flags & PIX_FMT_RGB) != (dst_desc->flags & PIX_FMT_RGB))
90 loss |= FF_LOSS_COLORSPACE;
92 if (has_alpha && !(dst_desc->flags & PIX_FMT_ALPHA) &&
93 (dst_desc->flags & PIX_FMT_ALPHA))
94 loss |= FF_LOSS_ALPHA;
96 if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
97 return loss | FF_LOSS_COLORQUANT;
99 if (src_desc->nb_components > dst_desc->nb_components)
100 if (is_gray(dst_desc))
101 loss |= FF_LOSS_CHROMA;
103 return loss;
106 static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
107 enum AVPixelFormat src_pix_fmt,
108 int has_alpha,
109 int loss_mask)
111 int dist, i, loss, min_dist;
112 enum AVPixelFormat dst_pix_fmt;
114 /* find exact color match with smallest size */
115 dst_pix_fmt = AV_PIX_FMT_NONE;
116 min_dist = 0x7fffffff;
117 i = 0;
118 while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
119 enum AVPixelFormat pix_fmt = pix_fmt_list[i];
121 if (i > AV_PIX_FMT_NB) {
122 av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
123 "it is either not properly terminated or contains duplicates\n");
124 return AV_PIX_FMT_NONE;
127 loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
128 if (loss == 0) {
129 dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
130 if (dist < min_dist) {
131 min_dist = dist;
132 dst_pix_fmt = pix_fmt;
135 i++;
137 return dst_pix_fmt;
140 #if FF_API_FIND_BEST_PIX_FMT
141 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
142 int has_alpha, int *loss_ptr)
144 enum AVPixelFormat list[64];
145 int i, j = 0;
147 // test only the first 64 pixel formats to avoid undefined behaviour
148 for (i = 0; i < 64; i++) {
149 if (pix_fmt_mask & (1ULL << i))
150 list[j++] = i;
152 list[j] = AV_PIX_FMT_NONE;
154 return avcodec_find_best_pix_fmt2(list, src_pix_fmt, has_alpha, loss_ptr);
156 #endif /* FF_API_FIND_BEST_PIX_FMT */
158 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
159 enum AVPixelFormat src_pix_fmt,
160 int has_alpha, int *loss_ptr)
162 enum AVPixelFormat dst_pix_fmt;
163 int loss_mask, i;
164 static const int loss_mask_order[] = {
165 ~0, /* no loss first */
166 ~FF_LOSS_ALPHA,
167 ~FF_LOSS_RESOLUTION,
168 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
169 ~FF_LOSS_COLORQUANT,
170 ~FF_LOSS_DEPTH,
174 /* try with successive loss */
175 i = 0;
176 for(;;) {
177 loss_mask = loss_mask_order[i++];
178 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
179 has_alpha, loss_mask);
180 if (dst_pix_fmt >= 0)
181 goto found;
182 if (loss_mask == 0)
183 break;
185 return AV_PIX_FMT_NONE;
186 found:
187 if (loss_ptr)
188 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
189 return dst_pix_fmt;
192 /* 2x2 -> 1x1 */
193 void ff_shrink22(uint8_t *dst, int dst_wrap,
194 const uint8_t *src, int src_wrap,
195 int width, int height)
197 int w;
198 const uint8_t *s1, *s2;
199 uint8_t *d;
201 for(;height > 0; height--) {
202 s1 = src;
203 s2 = s1 + src_wrap;
204 d = dst;
205 for(w = width;w >= 4; w-=4) {
206 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
207 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
208 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
209 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
210 s1 += 8;
211 s2 += 8;
212 d += 4;
214 for(;w > 0; w--) {
215 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
216 s1 += 2;
217 s2 += 2;
218 d++;
220 src += 2 * src_wrap;
221 dst += dst_wrap;
225 /* 4x4 -> 1x1 */
226 void ff_shrink44(uint8_t *dst, int dst_wrap,
227 const uint8_t *src, int src_wrap,
228 int width, int height)
230 int w;
231 const uint8_t *s1, *s2, *s3, *s4;
232 uint8_t *d;
234 for(;height > 0; height--) {
235 s1 = src;
236 s2 = s1 + src_wrap;
237 s3 = s2 + src_wrap;
238 s4 = s3 + src_wrap;
239 d = dst;
240 for(w = width;w > 0; w--) {
241 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
242 s2[0] + s2[1] + s2[2] + s2[3] +
243 s3[0] + s3[1] + s3[2] + s3[3] +
244 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
245 s1 += 4;
246 s2 += 4;
247 s3 += 4;
248 s4 += 4;
249 d++;
251 src += 4 * src_wrap;
252 dst += dst_wrap;
256 /* 8x8 -> 1x1 */
257 void ff_shrink88(uint8_t *dst, int dst_wrap,
258 const uint8_t *src, int src_wrap,
259 int width, int height)
261 int w, i;
263 for(;height > 0; height--) {
264 for(w = width;w > 0; w--) {
265 int tmp=0;
266 for(i=0; i<8; i++){
267 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
268 src += src_wrap;
270 *(dst++) = (tmp + 32)>>6;
271 src += 8 - 8*src_wrap;
273 src += 8*src_wrap - 8*width;
274 dst += dst_wrap - width;
278 /* return true if yuv planar */
279 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
281 return (!(desc->flags & PIX_FMT_RGB) &&
282 (desc->flags & PIX_FMT_PLANAR));
285 int av_picture_crop(AVPicture *dst, const AVPicture *src,
286 enum AVPixelFormat pix_fmt, int top_band, int left_band)
288 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
289 int y_shift;
290 int x_shift;
292 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
293 return -1;
295 y_shift = desc->log2_chroma_h;
296 x_shift = desc->log2_chroma_w;
298 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
299 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
300 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
302 dst->linesize[0] = src->linesize[0];
303 dst->linesize[1] = src->linesize[1];
304 dst->linesize[2] = src->linesize[2];
305 return 0;
308 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
309 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
310 int *color)
312 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
313 uint8_t *optr;
314 int y_shift;
315 int x_shift;
316 int yheight;
317 int i, y;
319 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
320 !is_yuv_planar(desc)) return -1;
322 for (i = 0; i < 3; i++) {
323 x_shift = i ? desc->log2_chroma_w : 0;
324 y_shift = i ? desc->log2_chroma_h : 0;
326 if (padtop || padleft) {
327 memset(dst->data[i], color[i],
328 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
331 if (padleft || padright) {
332 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
333 (dst->linesize[i] - (padright >> x_shift));
334 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
335 for (y = 0; y < yheight; y++) {
336 memset(optr, color[i], (padleft + padright) >> x_shift);
337 optr += dst->linesize[i];
341 if (src) { /* first line */
342 uint8_t *iptr = src->data[i];
343 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
344 (padleft >> x_shift);
345 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
346 iptr += src->linesize[i];
347 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
348 (dst->linesize[i] - (padright >> x_shift));
349 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
350 for (y = 0; y < yheight; y++) {
351 memset(optr, color[i], (padleft + padright) >> x_shift);
352 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
353 (width - padleft - padright) >> x_shift);
354 iptr += src->linesize[i];
355 optr += dst->linesize[i];
359 if (padbottom || padright) {
360 optr = dst->data[i] + dst->linesize[i] *
361 ((height - padbottom) >> y_shift) - (padright >> x_shift);
362 memset(optr, color[i],dst->linesize[i] *
363 (padbottom >> y_shift) + (padright >> x_shift));
366 return 0;
369 #if FF_API_DEINTERLACE
371 #if !HAVE_MMX_EXTERNAL
372 /* filter parameters: [-1 4 2 4 -1] // 8 */
373 static void deinterlace_line_c(uint8_t *dst,
374 const uint8_t *lum_m4, const uint8_t *lum_m3,
375 const uint8_t *lum_m2, const uint8_t *lum_m1,
376 const uint8_t *lum,
377 int size)
379 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
380 int sum;
382 for(;size > 0;size--) {
383 sum = -lum_m4[0];
384 sum += lum_m3[0] << 2;
385 sum += lum_m2[0] << 1;
386 sum += lum_m1[0] << 2;
387 sum += -lum[0];
388 dst[0] = cm[(sum + 4) >> 3];
389 lum_m4++;
390 lum_m3++;
391 lum_m2++;
392 lum_m1++;
393 lum++;
394 dst++;
398 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
399 uint8_t *lum_m2, uint8_t *lum_m1,
400 uint8_t *lum, int size)
402 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
403 int sum;
405 for(;size > 0;size--) {
406 sum = -lum_m4[0];
407 sum += lum_m3[0] << 2;
408 sum += lum_m2[0] << 1;
409 lum_m4[0]=lum_m2[0];
410 sum += lum_m1[0] << 2;
411 sum += -lum[0];
412 lum_m2[0] = cm[(sum + 4) >> 3];
413 lum_m4++;
414 lum_m3++;
415 lum_m2++;
416 lum_m1++;
417 lum++;
420 #endif /* !HAVE_MMX_EXTERNAL */
422 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
423 top field is copied as is, but the bottom field is deinterlaced
424 against the top field. */
425 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
426 const uint8_t *src1, int src_wrap,
427 int width, int height)
429 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
430 int y;
432 src_m2 = src1;
433 src_m1 = src1;
434 src_0=&src_m1[src_wrap];
435 src_p1=&src_0[src_wrap];
436 src_p2=&src_p1[src_wrap];
437 for(y=0;y<(height-2);y+=2) {
438 memcpy(dst,src_m1,width);
439 dst += dst_wrap;
440 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
441 src_m2 = src_0;
442 src_m1 = src_p1;
443 src_0 = src_p2;
444 src_p1 += 2*src_wrap;
445 src_p2 += 2*src_wrap;
446 dst += dst_wrap;
448 memcpy(dst,src_m1,width);
449 dst += dst_wrap;
450 /* do last line */
451 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
454 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
455 int width, int height)
457 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
458 int y;
459 uint8_t *buf;
460 buf = av_malloc(width);
462 src_m1 = src1;
463 memcpy(buf,src_m1,width);
464 src_0=&src_m1[src_wrap];
465 src_p1=&src_0[src_wrap];
466 src_p2=&src_p1[src_wrap];
467 for(y=0;y<(height-2);y+=2) {
468 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
469 src_m1 = src_p1;
470 src_0 = src_p2;
471 src_p1 += 2*src_wrap;
472 src_p2 += 2*src_wrap;
474 /* do last line */
475 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
476 av_free(buf);
479 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
480 enum AVPixelFormat pix_fmt, int width, int height)
482 int i;
484 if (pix_fmt != AV_PIX_FMT_YUV420P &&
485 pix_fmt != AV_PIX_FMT_YUVJ420P &&
486 pix_fmt != AV_PIX_FMT_YUV422P &&
487 pix_fmt != AV_PIX_FMT_YUVJ422P &&
488 pix_fmt != AV_PIX_FMT_YUV444P &&
489 pix_fmt != AV_PIX_FMT_YUV411P &&
490 pix_fmt != AV_PIX_FMT_GRAY8)
491 return -1;
492 if ((width & 3) != 0 || (height & 3) != 0)
493 return -1;
495 for(i=0;i<3;i++) {
496 if (i == 1) {
497 switch(pix_fmt) {
498 case AV_PIX_FMT_YUVJ420P:
499 case AV_PIX_FMT_YUV420P:
500 width >>= 1;
501 height >>= 1;
502 break;
503 case AV_PIX_FMT_YUV422P:
504 case AV_PIX_FMT_YUVJ422P:
505 width >>= 1;
506 break;
507 case AV_PIX_FMT_YUV411P:
508 width >>= 2;
509 break;
510 default:
511 break;
513 if (pix_fmt == AV_PIX_FMT_GRAY8) {
514 break;
517 if (src == dst) {
518 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
519 width, height);
520 } else {
521 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
522 src->data[i], src->linesize[i],
523 width, height);
526 emms_c();
527 return 0;
530 #endif /* FF_API_DEINTERLACE */