]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
imgconvert: store padded size information in PixFmtInfo, simplify avg_bits_per_pixel()
[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 FFmpeg.
6  *
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.
11  *
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.
16  *
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
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 "imgconvert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
40
41 #if HAVE_MMX && HAVE_YASM
42 #include "x86/dsputil_mmx.h"
43 #endif
44
45 #define FF_COLOR_RGB      0 /**< RGB color space */
46 #define FF_COLOR_GRAY     1 /**< gray color space */
47 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
49
50 #define FF_PIXEL_PLANAR   0 /**< each channel has one component in AVPicture */
51 #define FF_PIXEL_PACKED   1 /**< only one components containing all the channels */
52 #define FF_PIXEL_PALETTE  2  /**< one components containing indexes for a palette */
53
54 #if HAVE_MMX && HAVE_YASM
55 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
56 #define deinterlace_line         ff_deinterlace_line_mmx
57 #else
58 #define deinterlace_line_inplace deinterlace_line_inplace_c
59 #define deinterlace_line         deinterlace_line_c
60 #endif
61
62 typedef struct PixFmtInfo {
63     uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
64     uint8_t pixel_type;      /**< pixel storage type (see FF_PIXEL_xxx constants) */
65     uint8_t is_alpha : 1;    /**< true if alpha can be specified */
66     uint8_t depth;           /**< bit depth of the color components */
67     uint8_t padded_size;     /**< padded size in bits if different from the non-padded size */
68 } PixFmtInfo;
69
70 /* this table gives more information about formats */
71 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
72     /* YUV formats */
73     [PIX_FMT_YUV420P] = {
74         .color_type = FF_COLOR_YUV,
75         .pixel_type = FF_PIXEL_PLANAR,
76         .depth = 8,
77     },
78     [PIX_FMT_YUV422P] = {
79         .color_type = FF_COLOR_YUV,
80         .pixel_type = FF_PIXEL_PLANAR,
81         .depth = 8,
82     },
83     [PIX_FMT_YUV444P] = {
84         .color_type = FF_COLOR_YUV,
85         .pixel_type = FF_PIXEL_PLANAR,
86         .depth = 8,
87     },
88     [PIX_FMT_YUYV422] = {
89         .color_type = FF_COLOR_YUV,
90         .pixel_type = FF_PIXEL_PACKED,
91         .depth = 8,
92     },
93     [PIX_FMT_UYVY422] = {
94         .color_type = FF_COLOR_YUV,
95         .pixel_type = FF_PIXEL_PACKED,
96         .depth = 8,
97     },
98     [PIX_FMT_YUV410P] = {
99         .color_type = FF_COLOR_YUV,
100         .pixel_type = FF_PIXEL_PLANAR,
101         .depth = 8,
102     },
103     [PIX_FMT_YUV411P] = {
104         .color_type = FF_COLOR_YUV,
105         .pixel_type = FF_PIXEL_PLANAR,
106         .depth = 8,
107     },
108     [PIX_FMT_YUV440P] = {
109         .color_type = FF_COLOR_YUV,
110         .pixel_type = FF_PIXEL_PLANAR,
111         .depth = 8,
112     },
113     [PIX_FMT_YUV420P16LE] = {
114         .color_type = FF_COLOR_YUV,
115         .pixel_type = FF_PIXEL_PLANAR,
116         .depth = 16,
117     },
118     [PIX_FMT_YUV422P16LE] = {
119         .color_type = FF_COLOR_YUV,
120         .pixel_type = FF_PIXEL_PLANAR,
121         .depth = 16,
122     },
123     [PIX_FMT_YUV444P16LE] = {
124         .color_type = FF_COLOR_YUV,
125         .pixel_type = FF_PIXEL_PLANAR,
126         .depth = 16,
127     },
128     [PIX_FMT_YUV420P16BE] = {
129         .color_type = FF_COLOR_YUV,
130         .pixel_type = FF_PIXEL_PLANAR,
131         .depth = 16,
132     },
133     [PIX_FMT_YUV422P16BE] = {
134         .color_type = FF_COLOR_YUV,
135         .pixel_type = FF_PIXEL_PLANAR,
136         .depth = 16,
137     },
138     [PIX_FMT_YUV444P16BE] = {
139         .color_type = FF_COLOR_YUV,
140         .pixel_type = FF_PIXEL_PLANAR,
141         .depth = 16,
142     },
143
144     /* YUV formats with alpha plane */
145     [PIX_FMT_YUVA420P] = {
146         .color_type = FF_COLOR_YUV,
147         .pixel_type = FF_PIXEL_PLANAR,
148         .depth = 8,
149     },
150
151     /* JPEG YUV */
152     [PIX_FMT_YUVJ420P] = {
153         .color_type = FF_COLOR_YUV_JPEG,
154         .pixel_type = FF_PIXEL_PLANAR,
155         .depth = 8,
156     },
157     [PIX_FMT_YUVJ422P] = {
158         .color_type = FF_COLOR_YUV_JPEG,
159         .pixel_type = FF_PIXEL_PLANAR,
160         .depth = 8,
161     },
162     [PIX_FMT_YUVJ444P] = {
163         .color_type = FF_COLOR_YUV_JPEG,
164         .pixel_type = FF_PIXEL_PLANAR,
165         .depth = 8,
166     },
167     [PIX_FMT_YUVJ440P] = {
168         .color_type = FF_COLOR_YUV_JPEG,
169         .pixel_type = FF_PIXEL_PLANAR,
170         .depth = 8,
171     },
172
173     /* RGB formats */
174     [PIX_FMT_RGB24] = {
175         .color_type = FF_COLOR_RGB,
176         .pixel_type = FF_PIXEL_PACKED,
177         .depth = 8,
178     },
179     [PIX_FMT_BGR24] = {
180         .color_type = FF_COLOR_RGB,
181         .pixel_type = FF_PIXEL_PACKED,
182         .depth = 8,
183     },
184     [PIX_FMT_ARGB] = {
185         .is_alpha = 1,
186         .color_type = FF_COLOR_RGB,
187         .pixel_type = FF_PIXEL_PACKED,
188         .depth = 8,
189     },
190     [PIX_FMT_RGB48BE] = {
191         .color_type = FF_COLOR_RGB,
192         .pixel_type = FF_PIXEL_PACKED,
193         .depth = 16,
194     },
195     [PIX_FMT_RGB48LE] = {
196         .color_type = FF_COLOR_RGB,
197         .pixel_type = FF_PIXEL_PACKED,
198         .depth = 16,
199     },
200     [PIX_FMT_RGB565BE] = {
201         .color_type = FF_COLOR_RGB,
202         .pixel_type = FF_PIXEL_PACKED,
203         .depth = 5,
204     },
205     [PIX_FMT_RGB565LE] = {
206         .color_type = FF_COLOR_RGB,
207         .pixel_type = FF_PIXEL_PACKED,
208         .depth = 5,
209     },
210     [PIX_FMT_RGB555BE] = {
211         .color_type = FF_COLOR_RGB,
212         .pixel_type = FF_PIXEL_PACKED,
213         .depth = 5,
214         .padded_size = 16,
215     },
216     [PIX_FMT_RGB555LE] = {
217         .color_type = FF_COLOR_RGB,
218         .pixel_type = FF_PIXEL_PACKED,
219         .depth = 5,
220         .padded_size = 16,
221     },
222     [PIX_FMT_RGB444BE] = {
223         .color_type = FF_COLOR_RGB,
224         .pixel_type = FF_PIXEL_PACKED,
225         .depth = 4,
226         .padded_size = 16,
227     },
228     [PIX_FMT_RGB444LE] = {
229         .color_type = FF_COLOR_RGB,
230         .pixel_type = FF_PIXEL_PACKED,
231         .depth = 4,
232         .padded_size = 16,
233     },
234
235     /* gray / mono formats */
236     [PIX_FMT_GRAY16BE] = {
237         .color_type = FF_COLOR_GRAY,
238         .pixel_type = FF_PIXEL_PLANAR,
239         .depth = 16,
240     },
241     [PIX_FMT_GRAY16LE] = {
242         .color_type = FF_COLOR_GRAY,
243         .pixel_type = FF_PIXEL_PLANAR,
244         .depth = 16,
245     },
246     [PIX_FMT_GRAY8] = {
247         .color_type = FF_COLOR_GRAY,
248         .pixel_type = FF_PIXEL_PLANAR,
249         .depth = 8,
250     },
251     [PIX_FMT_MONOWHITE] = {
252         .color_type = FF_COLOR_GRAY,
253         .pixel_type = FF_PIXEL_PLANAR,
254         .depth = 1,
255     },
256     [PIX_FMT_MONOBLACK] = {
257         .color_type = FF_COLOR_GRAY,
258         .pixel_type = FF_PIXEL_PLANAR,
259         .depth = 1,
260     },
261
262     /* paletted formats */
263     [PIX_FMT_PAL8] = {
264         .is_alpha = 1,
265         .color_type = FF_COLOR_RGB,
266         .pixel_type = FF_PIXEL_PALETTE,
267         .depth = 8,
268     },
269     [PIX_FMT_UYYVYY411] = {
270         .color_type = FF_COLOR_YUV,
271         .pixel_type = FF_PIXEL_PACKED,
272         .depth = 8,
273     },
274     [PIX_FMT_ABGR] = {
275         .is_alpha = 1,
276         .color_type = FF_COLOR_RGB,
277         .pixel_type = FF_PIXEL_PACKED,
278         .depth = 8,
279     },
280     [PIX_FMT_BGR565BE] = {
281         .color_type = FF_COLOR_RGB,
282         .pixel_type = FF_PIXEL_PACKED,
283         .depth = 5,
284         .padded_size = 16,
285     },
286     [PIX_FMT_BGR565LE] = {
287         .color_type = FF_COLOR_RGB,
288         .pixel_type = FF_PIXEL_PACKED,
289         .depth = 5,
290         .padded_size = 16,
291     },
292     [PIX_FMT_BGR555BE] = {
293         .color_type = FF_COLOR_RGB,
294         .pixel_type = FF_PIXEL_PACKED,
295         .depth = 5,
296         .padded_size = 16,
297     },
298     [PIX_FMT_BGR555LE] = {
299         .color_type = FF_COLOR_RGB,
300         .pixel_type = FF_PIXEL_PACKED,
301         .depth = 5,
302         .padded_size = 16,
303     },
304     [PIX_FMT_BGR444BE] = {
305         .color_type = FF_COLOR_RGB,
306         .pixel_type = FF_PIXEL_PACKED,
307         .depth = 4,
308         .padded_size = 16,
309     },
310     [PIX_FMT_BGR444LE] = {
311         .color_type = FF_COLOR_RGB,
312         .pixel_type = FF_PIXEL_PACKED,
313         .depth = 4,
314         .padded_size = 16,
315     },
316     [PIX_FMT_RGB8] = {
317         .color_type = FF_COLOR_RGB,
318         .pixel_type = FF_PIXEL_PACKED,
319         .depth = 8,
320     },
321     [PIX_FMT_RGB4] = {
322         .color_type = FF_COLOR_RGB,
323         .pixel_type = FF_PIXEL_PACKED,
324         .depth = 4,
325     },
326     [PIX_FMT_RGB4_BYTE] = {
327         .color_type = FF_COLOR_RGB,
328         .pixel_type = FF_PIXEL_PACKED,
329         .depth = 8,
330         .padded_size = 8,
331     },
332     [PIX_FMT_BGR8] = {
333         .color_type = FF_COLOR_RGB,
334         .pixel_type = FF_PIXEL_PACKED,
335         .depth = 8,
336     },
337     [PIX_FMT_BGR4] = {
338         .color_type = FF_COLOR_RGB,
339         .pixel_type = FF_PIXEL_PACKED,
340         .depth = 4,
341     },
342     [PIX_FMT_BGR4_BYTE] = {
343         .color_type = FF_COLOR_RGB,
344         .pixel_type = FF_PIXEL_PACKED,
345         .depth = 8,
346         .padded_size = 8,
347     },
348     [PIX_FMT_NV12] = {
349         .color_type = FF_COLOR_YUV,
350         .pixel_type = FF_PIXEL_PLANAR,
351         .depth = 8,
352     },
353     [PIX_FMT_NV21] = {
354         .color_type = FF_COLOR_YUV,
355         .pixel_type = FF_PIXEL_PLANAR,
356         .depth = 8,
357     },
358
359     [PIX_FMT_BGRA] = {
360         .is_alpha = 1,
361         .color_type = FF_COLOR_RGB,
362         .pixel_type = FF_PIXEL_PACKED,
363         .depth = 8,
364     },
365     [PIX_FMT_RGBA] = {
366         .is_alpha = 1,
367         .color_type = FF_COLOR_RGB,
368         .pixel_type = FF_PIXEL_PACKED,
369         .depth = 8,
370     },
371 };
372
373 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
374 {
375     *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
376     *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
377 }
378
379 #if FF_API_GET_PIX_FMT_NAME
380 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
381 {
382     return av_get_pix_fmt_name(pix_fmt);
383 }
384 #endif
385
386 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
387 {
388     return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
389 }
390
391 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
392                    enum PixelFormat pix_fmt, int width, int height)
393 {
394     int ret;
395
396     if ((ret = av_image_check_size(width, height, 0, NULL)) < 0)
397         return ret;
398
399     if ((ret = av_image_fill_linesizes(picture->linesize, pix_fmt, width)) < 0)
400         return ret;
401
402     return av_image_fill_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
403 }
404
405 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
406                      unsigned char *dest, int dest_size)
407 {
408     int i, j, nb_planes = 0, linesizes[4];
409     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
410     int size = avpicture_get_size(pix_fmt, width, height);
411
412     if (size > dest_size || size < 0)
413         return AVERROR(EINVAL);
414
415     for (i = 0; i < desc->nb_components; i++)
416         nb_planes = FFMAX(desc->comp[i].plane, nb_planes);
417     nb_planes++;
418
419     av_image_fill_linesizes(linesizes, pix_fmt, width);
420     for (i = 0; i < nb_planes; i++) {
421         int h, shift = (i == 1 || i == 2) ? desc->log2_chroma_h : 0;
422         const unsigned char *s = src->data[i];
423         h = (height + (1 << shift) - 1) >> shift;
424
425         for (j = 0; j < h; j++) {
426             memcpy(dest, s, linesizes[i]);
427             dest += linesizes[i];
428             s += src->linesize[i];
429         }
430     }
431
432     if (desc->flags & PIX_FMT_PAL)
433         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
434
435     return size;
436 }
437
438 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
439 {
440     AVPicture dummy_pict;
441     if(av_image_check_size(width, height, 0, NULL))
442         return -1;
443     switch (pix_fmt) {
444     case PIX_FMT_RGB8:
445     case PIX_FMT_BGR8:
446     case PIX_FMT_RGB4_BYTE:
447     case PIX_FMT_BGR4_BYTE:
448     case PIX_FMT_GRAY8:
449         // do not include palette for these pseudo-paletted formats
450         return width * height;
451     }
452     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
453 }
454
455 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
456                              int has_alpha)
457 {
458     const PixFmtInfo *pf, *ps;
459     const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
460     const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
461     int loss;
462
463     ps = &pix_fmt_info[src_pix_fmt];
464
465     /* compute loss */
466     loss = 0;
467     pf = &pix_fmt_info[dst_pix_fmt];
468     if (pf->depth < ps->depth ||
469         ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
470           dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
471          (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
472           src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
473         loss |= FF_LOSS_DEPTH;
474     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
475         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
476         loss |= FF_LOSS_RESOLUTION;
477     switch(pf->color_type) {
478     case FF_COLOR_RGB:
479         if (ps->color_type != FF_COLOR_RGB &&
480             ps->color_type != FF_COLOR_GRAY)
481             loss |= FF_LOSS_COLORSPACE;
482         break;
483     case FF_COLOR_GRAY:
484         if (ps->color_type != FF_COLOR_GRAY)
485             loss |= FF_LOSS_COLORSPACE;
486         break;
487     case FF_COLOR_YUV:
488         if (ps->color_type != FF_COLOR_YUV)
489             loss |= FF_LOSS_COLORSPACE;
490         break;
491     case FF_COLOR_YUV_JPEG:
492         if (ps->color_type != FF_COLOR_YUV_JPEG &&
493             ps->color_type != FF_COLOR_YUV &&
494             ps->color_type != FF_COLOR_GRAY)
495             loss |= FF_LOSS_COLORSPACE;
496         break;
497     default:
498         /* fail safe test */
499         if (ps->color_type != pf->color_type)
500             loss |= FF_LOSS_COLORSPACE;
501         break;
502     }
503     if (pf->color_type == FF_COLOR_GRAY &&
504         ps->color_type != FF_COLOR_GRAY)
505         loss |= FF_LOSS_CHROMA;
506     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
507         loss |= FF_LOSS_ALPHA;
508     if (pf->pixel_type == FF_PIXEL_PALETTE &&
509         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
510         loss |= FF_LOSS_COLORQUANT;
511     return loss;
512 }
513
514 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
515 {
516     const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
517     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
518
519     return info->padded_size ?
520         info->padded_size : av_get_bits_per_pixel(desc);
521 }
522
523 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
524                                       enum PixelFormat src_pix_fmt,
525                                       int has_alpha,
526                                       int loss_mask)
527 {
528     int dist, i, loss, min_dist;
529     enum PixelFormat dst_pix_fmt;
530
531     /* find exact color match with smallest size */
532     dst_pix_fmt = PIX_FMT_NONE;
533     min_dist = 0x7fffffff;
534     for(i = 0;i < PIX_FMT_NB; i++) {
535         if (pix_fmt_mask & (1ULL << i)) {
536             loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
537             if (loss == 0) {
538                 dist = avg_bits_per_pixel(i);
539                 if (dist < min_dist) {
540                     min_dist = dist;
541                     dst_pix_fmt = i;
542                 }
543             }
544         }
545     }
546     return dst_pix_fmt;
547 }
548
549 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
550                               int has_alpha, int *loss_ptr)
551 {
552     enum PixelFormat dst_pix_fmt;
553     int loss_mask, i;
554     static const int loss_mask_order[] = {
555         ~0, /* no loss first */
556         ~FF_LOSS_ALPHA,
557         ~FF_LOSS_RESOLUTION,
558         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
559         ~FF_LOSS_COLORQUANT,
560         ~FF_LOSS_DEPTH,
561         0,
562     };
563
564     /* try with successive loss */
565     i = 0;
566     for(;;) {
567         loss_mask = loss_mask_order[i++];
568         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
569                                                  has_alpha, loss_mask);
570         if (dst_pix_fmt >= 0)
571             goto found;
572         if (loss_mask == 0)
573             break;
574     }
575     return PIX_FMT_NONE;
576  found:
577     if (loss_ptr)
578         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
579     return dst_pix_fmt;
580 }
581
582 void av_picture_copy(AVPicture *dst, const AVPicture *src,
583                      enum PixelFormat pix_fmt, int width, int height)
584 {
585     av_image_copy(dst->data, dst->linesize, src->data,
586                   src->linesize, pix_fmt, width, height);
587 }
588
589 /* 2x2 -> 1x1 */
590 void ff_shrink22(uint8_t *dst, int dst_wrap,
591                      const uint8_t *src, int src_wrap,
592                      int width, int height)
593 {
594     int w;
595     const uint8_t *s1, *s2;
596     uint8_t *d;
597
598     for(;height > 0; height--) {
599         s1 = src;
600         s2 = s1 + src_wrap;
601         d = dst;
602         for(w = width;w >= 4; w-=4) {
603             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
604             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
605             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
606             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
607             s1 += 8;
608             s2 += 8;
609             d += 4;
610         }
611         for(;w > 0; w--) {
612             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
613             s1 += 2;
614             s2 += 2;
615             d++;
616         }
617         src += 2 * src_wrap;
618         dst += dst_wrap;
619     }
620 }
621
622 /* 4x4 -> 1x1 */
623 void ff_shrink44(uint8_t *dst, int dst_wrap,
624                      const uint8_t *src, int src_wrap,
625                      int width, int height)
626 {
627     int w;
628     const uint8_t *s1, *s2, *s3, *s4;
629     uint8_t *d;
630
631     for(;height > 0; height--) {
632         s1 = src;
633         s2 = s1 + src_wrap;
634         s3 = s2 + src_wrap;
635         s4 = s3 + src_wrap;
636         d = dst;
637         for(w = width;w > 0; w--) {
638             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
639                     s2[0] + s2[1] + s2[2] + s2[3] +
640                     s3[0] + s3[1] + s3[2] + s3[3] +
641                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
642             s1 += 4;
643             s2 += 4;
644             s3 += 4;
645             s4 += 4;
646             d++;
647         }
648         src += 4 * src_wrap;
649         dst += dst_wrap;
650     }
651 }
652
653 /* 8x8 -> 1x1 */
654 void ff_shrink88(uint8_t *dst, int dst_wrap,
655                      const uint8_t *src, int src_wrap,
656                      int width, int height)
657 {
658     int w, i;
659
660     for(;height > 0; height--) {
661         for(w = width;w > 0; w--) {
662             int tmp=0;
663             for(i=0; i<8; i++){
664                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
665                 src += src_wrap;
666             }
667             *(dst++) = (tmp + 32)>>6;
668             src += 8 - 8*src_wrap;
669         }
670         src += 8*src_wrap - 8*width;
671         dst += dst_wrap - width;
672     }
673 }
674
675
676 int avpicture_alloc(AVPicture *picture,
677                     enum PixelFormat pix_fmt, int width, int height)
678 {
679     int ret;
680
681     if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
682         memset(picture, 0, sizeof(AVPicture));
683         return ret;
684     }
685
686     return 0;
687 }
688
689 void avpicture_free(AVPicture *picture)
690 {
691     av_free(picture->data[0]);
692 }
693
694 /* return true if yuv planar */
695 static inline int is_yuv_planar(const PixFmtInfo *ps)
696 {
697     return (ps->color_type == FF_COLOR_YUV ||
698             ps->color_type == FF_COLOR_YUV_JPEG) &&
699         ps->pixel_type == FF_PIXEL_PLANAR;
700 }
701
702 int av_picture_crop(AVPicture *dst, const AVPicture *src,
703                     enum PixelFormat pix_fmt, int top_band, int left_band)
704 {
705     int y_shift;
706     int x_shift;
707
708     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
709         return -1;
710
711     y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
712     x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
713
714     if (is_yuv_planar(&pix_fmt_info[pix_fmt])) {
715     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
716     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
717     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
718     } else{
719         if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
720             return -1;
721         if(left_band) //FIXME add support for this too
722             return -1;
723         dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
724     }
725
726     dst->linesize[0] = src->linesize[0];
727     dst->linesize[1] = src->linesize[1];
728     dst->linesize[2] = src->linesize[2];
729     return 0;
730 }
731
732 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
733                    enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
734             int *color)
735 {
736     uint8_t *optr;
737     int y_shift;
738     int x_shift;
739     int yheight;
740     int i, y;
741
742     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
743         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
744
745     for (i = 0; i < 3; i++) {
746         x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
747         y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
748
749         if (padtop || padleft) {
750             memset(dst->data[i], color[i],
751                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
752         }
753
754         if (padleft || padright) {
755             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
756                 (dst->linesize[i] - (padright >> x_shift));
757             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
758             for (y = 0; y < yheight; y++) {
759                 memset(optr, color[i], (padleft + padright) >> x_shift);
760                 optr += dst->linesize[i];
761             }
762         }
763
764         if (src) { /* first line */
765             uint8_t *iptr = src->data[i];
766             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
767                     (padleft >> x_shift);
768             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
769             iptr += src->linesize[i];
770             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
771                 (dst->linesize[i] - (padright >> x_shift));
772             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
773             for (y = 0; y < yheight; y++) {
774                 memset(optr, color[i], (padleft + padright) >> x_shift);
775                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
776                        (width - padleft - padright) >> x_shift);
777                 iptr += src->linesize[i];
778                 optr += dst->linesize[i];
779             }
780         }
781
782         if (padbottom || padright) {
783             optr = dst->data[i] + dst->linesize[i] *
784                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
785             memset(optr, color[i],dst->linesize[i] *
786                 (padbottom >> y_shift) + (padright >> x_shift));
787         }
788     }
789     return 0;
790 }
791
792 /* NOTE: we scan all the pixels to have an exact information */
793 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
794 {
795     const unsigned char *p;
796     int src_wrap, ret, x, y;
797     unsigned int a;
798     uint32_t *palette = (uint32_t *)src->data[1];
799
800     p = src->data[0];
801     src_wrap = src->linesize[0] - width;
802     ret = 0;
803     for(y=0;y<height;y++) {
804         for(x=0;x<width;x++) {
805             a = palette[p[0]] >> 24;
806             if (a == 0x00) {
807                 ret |= FF_ALPHA_TRANSP;
808             } else if (a != 0xff) {
809                 ret |= FF_ALPHA_SEMI_TRANSP;
810             }
811             p++;
812         }
813         p += src_wrap;
814     }
815     return ret;
816 }
817
818 int img_get_alpha_info(const AVPicture *src,
819                        enum PixelFormat pix_fmt, int width, int height)
820 {
821     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
822     int ret;
823
824     /* no alpha can be represented in format */
825     if (!pf->is_alpha)
826         return 0;
827     switch(pix_fmt) {
828     case PIX_FMT_PAL8:
829         ret = get_alpha_info_pal8(src, width, height);
830         break;
831     default:
832         /* we do not know, so everything is indicated */
833         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
834         break;
835     }
836     return ret;
837 }
838
839 #if !(HAVE_MMX && HAVE_YASM)
840 /* filter parameters: [-1 4 2 4 -1] // 8 */
841 static void deinterlace_line_c(uint8_t *dst,
842                              const uint8_t *lum_m4, const uint8_t *lum_m3,
843                              const uint8_t *lum_m2, const uint8_t *lum_m1,
844                              const uint8_t *lum,
845                              int size)
846 {
847     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
848     int sum;
849
850     for(;size > 0;size--) {
851         sum = -lum_m4[0];
852         sum += lum_m3[0] << 2;
853         sum += lum_m2[0] << 1;
854         sum += lum_m1[0] << 2;
855         sum += -lum[0];
856         dst[0] = cm[(sum + 4) >> 3];
857         lum_m4++;
858         lum_m3++;
859         lum_m2++;
860         lum_m1++;
861         lum++;
862         dst++;
863     }
864 }
865
866 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
867                                        uint8_t *lum_m2, uint8_t *lum_m1,
868                                        uint8_t *lum, int size)
869 {
870     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
871     int sum;
872
873     for(;size > 0;size--) {
874         sum = -lum_m4[0];
875         sum += lum_m3[0] << 2;
876         sum += lum_m2[0] << 1;
877         lum_m4[0]=lum_m2[0];
878         sum += lum_m1[0] << 2;
879         sum += -lum[0];
880         lum_m2[0] = cm[(sum + 4) >> 3];
881         lum_m4++;
882         lum_m3++;
883         lum_m2++;
884         lum_m1++;
885         lum++;
886     }
887 }
888 #endif
889
890 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
891    top field is copied as is, but the bottom field is deinterlaced
892    against the top field. */
893 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
894                                     const uint8_t *src1, int src_wrap,
895                                     int width, int height)
896 {
897     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
898     int y;
899
900     src_m2 = src1;
901     src_m1 = src1;
902     src_0=&src_m1[src_wrap];
903     src_p1=&src_0[src_wrap];
904     src_p2=&src_p1[src_wrap];
905     for(y=0;y<(height-2);y+=2) {
906         memcpy(dst,src_m1,width);
907         dst += dst_wrap;
908         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
909         src_m2 = src_0;
910         src_m1 = src_p1;
911         src_0 = src_p2;
912         src_p1 += 2*src_wrap;
913         src_p2 += 2*src_wrap;
914         dst += dst_wrap;
915     }
916     memcpy(dst,src_m1,width);
917     dst += dst_wrap;
918     /* do last line */
919     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
920 }
921
922 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
923                                              int width, int height)
924 {
925     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
926     int y;
927     uint8_t *buf;
928     buf = (uint8_t*)av_malloc(width);
929
930     src_m1 = src1;
931     memcpy(buf,src_m1,width);
932     src_0=&src_m1[src_wrap];
933     src_p1=&src_0[src_wrap];
934     src_p2=&src_p1[src_wrap];
935     for(y=0;y<(height-2);y+=2) {
936         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
937         src_m1 = src_p1;
938         src_0 = src_p2;
939         src_p1 += 2*src_wrap;
940         src_p2 += 2*src_wrap;
941     }
942     /* do last line */
943     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
944     av_free(buf);
945 }
946
947 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
948                           enum PixelFormat pix_fmt, int width, int height)
949 {
950     int i;
951
952     if (pix_fmt != PIX_FMT_YUV420P &&
953         pix_fmt != PIX_FMT_YUVJ420P &&
954         pix_fmt != PIX_FMT_YUV422P &&
955         pix_fmt != PIX_FMT_YUVJ422P &&
956         pix_fmt != PIX_FMT_YUV444P &&
957         pix_fmt != PIX_FMT_YUV411P &&
958         pix_fmt != PIX_FMT_GRAY8)
959         return -1;
960     if ((width & 3) != 0 || (height & 3) != 0)
961         return -1;
962
963     for(i=0;i<3;i++) {
964         if (i == 1) {
965             switch(pix_fmt) {
966             case PIX_FMT_YUVJ420P:
967             case PIX_FMT_YUV420P:
968                 width >>= 1;
969                 height >>= 1;
970                 break;
971             case PIX_FMT_YUV422P:
972             case PIX_FMT_YUVJ422P:
973                 width >>= 1;
974                 break;
975             case PIX_FMT_YUV411P:
976                 width >>= 2;
977                 break;
978             default:
979                 break;
980             }
981             if (pix_fmt == PIX_FMT_GRAY8) {
982                 break;
983             }
984         }
985         if (src == dst) {
986             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
987                                  width, height);
988         } else {
989             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
990                                         src->data[i], src->linesize[i],
991                                         width, height);
992         }
993     }
994     emms_c();
995     return 0;
996 }
997