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