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