]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
dsputil: Move ff_zigzag_direct and ff_crop_tab declarations to mathops.h
[ffmpeg] / libavcodec / imgconvert.c
1 /*
2  * Misc image conversion routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /**
23  * @file
24  * misc image conversion routines
25  */
26
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
31  */
32
33 #include "avcodec.h"
34 #include "imgconvert.h"
35 #include "internal.h"
36 #include "mathops.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
41
42 #if HAVE_MMX_EXTERNAL
43 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
44 #define deinterlace_line         ff_deinterlace_line_mmx
45 #else
46 #define deinterlace_line_inplace deinterlace_line_inplace_c
47 #define deinterlace_line         deinterlace_line_c
48 #endif
49
50 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
51 {
52     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
53     *h_shift = desc->log2_chroma_w;
54     *v_shift = desc->log2_chroma_h;
55 }
56
57 static int is_gray(const AVPixFmtDescriptor *desc)
58 {
59     return desc->nb_components - (desc->flags & AV_PIX_FMT_FLAG_ALPHA) == 1;
60 }
61
62 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
63                              enum AVPixelFormat src_pix_fmt,
64                              int has_alpha)
65 {
66     const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
67     const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
68     int loss, i, nb_components = FFMIN(src_desc->nb_components,
69                                        dst_desc->nb_components);
70
71     /* compute loss */
72     loss = 0;
73
74     if (dst_pix_fmt == src_pix_fmt)
75         return 0;
76
77     for (i = 0; i < nb_components; i++)
78         if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1)
79             loss |= FF_LOSS_DEPTH;
80
81     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
82         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
83         loss |= FF_LOSS_RESOLUTION;
84
85     if ((src_desc->flags & AV_PIX_FMT_FLAG_RGB) != (dst_desc->flags & AV_PIX_FMT_FLAG_RGB))
86         loss |= FF_LOSS_COLORSPACE;
87
88     if (has_alpha && !(dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA) &&
89          (dst_desc->flags & AV_PIX_FMT_FLAG_ALPHA))
90         loss |= FF_LOSS_ALPHA;
91
92     if (dst_pix_fmt == AV_PIX_FMT_PAL8 && !is_gray(src_desc))
93         return loss | FF_LOSS_COLORQUANT;
94
95     if (src_desc->nb_components > dst_desc->nb_components)
96         if (is_gray(dst_desc))
97             loss |= FF_LOSS_CHROMA;
98
99     return loss;
100 }
101
102 static enum AVPixelFormat avcodec_find_best_pix_fmt1(enum AVPixelFormat *pix_fmt_list,
103                                       enum AVPixelFormat src_pix_fmt,
104                                       int has_alpha,
105                                       int loss_mask)
106 {
107     int dist, i, loss, min_dist;
108     enum AVPixelFormat dst_pix_fmt;
109
110     /* find exact color match with smallest size */
111     dst_pix_fmt = AV_PIX_FMT_NONE;
112     min_dist = 0x7fffffff;
113     i = 0;
114     while (pix_fmt_list[i] != AV_PIX_FMT_NONE) {
115         enum AVPixelFormat pix_fmt = pix_fmt_list[i];
116
117         if (i > AV_PIX_FMT_NB) {
118             av_log(NULL, AV_LOG_ERROR, "Pixel format list longer than expected, "
119                    "it is either not properly terminated or contains duplicates\n");
120             return AV_PIX_FMT_NONE;
121         }
122
123         loss = avcodec_get_pix_fmt_loss(pix_fmt, src_pix_fmt, has_alpha) & loss_mask;
124         if (loss == 0) {
125             dist = av_get_bits_per_pixel(av_pix_fmt_desc_get(pix_fmt));
126             if (dist < min_dist) {
127                 min_dist = dist;
128                 dst_pix_fmt = pix_fmt;
129             }
130         }
131         i++;
132     }
133     return dst_pix_fmt;
134 }
135
136 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
137                                             enum AVPixelFormat src_pix_fmt,
138                                             int has_alpha, int *loss_ptr)
139 {
140     enum AVPixelFormat dst_pix_fmt;
141     int loss_mask, i;
142     static const int loss_mask_order[] = {
143         ~0, /* no loss first */
144         ~FF_LOSS_ALPHA,
145         ~FF_LOSS_RESOLUTION,
146         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
147         ~FF_LOSS_COLORQUANT,
148         ~FF_LOSS_DEPTH,
149         0,
150     };
151
152     /* try with successive loss */
153     i = 0;
154     for(;;) {
155         loss_mask = loss_mask_order[i++];
156         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_list, src_pix_fmt,
157                                                  has_alpha, loss_mask);
158         if (dst_pix_fmt >= 0)
159             goto found;
160         if (loss_mask == 0)
161             break;
162     }
163     return AV_PIX_FMT_NONE;
164  found:
165     if (loss_ptr)
166         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
167     return dst_pix_fmt;
168 }
169
170 /* 2x2 -> 1x1 */
171 void ff_shrink22(uint8_t *dst, int dst_wrap,
172                      const uint8_t *src, int src_wrap,
173                      int width, int height)
174 {
175     int w;
176     const uint8_t *s1, *s2;
177     uint8_t *d;
178
179     for(;height > 0; height--) {
180         s1 = src;
181         s2 = s1 + src_wrap;
182         d = dst;
183         for(w = width;w >= 4; w-=4) {
184             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
185             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
186             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
187             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
188             s1 += 8;
189             s2 += 8;
190             d += 4;
191         }
192         for(;w > 0; w--) {
193             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
194             s1 += 2;
195             s2 += 2;
196             d++;
197         }
198         src += 2 * src_wrap;
199         dst += dst_wrap;
200     }
201 }
202
203 /* 4x4 -> 1x1 */
204 void ff_shrink44(uint8_t *dst, int dst_wrap,
205                      const uint8_t *src, int src_wrap,
206                      int width, int height)
207 {
208     int w;
209     const uint8_t *s1, *s2, *s3, *s4;
210     uint8_t *d;
211
212     for(;height > 0; height--) {
213         s1 = src;
214         s2 = s1 + src_wrap;
215         s3 = s2 + src_wrap;
216         s4 = s3 + src_wrap;
217         d = dst;
218         for(w = width;w > 0; w--) {
219             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
220                     s2[0] + s2[1] + s2[2] + s2[3] +
221                     s3[0] + s3[1] + s3[2] + s3[3] +
222                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
223             s1 += 4;
224             s2 += 4;
225             s3 += 4;
226             s4 += 4;
227             d++;
228         }
229         src += 4 * src_wrap;
230         dst += dst_wrap;
231     }
232 }
233
234 /* 8x8 -> 1x1 */
235 void ff_shrink88(uint8_t *dst, int dst_wrap,
236                      const uint8_t *src, int src_wrap,
237                      int width, int height)
238 {
239     int w, i;
240
241     for(;height > 0; height--) {
242         for(w = width;w > 0; w--) {
243             int tmp=0;
244             for(i=0; i<8; i++){
245                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
246                 src += src_wrap;
247             }
248             *(dst++) = (tmp + 32)>>6;
249             src += 8 - 8*src_wrap;
250         }
251         src += 8*src_wrap - 8*width;
252         dst += dst_wrap - width;
253     }
254 }
255
256 /* return true if yuv planar */
257 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
258 {
259     return (!(desc->flags & AV_PIX_FMT_FLAG_RGB) &&
260              (desc->flags & AV_PIX_FMT_FLAG_PLANAR));
261 }
262
263 int av_picture_crop(AVPicture *dst, const AVPicture *src,
264                     enum AVPixelFormat pix_fmt, int top_band, int left_band)
265 {
266     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
267     int y_shift;
268     int x_shift;
269
270     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB || !is_yuv_planar(desc))
271         return -1;
272
273     y_shift = desc->log2_chroma_h;
274     x_shift = desc->log2_chroma_w;
275
276     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
277     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
278     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
279
280     dst->linesize[0] = src->linesize[0];
281     dst->linesize[1] = src->linesize[1];
282     dst->linesize[2] = src->linesize[2];
283     return 0;
284 }
285
286 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
287                    enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
288             int *color)
289 {
290     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
291     uint8_t *optr;
292     int y_shift;
293     int x_shift;
294     int yheight;
295     int i, y;
296
297     if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
298         !is_yuv_planar(desc)) return -1;
299
300     for (i = 0; i < 3; i++) {
301         x_shift = i ? desc->log2_chroma_w : 0;
302         y_shift = i ? desc->log2_chroma_h : 0;
303
304         if (padtop || padleft) {
305             memset(dst->data[i], color[i],
306                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
307         }
308
309         if (padleft || padright) {
310             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
311                 (dst->linesize[i] - (padright >> x_shift));
312             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
313             for (y = 0; y < yheight; y++) {
314                 memset(optr, color[i], (padleft + padright) >> x_shift);
315                 optr += dst->linesize[i];
316             }
317         }
318
319         if (src) { /* first line */
320             uint8_t *iptr = src->data[i];
321             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
322                     (padleft >> x_shift);
323             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
324             iptr += src->linesize[i];
325             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
326                 (dst->linesize[i] - (padright >> x_shift));
327             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
328             for (y = 0; y < yheight; y++) {
329                 memset(optr, color[i], (padleft + padright) >> x_shift);
330                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
331                        (width - padleft - padright) >> x_shift);
332                 iptr += src->linesize[i];
333                 optr += dst->linesize[i];
334             }
335         }
336
337         if (padbottom || padright) {
338             optr = dst->data[i] + dst->linesize[i] *
339                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
340             memset(optr, color[i],dst->linesize[i] *
341                 (padbottom >> y_shift) + (padright >> x_shift));
342         }
343     }
344     return 0;
345 }
346
347 #if FF_API_DEINTERLACE
348
349 #if !HAVE_MMX_EXTERNAL
350 /* filter parameters: [-1 4 2 4 -1] // 8 */
351 static void deinterlace_line_c(uint8_t *dst,
352                              const uint8_t *lum_m4, const uint8_t *lum_m3,
353                              const uint8_t *lum_m2, const uint8_t *lum_m1,
354                              const uint8_t *lum,
355                              int size)
356 {
357     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
358     int sum;
359
360     for(;size > 0;size--) {
361         sum = -lum_m4[0];
362         sum += lum_m3[0] << 2;
363         sum += lum_m2[0] << 1;
364         sum += lum_m1[0] << 2;
365         sum += -lum[0];
366         dst[0] = cm[(sum + 4) >> 3];
367         lum_m4++;
368         lum_m3++;
369         lum_m2++;
370         lum_m1++;
371         lum++;
372         dst++;
373     }
374 }
375
376 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
377                                        uint8_t *lum_m2, uint8_t *lum_m1,
378                                        uint8_t *lum, int size)
379 {
380     const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
381     int sum;
382
383     for(;size > 0;size--) {
384         sum = -lum_m4[0];
385         sum += lum_m3[0] << 2;
386         sum += lum_m2[0] << 1;
387         lum_m4[0]=lum_m2[0];
388         sum += lum_m1[0] << 2;
389         sum += -lum[0];
390         lum_m2[0] = cm[(sum + 4) >> 3];
391         lum_m4++;
392         lum_m3++;
393         lum_m2++;
394         lum_m1++;
395         lum++;
396     }
397 }
398 #endif /* !HAVE_MMX_EXTERNAL */
399
400 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
401    top field is copied as is, but the bottom field is deinterlaced
402    against the top field. */
403 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
404                                     const uint8_t *src1, int src_wrap,
405                                     int width, int height)
406 {
407     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
408     int y;
409
410     src_m2 = src1;
411     src_m1 = src1;
412     src_0=&src_m1[src_wrap];
413     src_p1=&src_0[src_wrap];
414     src_p2=&src_p1[src_wrap];
415     for(y=0;y<(height-2);y+=2) {
416         memcpy(dst,src_m1,width);
417         dst += dst_wrap;
418         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
419         src_m2 = src_0;
420         src_m1 = src_p1;
421         src_0 = src_p2;
422         src_p1 += 2*src_wrap;
423         src_p2 += 2*src_wrap;
424         dst += dst_wrap;
425     }
426     memcpy(dst,src_m1,width);
427     dst += dst_wrap;
428     /* do last line */
429     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
430 }
431
432 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
433                                              int width, int height)
434 {
435     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
436     int y;
437     uint8_t *buf;
438     buf = av_malloc(width);
439
440     src_m1 = src1;
441     memcpy(buf,src_m1,width);
442     src_0=&src_m1[src_wrap];
443     src_p1=&src_0[src_wrap];
444     src_p2=&src_p1[src_wrap];
445     for(y=0;y<(height-2);y+=2) {
446         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
447         src_m1 = src_p1;
448         src_0 = src_p2;
449         src_p1 += 2*src_wrap;
450         src_p2 += 2*src_wrap;
451     }
452     /* do last line */
453     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
454     av_free(buf);
455 }
456
457 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
458                           enum AVPixelFormat pix_fmt, int width, int height)
459 {
460     int i;
461
462     if (pix_fmt != AV_PIX_FMT_YUV420P &&
463         pix_fmt != AV_PIX_FMT_YUVJ420P &&
464         pix_fmt != AV_PIX_FMT_YUV422P &&
465         pix_fmt != AV_PIX_FMT_YUVJ422P &&
466         pix_fmt != AV_PIX_FMT_YUV444P &&
467         pix_fmt != AV_PIX_FMT_YUV411P &&
468         pix_fmt != AV_PIX_FMT_GRAY8)
469         return -1;
470     if ((width & 3) != 0 || (height & 3) != 0)
471         return -1;
472
473     for(i=0;i<3;i++) {
474         if (i == 1) {
475             switch(pix_fmt) {
476             case AV_PIX_FMT_YUVJ420P:
477             case AV_PIX_FMT_YUV420P:
478                 width >>= 1;
479                 height >>= 1;
480                 break;
481             case AV_PIX_FMT_YUV422P:
482             case AV_PIX_FMT_YUVJ422P:
483                 width >>= 1;
484                 break;
485             case AV_PIX_FMT_YUV411P:
486                 width >>= 2;
487                 break;
488             default:
489                 break;
490             }
491             if (pix_fmt == AV_PIX_FMT_GRAY8) {
492                 break;
493             }
494         }
495         if (src == dst) {
496             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
497                                  width, height);
498         } else {
499             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
500                                         src->data[i], src->linesize[i],
501                                         width, height);
502         }
503     }
504     emms_c();
505     return 0;
506 }
507
508 #endif /* FF_API_DEINTERLACE */