]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/imgconvert.c
Fix segmentation fault for gray16le to gray conversion.
[frescor/ffmpeg.git] / libavcodec / imgconvert.c
1 /*
2  * Misc image convertion 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 imgconvert.c
24  * Misc image convertion 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
36 #ifdef USE_FASTMEMCPY
37 #include "libvo/fastmemcpy.h"
38 #endif
39
40 #ifdef HAVE_MMX
41 #include "i386/mmx.h"
42 #endif
43
44 #define xglue(x, y) x ## y
45 #define glue(x, y) xglue(x, y)
46
47 #define FF_COLOR_RGB      0 /**< RGB color space */
48 #define FF_COLOR_GRAY     1 /**< gray color space */
49 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
50 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
51
52 #define FF_PIXEL_PLANAR   0 /**< each channel has one component in AVPicture */
53 #define FF_PIXEL_PACKED   1 /**< only one components containing all the channels */
54 #define FF_PIXEL_PALETTE  2  /**< one components containing indexes for a palette */
55
56 typedef struct PixFmtInfo {
57     const char *name;
58     uint8_t nb_channels;     /**< number of channels (including alpha) */
59     uint8_t color_type;      /**< color type (see FF_COLOR_xxx constants) */
60     uint8_t pixel_type;      /**< pixel storage type (see FF_PIXEL_xxx constants) */
61     uint8_t is_alpha : 1;    /**< true if alpha can be specified */
62     uint8_t x_chroma_shift;  /**< X chroma subsampling factor is 2 ^ shift */
63     uint8_t y_chroma_shift;  /**< Y chroma subsampling factor is 2 ^ shift */
64     uint8_t depth;           /**< bit depth of the color components */
65 } PixFmtInfo;
66
67 /* this table gives more information about formats */
68 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
69     /* YUV formats */
70     [PIX_FMT_YUV420P] = {
71         .name = "yuv420p",
72         .nb_channels = 3,
73         .color_type = FF_COLOR_YUV,
74         .pixel_type = FF_PIXEL_PLANAR,
75         .depth = 8,
76         .x_chroma_shift = 1, .y_chroma_shift = 1,
77     },
78     [PIX_FMT_YUV422P] = {
79         .name = "yuv422p",
80         .nb_channels = 3,
81         .color_type = FF_COLOR_YUV,
82         .pixel_type = FF_PIXEL_PLANAR,
83         .depth = 8,
84         .x_chroma_shift = 1, .y_chroma_shift = 0,
85     },
86     [PIX_FMT_YUV444P] = {
87         .name = "yuv444p",
88         .nb_channels = 3,
89         .color_type = FF_COLOR_YUV,
90         .pixel_type = FF_PIXEL_PLANAR,
91         .depth = 8,
92         .x_chroma_shift = 0, .y_chroma_shift = 0,
93     },
94     [PIX_FMT_YUYV422] = {
95         .name = "yuyv422",
96         .nb_channels = 1,
97         .color_type = FF_COLOR_YUV,
98         .pixel_type = FF_PIXEL_PACKED,
99         .depth = 8,
100         .x_chroma_shift = 1, .y_chroma_shift = 0,
101     },
102     [PIX_FMT_UYVY422] = {
103         .name = "uyvy422",
104         .nb_channels = 1,
105         .color_type = FF_COLOR_YUV,
106         .pixel_type = FF_PIXEL_PACKED,
107         .depth = 8,
108         .x_chroma_shift = 1, .y_chroma_shift = 0,
109     },
110     [PIX_FMT_YUV410P] = {
111         .name = "yuv410p",
112         .nb_channels = 3,
113         .color_type = FF_COLOR_YUV,
114         .pixel_type = FF_PIXEL_PLANAR,
115         .depth = 8,
116         .x_chroma_shift = 2, .y_chroma_shift = 2,
117     },
118     [PIX_FMT_YUV411P] = {
119         .name = "yuv411p",
120         .nb_channels = 3,
121         .color_type = FF_COLOR_YUV,
122         .pixel_type = FF_PIXEL_PLANAR,
123         .depth = 8,
124         .x_chroma_shift = 2, .y_chroma_shift = 0,
125     },
126
127     /* JPEG YUV */
128     [PIX_FMT_YUVJ420P] = {
129         .name = "yuvj420p",
130         .nb_channels = 3,
131         .color_type = FF_COLOR_YUV_JPEG,
132         .pixel_type = FF_PIXEL_PLANAR,
133         .depth = 8,
134         .x_chroma_shift = 1, .y_chroma_shift = 1,
135     },
136     [PIX_FMT_YUVJ422P] = {
137         .name = "yuvj422p",
138         .nb_channels = 3,
139         .color_type = FF_COLOR_YUV_JPEG,
140         .pixel_type = FF_PIXEL_PLANAR,
141         .depth = 8,
142         .x_chroma_shift = 1, .y_chroma_shift = 0,
143     },
144     [PIX_FMT_YUVJ444P] = {
145         .name = "yuvj444p",
146         .nb_channels = 3,
147         .color_type = FF_COLOR_YUV_JPEG,
148         .pixel_type = FF_PIXEL_PLANAR,
149         .depth = 8,
150         .x_chroma_shift = 0, .y_chroma_shift = 0,
151     },
152
153     /* RGB formats */
154     [PIX_FMT_RGB24] = {
155         .name = "rgb24",
156         .nb_channels = 3,
157         .color_type = FF_COLOR_RGB,
158         .pixel_type = FF_PIXEL_PACKED,
159         .depth = 8,
160         .x_chroma_shift = 0, .y_chroma_shift = 0,
161     },
162     [PIX_FMT_BGR24] = {
163         .name = "bgr24",
164         .nb_channels = 3,
165         .color_type = FF_COLOR_RGB,
166         .pixel_type = FF_PIXEL_PACKED,
167         .depth = 8,
168         .x_chroma_shift = 0, .y_chroma_shift = 0,
169     },
170     [PIX_FMT_RGB32] = {
171         .name = "rgb32",
172         .nb_channels = 4, .is_alpha = 1,
173         .color_type = FF_COLOR_RGB,
174         .pixel_type = FF_PIXEL_PACKED,
175         .depth = 8,
176         .x_chroma_shift = 0, .y_chroma_shift = 0,
177     },
178     [PIX_FMT_RGB565] = {
179         .name = "rgb565",
180         .nb_channels = 3,
181         .color_type = FF_COLOR_RGB,
182         .pixel_type = FF_PIXEL_PACKED,
183         .depth = 5,
184         .x_chroma_shift = 0, .y_chroma_shift = 0,
185     },
186     [PIX_FMT_RGB555] = {
187         .name = "rgb555",
188         .nb_channels = 3,
189         .color_type = FF_COLOR_RGB,
190         .pixel_type = FF_PIXEL_PACKED,
191         .depth = 5,
192         .x_chroma_shift = 0, .y_chroma_shift = 0,
193     },
194
195     /* gray / mono formats */
196     [PIX_FMT_GRAY16BE] = {
197         .name = "gray16be",
198         .nb_channels = 1,
199         .color_type = FF_COLOR_GRAY,
200         .pixel_type = FF_PIXEL_PLANAR,
201         .depth = 16,
202     },
203     [PIX_FMT_GRAY16LE] = {
204         .name = "gray16le",
205         .nb_channels = 1,
206         .color_type = FF_COLOR_GRAY,
207         .pixel_type = FF_PIXEL_PLANAR,
208         .depth = 16,
209     },
210     [PIX_FMT_GRAY8] = {
211         .name = "gray",
212         .nb_channels = 1,
213         .color_type = FF_COLOR_GRAY,
214         .pixel_type = FF_PIXEL_PLANAR,
215         .depth = 8,
216     },
217     [PIX_FMT_MONOWHITE] = {
218         .name = "monow",
219         .nb_channels = 1,
220         .color_type = FF_COLOR_GRAY,
221         .pixel_type = FF_PIXEL_PLANAR,
222         .depth = 1,
223     },
224     [PIX_FMT_MONOBLACK] = {
225         .name = "monob",
226         .nb_channels = 1,
227         .color_type = FF_COLOR_GRAY,
228         .pixel_type = FF_PIXEL_PLANAR,
229         .depth = 1,
230     },
231
232     /* paletted formats */
233     [PIX_FMT_PAL8] = {
234         .name = "pal8",
235         .nb_channels = 4, .is_alpha = 1,
236         .color_type = FF_COLOR_RGB,
237         .pixel_type = FF_PIXEL_PALETTE,
238         .depth = 8,
239     },
240     [PIX_FMT_XVMC_MPEG2_MC] = {
241         .name = "xvmcmc",
242     },
243     [PIX_FMT_XVMC_MPEG2_IDCT] = {
244         .name = "xvmcidct",
245     },
246     [PIX_FMT_UYYVYY411] = {
247         .name = "uyyvyy411",
248         .nb_channels = 1,
249         .color_type = FF_COLOR_YUV,
250         .pixel_type = FF_PIXEL_PACKED,
251         .depth = 8,
252         .x_chroma_shift = 2, .y_chroma_shift = 0,
253     },
254     [PIX_FMT_BGR32] = {
255         .name = "bgr32",
256         .nb_channels = 4, .is_alpha = 1,
257         .color_type = FF_COLOR_RGB,
258         .pixel_type = FF_PIXEL_PACKED,
259         .depth = 8,
260         .x_chroma_shift = 0, .y_chroma_shift = 0,
261     },
262     [PIX_FMT_BGR565] = {
263         .name = "bgr565",
264         .nb_channels = 3,
265         .color_type = FF_COLOR_RGB,
266         .pixel_type = FF_PIXEL_PACKED,
267         .depth = 5,
268         .x_chroma_shift = 0, .y_chroma_shift = 0,
269     },
270     [PIX_FMT_BGR555] = {
271         .name = "bgr555",
272         .nb_channels = 3,
273         .color_type = FF_COLOR_RGB,
274         .pixel_type = FF_PIXEL_PACKED,
275         .depth = 5,
276         .x_chroma_shift = 0, .y_chroma_shift = 0,
277     },
278     [PIX_FMT_RGB8] = {
279         .name = "rgb8",
280         .nb_channels = 1,
281         .color_type = FF_COLOR_RGB,
282         .pixel_type = FF_PIXEL_PACKED,
283         .depth = 8,
284         .x_chroma_shift = 0, .y_chroma_shift = 0,
285     },
286     [PIX_FMT_RGB4] = {
287         .name = "rgb4",
288         .nb_channels = 1,
289         .color_type = FF_COLOR_RGB,
290         .pixel_type = FF_PIXEL_PACKED,
291         .depth = 4,
292         .x_chroma_shift = 0, .y_chroma_shift = 0,
293     },
294     [PIX_FMT_RGB4_BYTE] = {
295         .name = "rgb4_byte",
296         .nb_channels = 1,
297         .color_type = FF_COLOR_RGB,
298         .pixel_type = FF_PIXEL_PACKED,
299         .depth = 8,
300         .x_chroma_shift = 0, .y_chroma_shift = 0,
301     },
302     [PIX_FMT_BGR8] = {
303         .name = "bgr8",
304         .nb_channels = 1,
305         .color_type = FF_COLOR_RGB,
306         .pixel_type = FF_PIXEL_PACKED,
307         .depth = 8,
308         .x_chroma_shift = 0, .y_chroma_shift = 0,
309     },
310     [PIX_FMT_BGR4] = {
311         .name = "bgr4",
312         .nb_channels = 1,
313         .color_type = FF_COLOR_RGB,
314         .pixel_type = FF_PIXEL_PACKED,
315         .depth = 4,
316         .x_chroma_shift = 0, .y_chroma_shift = 0,
317     },
318     [PIX_FMT_BGR4_BYTE] = {
319         .name = "bgr4_byte",
320         .nb_channels = 1,
321         .color_type = FF_COLOR_RGB,
322         .pixel_type = FF_PIXEL_PACKED,
323         .depth = 8,
324         .x_chroma_shift = 0, .y_chroma_shift = 0,
325     },
326     [PIX_FMT_NV12] = {
327         .name = "nv12",
328         .nb_channels = 2,
329         .color_type = FF_COLOR_YUV,
330         .pixel_type = FF_PIXEL_PLANAR,
331         .depth = 8,
332         .x_chroma_shift = 1, .y_chroma_shift = 1,
333     },
334     [PIX_FMT_NV21] = {
335         .name = "nv12",
336         .nb_channels = 2,
337         .color_type = FF_COLOR_YUV,
338         .pixel_type = FF_PIXEL_PLANAR,
339         .depth = 8,
340         .x_chroma_shift = 1, .y_chroma_shift = 1,
341     },
342
343     [PIX_FMT_BGR32_1] = {
344         .name = "bgr32_1",
345         .nb_channels = 4, .is_alpha = 1,
346         .color_type = FF_COLOR_RGB,
347         .pixel_type = FF_PIXEL_PACKED,
348         .depth = 8,
349         .x_chroma_shift = 0, .y_chroma_shift = 0,
350     },
351     [PIX_FMT_RGB32_1] = {
352         .name = "rgb32_1",
353         .nb_channels = 4, .is_alpha = 1,
354         .color_type = FF_COLOR_RGB,
355         .pixel_type = FF_PIXEL_PACKED,
356         .depth = 8,
357         .x_chroma_shift = 0, .y_chroma_shift = 0,
358     },
359 };
360
361 void avcodec_get_chroma_sub_sample(int pix_fmt, int *h_shift, int *v_shift)
362 {
363     *h_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
364     *v_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
365 }
366
367 const char *avcodec_get_pix_fmt_name(int pix_fmt)
368 {
369     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
370         return "???";
371     else
372         return pix_fmt_info[pix_fmt].name;
373 }
374
375 enum PixelFormat avcodec_get_pix_fmt(const char* name)
376 {
377     int i;
378
379     for (i=0; i < PIX_FMT_NB; i++)
380          if (!strcmp(pix_fmt_info[i].name, name))
381              break;
382     return i;
383 }
384
385 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
386                    int pix_fmt, int width, int height)
387 {
388     int size, w2, h2, size2;
389     const PixFmtInfo *pinfo;
390
391     if(avcodec_check_dimensions(NULL, width, height))
392         goto fail;
393
394     pinfo = &pix_fmt_info[pix_fmt];
395     size = width * height;
396     switch(pix_fmt) {
397     case PIX_FMT_YUV420P:
398     case PIX_FMT_YUV422P:
399     case PIX_FMT_YUV444P:
400     case PIX_FMT_YUV410P:
401     case PIX_FMT_YUV411P:
402     case PIX_FMT_YUVJ420P:
403     case PIX_FMT_YUVJ422P:
404     case PIX_FMT_YUVJ444P:
405         w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
406         h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
407         size2 = w2 * h2;
408         picture->data[0] = ptr;
409         picture->data[1] = picture->data[0] + size;
410         picture->data[2] = picture->data[1] + size2;
411         picture->linesize[0] = width;
412         picture->linesize[1] = w2;
413         picture->linesize[2] = w2;
414         return size + 2 * size2;
415     case PIX_FMT_NV12:
416     case PIX_FMT_NV21:
417         w2 = (width + (1 << pinfo->x_chroma_shift) - 1) >> pinfo->x_chroma_shift;
418         h2 = (height + (1 << pinfo->y_chroma_shift) - 1) >> pinfo->y_chroma_shift;
419         size2 = w2 * h2 * 2;
420         picture->data[0] = ptr;
421         picture->data[1] = picture->data[0] + size;
422         picture->data[2] = NULL;
423         picture->linesize[0] = width;
424         picture->linesize[1] = w2;
425         picture->linesize[2] = 0;
426         return size + 2 * size2;
427     case PIX_FMT_RGB24:
428     case PIX_FMT_BGR24:
429         picture->data[0] = ptr;
430         picture->data[1] = NULL;
431         picture->data[2] = NULL;
432         picture->linesize[0] = width * 3;
433         return size * 3;
434     case PIX_FMT_RGB32:
435     case PIX_FMT_BGR32:
436     case PIX_FMT_RGB32_1:
437     case PIX_FMT_BGR32_1:
438         picture->data[0] = ptr;
439         picture->data[1] = NULL;
440         picture->data[2] = NULL;
441         picture->linesize[0] = width * 4;
442         return size * 4;
443     case PIX_FMT_GRAY16BE:
444     case PIX_FMT_GRAY16LE:
445     case PIX_FMT_BGR555:
446     case PIX_FMT_BGR565:
447     case PIX_FMT_RGB555:
448     case PIX_FMT_RGB565:
449     case PIX_FMT_YUYV422:
450         picture->data[0] = ptr;
451         picture->data[1] = NULL;
452         picture->data[2] = NULL;
453         picture->linesize[0] = width * 2;
454         return size * 2;
455     case PIX_FMT_UYVY422:
456         picture->data[0] = ptr;
457         picture->data[1] = NULL;
458         picture->data[2] = NULL;
459         picture->linesize[0] = width * 2;
460         return size * 2;
461     case PIX_FMT_UYYVYY411:
462         picture->data[0] = ptr;
463         picture->data[1] = NULL;
464         picture->data[2] = NULL;
465         picture->linesize[0] = width + width/2;
466         return size + size/2;
467     case PIX_FMT_RGB8:
468     case PIX_FMT_BGR8:
469     case PIX_FMT_RGB4_BYTE:
470     case PIX_FMT_BGR4_BYTE:
471     case PIX_FMT_GRAY8:
472         picture->data[0] = ptr;
473         picture->data[1] = NULL;
474         picture->data[2] = NULL;
475         picture->linesize[0] = width;
476         return size;
477     case PIX_FMT_RGB4:
478     case PIX_FMT_BGR4:
479         picture->data[0] = ptr;
480         picture->data[1] = NULL;
481         picture->data[2] = NULL;
482         picture->linesize[0] = width / 2;
483         return size / 2;
484     case PIX_FMT_MONOWHITE:
485     case PIX_FMT_MONOBLACK:
486         picture->data[0] = ptr;
487         picture->data[1] = NULL;
488         picture->data[2] = NULL;
489         picture->linesize[0] = (width + 7) >> 3;
490         return picture->linesize[0] * height;
491     case PIX_FMT_PAL8:
492         size2 = (size + 3) & ~3;
493         picture->data[0] = ptr;
494         picture->data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
495         picture->data[2] = NULL;
496         picture->linesize[0] = width;
497         picture->linesize[1] = 4;
498         return size2 + 256 * 4;
499     default:
500 fail:
501         picture->data[0] = NULL;
502         picture->data[1] = NULL;
503         picture->data[2] = NULL;
504         picture->data[3] = NULL;
505         return -1;
506     }
507 }
508
509 int avpicture_layout(const AVPicture* src, int pix_fmt, int width, int height,
510                      unsigned char *dest, int dest_size)
511 {
512     const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
513     int i, j, w, h, data_planes;
514     const unsigned char* s;
515     int size = avpicture_get_size(pix_fmt, width, height);
516
517     if (size > dest_size || size < 0)
518         return -1;
519
520     if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
521         if (pix_fmt == PIX_FMT_YUYV422 ||
522             pix_fmt == PIX_FMT_UYVY422 ||
523             pix_fmt == PIX_FMT_BGR565 ||
524             pix_fmt == PIX_FMT_BGR555 ||
525             pix_fmt == PIX_FMT_RGB565 ||
526             pix_fmt == PIX_FMT_RGB555)
527             w = width * 2;
528         else if (pix_fmt == PIX_FMT_UYYVYY411)
529           w = width + width/2;
530         else if (pix_fmt == PIX_FMT_PAL8)
531           w = width;
532         else
533           w = width * (pf->depth * pf->nb_channels / 8);
534
535         data_planes = 1;
536         h = height;
537     } else {
538         data_planes = pf->nb_channels;
539         w = (width*pf->depth + 7)/8;
540         h = height;
541     }
542
543     for (i=0; i<data_planes; i++) {
544          if (i == 1) {
545              w = width >> pf->x_chroma_shift;
546              h = height >> pf->y_chroma_shift;
547          }
548          s = src->data[i];
549          for(j=0; j<h; j++) {
550              memcpy(dest, s, w);
551              dest += w;
552              s += src->linesize[i];
553          }
554     }
555
556     if (pf->pixel_type == FF_PIXEL_PALETTE)
557         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
558
559     return size;
560 }
561
562 int avpicture_get_size(int pix_fmt, int width, int height)
563 {
564     AVPicture dummy_pict;
565     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
566 }
567
568 int avcodec_get_pix_fmt_loss(int dst_pix_fmt, int src_pix_fmt,
569                              int has_alpha)
570 {
571     const PixFmtInfo *pf, *ps;
572     int loss;
573
574     ps = &pix_fmt_info[src_pix_fmt];
575     pf = &pix_fmt_info[dst_pix_fmt];
576
577     /* compute loss */
578     loss = 0;
579     pf = &pix_fmt_info[dst_pix_fmt];
580     if (pf->depth < ps->depth ||
581         (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
582         loss |= FF_LOSS_DEPTH;
583     if (pf->x_chroma_shift > ps->x_chroma_shift ||
584         pf->y_chroma_shift > ps->y_chroma_shift)
585         loss |= FF_LOSS_RESOLUTION;
586     switch(pf->color_type) {
587     case FF_COLOR_RGB:
588         if (ps->color_type != FF_COLOR_RGB &&
589             ps->color_type != FF_COLOR_GRAY)
590             loss |= FF_LOSS_COLORSPACE;
591         break;
592     case FF_COLOR_GRAY:
593         if (ps->color_type != FF_COLOR_GRAY)
594             loss |= FF_LOSS_COLORSPACE;
595         break;
596     case FF_COLOR_YUV:
597         if (ps->color_type != FF_COLOR_YUV)
598             loss |= FF_LOSS_COLORSPACE;
599         break;
600     case FF_COLOR_YUV_JPEG:
601         if (ps->color_type != FF_COLOR_YUV_JPEG &&
602             ps->color_type != FF_COLOR_YUV &&
603             ps->color_type != FF_COLOR_GRAY)
604             loss |= FF_LOSS_COLORSPACE;
605         break;
606     default:
607         /* fail safe test */
608         if (ps->color_type != pf->color_type)
609             loss |= FF_LOSS_COLORSPACE;
610         break;
611     }
612     if (pf->color_type == FF_COLOR_GRAY &&
613         ps->color_type != FF_COLOR_GRAY)
614         loss |= FF_LOSS_CHROMA;
615     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
616         loss |= FF_LOSS_ALPHA;
617     if (pf->pixel_type == FF_PIXEL_PALETTE &&
618         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
619         loss |= FF_LOSS_COLORQUANT;
620     return loss;
621 }
622
623 static int avg_bits_per_pixel(int pix_fmt)
624 {
625     int bits;
626     const PixFmtInfo *pf;
627
628     pf = &pix_fmt_info[pix_fmt];
629     switch(pf->pixel_type) {
630     case FF_PIXEL_PACKED:
631         switch(pix_fmt) {
632         case PIX_FMT_YUYV422:
633         case PIX_FMT_UYVY422:
634         case PIX_FMT_RGB565:
635         case PIX_FMT_RGB555:
636         case PIX_FMT_BGR565:
637         case PIX_FMT_BGR555:
638             bits = 16;
639             break;
640         case PIX_FMT_UYYVYY411:
641             bits = 12;
642             break;
643         default:
644             bits = pf->depth * pf->nb_channels;
645             break;
646         }
647         break;
648     case FF_PIXEL_PLANAR:
649         if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
650             bits = pf->depth * pf->nb_channels;
651         } else {
652             bits = pf->depth + ((2 * pf->depth) >>
653                                 (pf->x_chroma_shift + pf->y_chroma_shift));
654         }
655         break;
656     case FF_PIXEL_PALETTE:
657         bits = 8;
658         break;
659     default:
660         bits = -1;
661         break;
662     }
663     return bits;
664 }
665
666 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask,
667                                       int src_pix_fmt,
668                                       int has_alpha,
669                                       int loss_mask)
670 {
671     int dist, i, loss, min_dist, dst_pix_fmt;
672
673     /* find exact color match with smallest size */
674     dst_pix_fmt = -1;
675     min_dist = 0x7fffffff;
676     for(i = 0;i < PIX_FMT_NB; i++) {
677         if (pix_fmt_mask & (1 << i)) {
678             loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
679             if (loss == 0) {
680                 dist = avg_bits_per_pixel(i);
681                 if (dist < min_dist) {
682                     min_dist = dist;
683                     dst_pix_fmt = i;
684                 }
685             }
686         }
687     }
688     return dst_pix_fmt;
689 }
690
691 int avcodec_find_best_pix_fmt(int pix_fmt_mask, int src_pix_fmt,
692                               int has_alpha, int *loss_ptr)
693 {
694     int dst_pix_fmt, loss_mask, i;
695     static const int loss_mask_order[] = {
696         ~0, /* no loss first */
697         ~FF_LOSS_ALPHA,
698         ~FF_LOSS_RESOLUTION,
699         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
700         ~FF_LOSS_COLORQUANT,
701         ~FF_LOSS_DEPTH,
702         0,
703     };
704
705     /* try with successive loss */
706     i = 0;
707     for(;;) {
708         loss_mask = loss_mask_order[i++];
709         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
710                                                  has_alpha, loss_mask);
711         if (dst_pix_fmt >= 0)
712             goto found;
713         if (loss_mask == 0)
714             break;
715     }
716     return -1;
717  found:
718     if (loss_ptr)
719         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
720     return dst_pix_fmt;
721 }
722
723 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
724                            const uint8_t *src, int src_wrap,
725                            int width, int height)
726 {
727     if((!dst) || (!src))
728         return;
729     for(;height > 0; height--) {
730         memcpy(dst, src, width);
731         dst += dst_wrap;
732         src += src_wrap;
733     }
734 }
735
736 void av_picture_copy(AVPicture *dst, const AVPicture *src,
737               int pix_fmt, int width, int height)
738 {
739     int bwidth, bits, i;
740     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
741
742     pf = &pix_fmt_info[pix_fmt];
743     switch(pf->pixel_type) {
744     case FF_PIXEL_PACKED:
745         switch(pix_fmt) {
746         case PIX_FMT_YUYV422:
747         case PIX_FMT_UYVY422:
748         case PIX_FMT_RGB565:
749         case PIX_FMT_RGB555:
750         case PIX_FMT_BGR565:
751         case PIX_FMT_BGR555:
752             bits = 16;
753             break;
754         case PIX_FMT_UYYVYY411:
755             bits = 12;
756             break;
757         default:
758             bits = pf->depth * pf->nb_channels;
759             break;
760         }
761         bwidth = (width * bits + 7) >> 3;
762         ff_img_copy_plane(dst->data[0], dst->linesize[0],
763                        src->data[0], src->linesize[0],
764                        bwidth, height);
765         break;
766     case FF_PIXEL_PLANAR:
767         for(i = 0; i < pf->nb_channels; i++) {
768             int w, h;
769             w = width;
770             h = height;
771             if (i == 1 || i == 2) {
772                 w >>= pf->x_chroma_shift;
773                 h >>= pf->y_chroma_shift;
774             }
775             bwidth = (w * pf->depth + 7) >> 3;
776             ff_img_copy_plane(dst->data[i], dst->linesize[i],
777                            src->data[i], src->linesize[i],
778                            bwidth, h);
779         }
780         break;
781     case FF_PIXEL_PALETTE:
782         ff_img_copy_plane(dst->data[0], dst->linesize[0],
783                        src->data[0], src->linesize[0],
784                        width, height);
785         /* copy the palette */
786         ff_img_copy_plane(dst->data[1], dst->linesize[1],
787                        src->data[1], src->linesize[1],
788                        4, 256);
789         break;
790     }
791 }
792
793 /* XXX: totally non optimized */
794
795 static void yuyv422_to_yuv420p(AVPicture *dst, const AVPicture *src,
796                               int width, int height)
797 {
798     const uint8_t *p, *p1;
799     uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
800     int w;
801
802     p1 = src->data[0];
803     lum1 = dst->data[0];
804     cb1 = dst->data[1];
805     cr1 = dst->data[2];
806
807     for(;height >= 1; height -= 2) {
808         p = p1;
809         lum = lum1;
810         cb = cb1;
811         cr = cr1;
812         for(w = width; w >= 2; w -= 2) {
813             lum[0] = p[0];
814             cb[0] = p[1];
815             lum[1] = p[2];
816             cr[0] = p[3];
817             p += 4;
818             lum += 2;
819             cb++;
820             cr++;
821         }
822         if (w) {
823             lum[0] = p[0];
824             cb[0] = p[1];
825             cr[0] = p[3];
826             cb++;
827             cr++;
828         }
829         p1 += src->linesize[0];
830         lum1 += dst->linesize[0];
831         if (height>1) {
832             p = p1;
833             lum = lum1;
834             for(w = width; w >= 2; w -= 2) {
835                 lum[0] = p[0];
836                 lum[1] = p[2];
837                 p += 4;
838                 lum += 2;
839             }
840             if (w) {
841                 lum[0] = p[0];
842             }
843             p1 += src->linesize[0];
844             lum1 += dst->linesize[0];
845         }
846         cb1 += dst->linesize[1];
847         cr1 += dst->linesize[2];
848     }
849 }
850
851 static void uyvy422_to_yuv420p(AVPicture *dst, const AVPicture *src,
852                               int width, int height)
853 {
854     const uint8_t *p, *p1;
855     uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
856     int w;
857
858     p1 = src->data[0];
859
860     lum1 = dst->data[0];
861     cb1 = dst->data[1];
862     cr1 = dst->data[2];
863
864     for(;height >= 1; height -= 2) {
865         p = p1;
866         lum = lum1;
867         cb = cb1;
868         cr = cr1;
869         for(w = width; w >= 2; w -= 2) {
870             lum[0] = p[1];
871             cb[0] = p[0];
872             lum[1] = p[3];
873             cr[0] = p[2];
874             p += 4;
875             lum += 2;
876             cb++;
877             cr++;
878         }
879         if (w) {
880             lum[0] = p[1];
881             cb[0] = p[0];
882             cr[0] = p[2];
883             cb++;
884             cr++;
885         }
886         p1 += src->linesize[0];
887         lum1 += dst->linesize[0];
888         if (height>1) {
889             p = p1;
890             lum = lum1;
891             for(w = width; w >= 2; w -= 2) {
892                 lum[0] = p[1];
893                 lum[1] = p[3];
894                 p += 4;
895                 lum += 2;
896             }
897             if (w) {
898                 lum[0] = p[1];
899             }
900             p1 += src->linesize[0];
901             lum1 += dst->linesize[0];
902         }
903         cb1 += dst->linesize[1];
904         cr1 += dst->linesize[2];
905     }
906 }
907
908
909 static void uyvy422_to_yuv422p(AVPicture *dst, const AVPicture *src,
910                               int width, int height)
911 {
912     const uint8_t *p, *p1;
913     uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
914     int w;
915
916     p1 = src->data[0];
917     lum1 = dst->data[0];
918     cb1 = dst->data[1];
919     cr1 = dst->data[2];
920     for(;height > 0; height--) {
921         p = p1;
922         lum = lum1;
923         cb = cb1;
924         cr = cr1;
925         for(w = width; w >= 2; w -= 2) {
926             lum[0] = p[1];
927             cb[0] = p[0];
928             lum[1] = p[3];
929             cr[0] = p[2];
930             p += 4;
931             lum += 2;
932             cb++;
933             cr++;
934         }
935         p1 += src->linesize[0];
936         lum1 += dst->linesize[0];
937         cb1 += dst->linesize[1];
938         cr1 += dst->linesize[2];
939     }
940 }
941
942
943 static void yuyv422_to_yuv422p(AVPicture *dst, const AVPicture *src,
944                               int width, int height)
945 {
946     const uint8_t *p, *p1;
947     uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
948     int w;
949
950     p1 = src->data[0];
951     lum1 = dst->data[0];
952     cb1 = dst->data[1];
953     cr1 = dst->data[2];
954     for(;height > 0; height--) {
955         p = p1;
956         lum = lum1;
957         cb = cb1;
958         cr = cr1;
959         for(w = width; w >= 2; w -= 2) {
960             lum[0] = p[0];
961             cb[0] = p[1];
962             lum[1] = p[2];
963             cr[0] = p[3];
964             p += 4;
965             lum += 2;
966             cb++;
967             cr++;
968         }
969         p1 += src->linesize[0];
970         lum1 += dst->linesize[0];
971         cb1 += dst->linesize[1];
972         cr1 += dst->linesize[2];
973     }
974 }
975
976 static void yuv422p_to_yuyv422(AVPicture *dst, const AVPicture *src,
977                               int width, int height)
978 {
979     uint8_t *p, *p1;
980     const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
981     int w;
982
983     p1 = dst->data[0];
984     lum1 = src->data[0];
985     cb1 = src->data[1];
986     cr1 = src->data[2];
987     for(;height > 0; height--) {
988         p = p1;
989         lum = lum1;
990         cb = cb1;
991         cr = cr1;
992         for(w = width; w >= 2; w -= 2) {
993             p[0] = lum[0];
994             p[1] = cb[0];
995             p[2] = lum[1];
996             p[3] = cr[0];
997             p += 4;
998             lum += 2;
999             cb++;
1000             cr++;
1001         }
1002         p1 += dst->linesize[0];
1003         lum1 += src->linesize[0];
1004         cb1 += src->linesize[1];
1005         cr1 += src->linesize[2];
1006     }
1007 }
1008
1009 static void yuv422p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1010                               int width, int height)
1011 {
1012     uint8_t *p, *p1;
1013     const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1014     int w;
1015
1016     p1 = dst->data[0];
1017     lum1 = src->data[0];
1018     cb1 = src->data[1];
1019     cr1 = src->data[2];
1020     for(;height > 0; height--) {
1021         p = p1;
1022         lum = lum1;
1023         cb = cb1;
1024         cr = cr1;
1025         for(w = width; w >= 2; w -= 2) {
1026             p[1] = lum[0];
1027             p[0] = cb[0];
1028             p[3] = lum[1];
1029             p[2] = cr[0];
1030             p += 4;
1031             lum += 2;
1032             cb++;
1033             cr++;
1034         }
1035         p1 += dst->linesize[0];
1036         lum1 += src->linesize[0];
1037         cb1 += src->linesize[1];
1038         cr1 += src->linesize[2];
1039     }
1040 }
1041
1042 static void uyyvyy411_to_yuv411p(AVPicture *dst, const AVPicture *src,
1043                               int width, int height)
1044 {
1045     const uint8_t *p, *p1;
1046     uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1047     int w;
1048
1049     p1 = src->data[0];
1050     lum1 = dst->data[0];
1051     cb1 = dst->data[1];
1052     cr1 = dst->data[2];
1053     for(;height > 0; height--) {
1054         p = p1;
1055         lum = lum1;
1056         cb = cb1;
1057         cr = cr1;
1058         for(w = width; w >= 4; w -= 4) {
1059             cb[0] = p[0];
1060             lum[0] = p[1];
1061             lum[1] = p[2];
1062             cr[0] = p[3];
1063             lum[2] = p[4];
1064             lum[3] = p[5];
1065             p += 6;
1066             lum += 4;
1067             cb++;
1068             cr++;
1069         }
1070         p1 += src->linesize[0];
1071         lum1 += dst->linesize[0];
1072         cb1 += dst->linesize[1];
1073         cr1 += dst->linesize[2];
1074     }
1075 }
1076
1077
1078 static void yuv420p_to_yuyv422(AVPicture *dst, const AVPicture *src,
1079                               int width, int height)
1080 {
1081     int w, h;
1082     uint8_t *line1, *line2, *linesrc = dst->data[0];
1083     uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1084     uint8_t *cb1, *cb2 = src->data[1];
1085     uint8_t *cr1, *cr2 = src->data[2];
1086
1087     for(h = height / 2; h--;) {
1088         line1 = linesrc;
1089         line2 = linesrc + dst->linesize[0];
1090
1091         lum1 = lumsrc;
1092         lum2 = lumsrc + src->linesize[0];
1093
1094         cb1 = cb2;
1095         cr1 = cr2;
1096
1097         for(w = width / 2; w--;) {
1098                 *line1++ = *lum1++; *line2++ = *lum2++;
1099                 *line1++ =          *line2++ = *cb1++;
1100                 *line1++ = *lum1++; *line2++ = *lum2++;
1101                 *line1++ =          *line2++ = *cr1++;
1102         }
1103
1104         linesrc += dst->linesize[0] * 2;
1105         lumsrc += src->linesize[0] * 2;
1106         cb2 += src->linesize[1];
1107         cr2 += src->linesize[2];
1108     }
1109 }
1110
1111 static void yuv420p_to_uyvy422(AVPicture *dst, const AVPicture *src,
1112                               int width, int height)
1113 {
1114     int w, h;
1115     uint8_t *line1, *line2, *linesrc = dst->data[0];
1116     uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1117     uint8_t *cb1, *cb2 = src->data[1];
1118     uint8_t *cr1, *cr2 = src->data[2];
1119
1120     for(h = height / 2; h--;) {
1121         line1 = linesrc;
1122         line2 = linesrc + dst->linesize[0];
1123
1124         lum1 = lumsrc;
1125         lum2 = lumsrc + src->linesize[0];
1126
1127         cb1 = cb2;
1128         cr1 = cr2;
1129
1130         for(w = width / 2; w--;) {
1131                 *line1++ =          *line2++ = *cb1++;
1132                 *line1++ = *lum1++; *line2++ = *lum2++;
1133                 *line1++ =          *line2++ = *cr1++;
1134                 *line1++ = *lum1++; *line2++ = *lum2++;
1135         }
1136
1137         linesrc += dst->linesize[0] * 2;
1138         lumsrc += src->linesize[0] * 2;
1139         cb2 += src->linesize[1];
1140         cr2 += src->linesize[2];
1141     }
1142 }
1143
1144 #define SCALEBITS 10
1145 #define ONE_HALF  (1 << (SCALEBITS - 1))
1146 #define FIX(x)    ((int) ((x) * (1<<SCALEBITS) + 0.5))
1147
1148 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1149 {\
1150     cb = (cb1) - 128;\
1151     cr = (cr1) - 128;\
1152     r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1153     g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1154             ONE_HALF;\
1155     b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1156 }
1157
1158 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1159 {\
1160     y = ((y1) - 16) * FIX(255.0/219.0);\
1161     r = cm[(y + r_add) >> SCALEBITS];\
1162     g = cm[(y + g_add) >> SCALEBITS];\
1163     b = cm[(y + b_add) >> SCALEBITS];\
1164 }
1165
1166 #define YUV_TO_RGB1(cb1, cr1)\
1167 {\
1168     cb = (cb1) - 128;\
1169     cr = (cr1) - 128;\
1170     r_add = FIX(1.40200) * cr + ONE_HALF;\
1171     g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1172     b_add = FIX(1.77200) * cb + ONE_HALF;\
1173 }
1174
1175 #define YUV_TO_RGB2(r, g, b, y1)\
1176 {\
1177     y = (y1) << SCALEBITS;\
1178     r = cm[(y + r_add) >> SCALEBITS];\
1179     g = cm[(y + g_add) >> SCALEBITS];\
1180     b = cm[(y + b_add) >> SCALEBITS];\
1181 }
1182
1183 #define Y_CCIR_TO_JPEG(y)\
1184  cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1185
1186 #define Y_JPEG_TO_CCIR(y)\
1187  (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1188
1189 #define C_CCIR_TO_JPEG(y)\
1190  cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1191
1192 /* NOTE: the clamp is really necessary! */
1193 static inline int C_JPEG_TO_CCIR(int y) {
1194     y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
1195     if (y < 16)
1196         y = 16;
1197     return y;
1198 }
1199
1200
1201 #define RGB_TO_Y(r, g, b) \
1202 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1203   FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1204
1205 #define RGB_TO_U(r1, g1, b1, shift)\
1206 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +         \
1207      FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1208
1209 #define RGB_TO_V(r1, g1, b1, shift)\
1210 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 -           \
1211    FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1212
1213 #define RGB_TO_Y_CCIR(r, g, b) \
1214 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1215   FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1216
1217 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1218 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 +         \
1219      FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1220
1221 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1222 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 -           \
1223    FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1224
1225 static uint8_t y_ccir_to_jpeg[256];
1226 static uint8_t y_jpeg_to_ccir[256];
1227 static uint8_t c_ccir_to_jpeg[256];
1228 static uint8_t c_jpeg_to_ccir[256];
1229
1230 /* init various conversion tables */
1231 static void img_convert_init(void)
1232 {
1233     int i;
1234     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1235
1236     for(i = 0;i < 256; i++) {
1237         y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG(i);
1238         y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR(i);
1239         c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG(i);
1240         c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR(i);
1241     }
1242 }
1243
1244 /* apply to each pixel the given table */
1245 static void img_apply_table(uint8_t *dst, int dst_wrap,
1246                             const uint8_t *src, int src_wrap,
1247                             int width, int height, const uint8_t *table1)
1248 {
1249     int n;
1250     const uint8_t *s;
1251     uint8_t *d;
1252     const uint8_t *table;
1253
1254     table = table1;
1255     for(;height > 0; height--) {
1256         s = src;
1257         d = dst;
1258         n = width;
1259         while (n >= 4) {
1260             d[0] = table[s[0]];
1261             d[1] = table[s[1]];
1262             d[2] = table[s[2]];
1263             d[3] = table[s[3]];
1264             d += 4;
1265             s += 4;
1266             n -= 4;
1267         }
1268         while (n > 0) {
1269             d[0] = table[s[0]];
1270             d++;
1271             s++;
1272             n--;
1273         }
1274         dst += dst_wrap;
1275         src += src_wrap;
1276     }
1277 }
1278
1279 /* XXX: use generic filter ? */
1280 /* XXX: in most cases, the sampling position is incorrect */
1281
1282 /* 4x1 -> 1x1 */
1283 static void shrink41(uint8_t *dst, int dst_wrap,
1284                      const uint8_t *src, int src_wrap,
1285                      int width, int height)
1286 {
1287     int w;
1288     const uint8_t *s;
1289     uint8_t *d;
1290
1291     for(;height > 0; height--) {
1292         s = src;
1293         d = dst;
1294         for(w = width;w > 0; w--) {
1295             d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1296             s += 4;
1297             d++;
1298         }
1299         src += src_wrap;
1300         dst += dst_wrap;
1301     }
1302 }
1303
1304 /* 2x1 -> 1x1 */
1305 static void shrink21(uint8_t *dst, int dst_wrap,
1306                      const uint8_t *src, int src_wrap,
1307                      int width, int height)
1308 {
1309     int w;
1310     const uint8_t *s;
1311     uint8_t *d;
1312
1313     for(;height > 0; height--) {
1314         s = src;
1315         d = dst;
1316         for(w = width;w > 0; w--) {
1317             d[0] = (s[0] + s[1]) >> 1;
1318             s += 2;
1319             d++;
1320         }
1321         src += src_wrap;
1322         dst += dst_wrap;
1323     }
1324 }
1325
1326 /* 1x2 -> 1x1 */
1327 static void shrink12(uint8_t *dst, int dst_wrap,
1328                      const uint8_t *src, int src_wrap,
1329                      int width, int height)
1330 {
1331     int w;
1332     uint8_t *d;
1333     const uint8_t *s1, *s2;
1334
1335     for(;height > 0; height--) {
1336         s1 = src;
1337         s2 = s1 + src_wrap;
1338         d = dst;
1339         for(w = width;w >= 4; w-=4) {
1340             d[0] = (s1[0] + s2[0]) >> 1;
1341             d[1] = (s1[1] + s2[1]) >> 1;
1342             d[2] = (s1[2] + s2[2]) >> 1;
1343             d[3] = (s1[3] + s2[3]) >> 1;
1344             s1 += 4;
1345             s2 += 4;
1346             d += 4;
1347         }
1348         for(;w > 0; w--) {
1349             d[0] = (s1[0] + s2[0]) >> 1;
1350             s1++;
1351             s2++;
1352             d++;
1353         }
1354         src += 2 * src_wrap;
1355         dst += dst_wrap;
1356     }
1357 }
1358
1359 /* 2x2 -> 1x1 */
1360 void ff_shrink22(uint8_t *dst, int dst_wrap,
1361                      const uint8_t *src, int src_wrap,
1362                      int width, int height)
1363 {
1364     int w;
1365     const uint8_t *s1, *s2;
1366     uint8_t *d;
1367
1368     for(;height > 0; height--) {
1369         s1 = src;
1370         s2 = s1 + src_wrap;
1371         d = dst;
1372         for(w = width;w >= 4; w-=4) {
1373             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1374             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1375             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1376             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1377             s1 += 8;
1378             s2 += 8;
1379             d += 4;
1380         }
1381         for(;w > 0; w--) {
1382             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1383             s1 += 2;
1384             s2 += 2;
1385             d++;
1386         }
1387         src += 2 * src_wrap;
1388         dst += dst_wrap;
1389     }
1390 }
1391
1392 /* 4x4 -> 1x1 */
1393 void ff_shrink44(uint8_t *dst, int dst_wrap,
1394                      const uint8_t *src, int src_wrap,
1395                      int width, int height)
1396 {
1397     int w;
1398     const uint8_t *s1, *s2, *s3, *s4;
1399     uint8_t *d;
1400
1401     for(;height > 0; height--) {
1402         s1 = src;
1403         s2 = s1 + src_wrap;
1404         s3 = s2 + src_wrap;
1405         s4 = s3 + src_wrap;
1406         d = dst;
1407         for(w = width;w > 0; w--) {
1408             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1409                     s2[0] + s2[1] + s2[2] + s2[3] +
1410                     s3[0] + s3[1] + s3[2] + s3[3] +
1411                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1412             s1 += 4;
1413             s2 += 4;
1414             s3 += 4;
1415             s4 += 4;
1416             d++;
1417         }
1418         src += 4 * src_wrap;
1419         dst += dst_wrap;
1420     }
1421 }
1422
1423 /* 8x8 -> 1x1 */
1424 void ff_shrink88(uint8_t *dst, int dst_wrap,
1425                      const uint8_t *src, int src_wrap,
1426                      int width, int height)
1427 {
1428     int w, i;
1429
1430     for(;height > 0; height--) {
1431         for(w = width;w > 0; w--) {
1432             int tmp=0;
1433             for(i=0; i<8; i++){
1434                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1435                 src += src_wrap;
1436             }
1437             *(dst++) = (tmp + 32)>>6;
1438             src += 8 - 8*src_wrap;
1439         }
1440         src += 8*src_wrap - 8*width;
1441         dst += dst_wrap - width;
1442     }
1443 }
1444
1445 static void grow21_line(uint8_t *dst, const uint8_t *src,
1446                         int width)
1447 {
1448     int w;
1449     const uint8_t *s1;
1450     uint8_t *d;
1451
1452     s1 = src;
1453     d = dst;
1454     for(w = width;w >= 4; w-=4) {
1455         d[1] = d[0] = s1[0];
1456         d[3] = d[2] = s1[1];
1457         s1 += 2;
1458         d += 4;
1459     }
1460     for(;w >= 2; w -= 2) {
1461         d[1] = d[0] = s1[0];
1462         s1 ++;
1463         d += 2;
1464     }
1465     /* only needed if width is not a multiple of two */
1466     /* XXX: veryfy that */
1467     if (w) {
1468         d[0] = s1[0];
1469     }
1470 }
1471
1472 static void grow41_line(uint8_t *dst, const uint8_t *src,
1473                         int width)
1474 {
1475     int w, v;
1476     const uint8_t *s1;
1477     uint8_t *d;
1478
1479     s1 = src;
1480     d = dst;
1481     for(w = width;w >= 4; w-=4) {
1482         v = s1[0];
1483         d[0] = v;
1484         d[1] = v;
1485         d[2] = v;
1486         d[3] = v;
1487         s1 ++;
1488         d += 4;
1489     }
1490 }
1491
1492 /* 1x1 -> 2x1 */
1493 static void grow21(uint8_t *dst, int dst_wrap,
1494                    const uint8_t *src, int src_wrap,
1495                    int width, int height)
1496 {
1497     for(;height > 0; height--) {
1498         grow21_line(dst, src, width);
1499         src += src_wrap;
1500         dst += dst_wrap;
1501     }
1502 }
1503
1504 /* 1x1 -> 2x2 */
1505 static void grow22(uint8_t *dst, int dst_wrap,
1506                    const uint8_t *src, int src_wrap,
1507                    int width, int height)
1508 {
1509     for(;height > 0; height--) {
1510         grow21_line(dst, src, width);
1511         if (height%2)
1512             src += src_wrap;
1513         dst += dst_wrap;
1514     }
1515 }
1516
1517 /* 1x1 -> 4x1 */
1518 static void grow41(uint8_t *dst, int dst_wrap,
1519                    const uint8_t *src, int src_wrap,
1520                    int width, int height)
1521 {
1522     for(;height > 0; height--) {
1523         grow41_line(dst, src, width);
1524         src += src_wrap;
1525         dst += dst_wrap;
1526     }
1527 }
1528
1529 /* 1x1 -> 4x4 */
1530 static void grow44(uint8_t *dst, int dst_wrap,
1531                    const uint8_t *src, int src_wrap,
1532                    int width, int height)
1533 {
1534     for(;height > 0; height--) {
1535         grow41_line(dst, src, width);
1536         if ((height & 3) == 1)
1537             src += src_wrap;
1538         dst += dst_wrap;
1539     }
1540 }
1541
1542 /* 1x2 -> 2x1 */
1543 static void conv411(uint8_t *dst, int dst_wrap,
1544                     const uint8_t *src, int src_wrap,
1545                     int width, int height)
1546 {
1547     int w, c;
1548     const uint8_t *s1, *s2;
1549     uint8_t *d;
1550
1551     width>>=1;
1552
1553     for(;height > 0; height--) {
1554         s1 = src;
1555         s2 = src + src_wrap;
1556         d = dst;
1557         for(w = width;w > 0; w--) {
1558             c = (s1[0] + s2[0]) >> 1;
1559             d[0] = c;
1560             d[1] = c;
1561             s1++;
1562             s2++;
1563             d += 2;
1564         }
1565         src += src_wrap * 2;
1566         dst += dst_wrap;
1567     }
1568 }
1569
1570 /* XXX: add jpeg quantize code */
1571
1572 #define TRANSP_INDEX (6*6*6)
1573
1574 /* this is maybe slow, but allows for extensions */
1575 static inline unsigned char gif_clut_index(uint8_t r, uint8_t g, uint8_t b)
1576 {
1577     return ((((r)/47)%6)*6*6+(((g)/47)%6)*6+(((b)/47)%6));
1578 }
1579
1580 static void build_rgb_palette(uint8_t *palette, int has_alpha)
1581 {
1582     uint32_t *pal;
1583     static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1584     int i, r, g, b;
1585
1586     pal = (uint32_t *)palette;
1587     i = 0;
1588     for(r = 0; r < 6; r++) {
1589         for(g = 0; g < 6; g++) {
1590             for(b = 0; b < 6; b++) {
1591                 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1592                     (pal_value[g] << 8) | pal_value[b];
1593             }
1594         }
1595     }
1596     if (has_alpha)
1597         pal[i++] = 0;
1598     while (i < 256)
1599         pal[i++] = 0xff000000;
1600 }
1601
1602 /* copy bit n to bits 0 ... n - 1 */
1603 static inline unsigned int bitcopy_n(unsigned int a, int n)
1604 {
1605     int mask;
1606     mask = (1 << n) - 1;
1607     return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1608 }
1609
1610 /* rgb555 handling */
1611
1612 #define RGB_NAME rgb555
1613
1614 #define RGB_IN(r, g, b, s)\
1615 {\
1616     unsigned int v = ((const uint16_t *)(s))[0];\
1617     r = bitcopy_n(v >> (10 - 3), 3);\
1618     g = bitcopy_n(v >> (5 - 3), 3);\
1619     b = bitcopy_n(v << 3, 3);\
1620 }
1621
1622
1623 #define RGB_OUT(d, r, g, b)\
1624 {\
1625     ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1626 }
1627
1628 #define BPP 2
1629
1630 #include "imgconvert_template.h"
1631
1632 /* rgb565 handling */
1633
1634 #define RGB_NAME rgb565
1635
1636 #define RGB_IN(r, g, b, s)\
1637 {\
1638     unsigned int v = ((const uint16_t *)(s))[0];\
1639     r = bitcopy_n(v >> (11 - 3), 3);\
1640     g = bitcopy_n(v >> (5 - 2), 2);\
1641     b = bitcopy_n(v << 3, 3);\
1642 }
1643
1644 #define RGB_OUT(d, r, g, b)\
1645 {\
1646     ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1647 }
1648
1649 #define BPP 2
1650
1651 #include "imgconvert_template.h"
1652
1653 /* bgr24 handling */
1654
1655 #define RGB_NAME bgr24
1656
1657 #define RGB_IN(r, g, b, s)\
1658 {\
1659     b = (s)[0];\
1660     g = (s)[1];\
1661     r = (s)[2];\
1662 }
1663
1664 #define RGB_OUT(d, r, g, b)\
1665 {\
1666     (d)[0] = b;\
1667     (d)[1] = g;\
1668     (d)[2] = r;\
1669 }
1670
1671 #define BPP 3
1672
1673 #include "imgconvert_template.h"
1674
1675 #undef RGB_IN
1676 #undef RGB_OUT
1677 #undef BPP
1678
1679 /* rgb24 handling */
1680
1681 #define RGB_NAME rgb24
1682 #define FMT_RGB24
1683
1684 #define RGB_IN(r, g, b, s)\
1685 {\
1686     r = (s)[0];\
1687     g = (s)[1];\
1688     b = (s)[2];\
1689 }
1690
1691 #define RGB_OUT(d, r, g, b)\
1692 {\
1693     (d)[0] = r;\
1694     (d)[1] = g;\
1695     (d)[2] = b;\
1696 }
1697
1698 #define BPP 3
1699
1700 #include "imgconvert_template.h"
1701
1702 /* rgb32 handling */
1703
1704 #define RGB_NAME rgb32
1705 #define FMT_RGB32
1706
1707 #define RGB_IN(r, g, b, s)\
1708 {\
1709     unsigned int v = ((const uint32_t *)(s))[0];\
1710     r = (v >> 16) & 0xff;\
1711     g = (v >> 8) & 0xff;\
1712     b = v & 0xff;\
1713 }
1714
1715 #define RGBA_IN(r, g, b, a, s)\
1716 {\
1717     unsigned int v = ((const uint32_t *)(s))[0];\
1718     a = (v >> 24) & 0xff;\
1719     r = (v >> 16) & 0xff;\
1720     g = (v >> 8) & 0xff;\
1721     b = v & 0xff;\
1722 }
1723
1724 #define RGBA_OUT(d, r, g, b, a)\
1725 {\
1726     ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1727 }
1728
1729 #define BPP 4
1730
1731 #include "imgconvert_template.h"
1732
1733 static void mono_to_gray(AVPicture *dst, const AVPicture *src,
1734                          int width, int height, int xor_mask)
1735 {
1736     const unsigned char *p;
1737     unsigned char *q;
1738     int v, dst_wrap, src_wrap;
1739     int y, w;
1740
1741     p = src->data[0];
1742     src_wrap = src->linesize[0] - ((width + 7) >> 3);
1743
1744     q = dst->data[0];
1745     dst_wrap = dst->linesize[0] - width;
1746     for(y=0;y<height;y++) {
1747         w = width;
1748         while (w >= 8) {
1749             v = *p++ ^ xor_mask;
1750             q[0] = -(v >> 7);
1751             q[1] = -((v >> 6) & 1);
1752             q[2] = -((v >> 5) & 1);
1753             q[3] = -((v >> 4) & 1);
1754             q[4] = -((v >> 3) & 1);
1755             q[5] = -((v >> 2) & 1);
1756             q[6] = -((v >> 1) & 1);
1757             q[7] = -((v >> 0) & 1);
1758             w -= 8;
1759             q += 8;
1760         }
1761         if (w > 0) {
1762             v = *p++ ^ xor_mask;
1763             do {
1764                 q[0] = -((v >> 7) & 1);
1765                 q++;
1766                 v <<= 1;
1767             } while (--w);
1768         }
1769         p += src_wrap;
1770         q += dst_wrap;
1771     }
1772 }
1773
1774 static void monowhite_to_gray(AVPicture *dst, const AVPicture *src,
1775                                int width, int height)
1776 {
1777     mono_to_gray(dst, src, width, height, 0xff);
1778 }
1779
1780 static void monoblack_to_gray(AVPicture *dst, const AVPicture *src,
1781                                int width, int height)
1782 {
1783     mono_to_gray(dst, src, width, height, 0x00);
1784 }
1785
1786 static void gray_to_mono(AVPicture *dst, const AVPicture *src,
1787                          int width, int height, int xor_mask)
1788 {
1789     int n;
1790     const uint8_t *s;
1791     uint8_t *d;
1792     int j, b, v, n1, src_wrap, dst_wrap, y;
1793
1794     s = src->data[0];
1795     src_wrap = src->linesize[0] - width;
1796
1797     d = dst->data[0];
1798     dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1799
1800     for(y=0;y<height;y++) {
1801         n = width;
1802         while (n >= 8) {
1803             v = 0;
1804             for(j=0;j<8;j++) {
1805                 b = s[0];
1806                 s++;
1807                 v = (v << 1) | (b >> 7);
1808             }
1809             d[0] = v ^ xor_mask;
1810             d++;
1811             n -= 8;
1812         }
1813         if (n > 0) {
1814             n1 = n;
1815             v = 0;
1816             while (n > 0) {
1817                 b = s[0];
1818                 s++;
1819                 v = (v << 1) | (b >> 7);
1820                 n--;
1821             }
1822             d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1823             d++;
1824         }
1825         s += src_wrap;
1826         d += dst_wrap;
1827     }
1828 }
1829
1830 static void gray_to_monowhite(AVPicture *dst, const AVPicture *src,
1831                               int width, int height)
1832 {
1833     gray_to_mono(dst, src, width, height, 0xff);
1834 }
1835
1836 static void gray_to_monoblack(AVPicture *dst, const AVPicture *src,
1837                               int width, int height)
1838 {
1839     gray_to_mono(dst, src, width, height, 0x00);
1840 }
1841
1842 static void gray_to_gray16(AVPicture *dst, const AVPicture *src,
1843                               int width, int height)
1844 {
1845     int x, y, src_wrap, dst_wrap;
1846     uint8_t *s, *d;
1847     s = src->data[0];
1848     src_wrap = src->linesize[0] - width;
1849     d = dst->data[0];
1850     dst_wrap = dst->linesize[0] - width * 2;
1851     for(y=0; y<height; y++){
1852         for(x=0; x<width; x++){
1853             *d++ = *s;
1854             *d++ = *s++;
1855         }
1856         s += src_wrap;
1857         d += dst_wrap;
1858     }
1859 }
1860
1861 static void gray16_to_gray(AVPicture *dst, const AVPicture *src,
1862                               int width, int height)
1863 {
1864     int x, y, src_wrap, dst_wrap;
1865     uint8_t *s, *d;
1866     s = src->data[0];
1867     src_wrap = src->linesize[0] - width * 2;
1868     d = dst->data[0];
1869     dst_wrap = dst->linesize[0] - width;
1870     for(y=0; y<height; y++){
1871         for(x=0; x<width; x++){
1872             *d++ = *s;
1873             s += 2;
1874         }
1875         s += src_wrap;
1876         d += dst_wrap;
1877     }
1878 }
1879
1880 static void gray16be_to_gray(AVPicture *dst, const AVPicture *src,
1881                               int width, int height)
1882 {
1883     gray16_to_gray(dst, src, width, height);
1884 }
1885
1886 static void gray16le_to_gray(AVPicture *dst, const AVPicture *src,
1887                               int width, int height)
1888 {
1889     AVPicture tmpsrc = *src;
1890     tmpsrc.data[0]++;
1891     gray16_to_gray(dst, &tmpsrc, width, height);
1892 }
1893
1894 static void gray16_to_gray16(AVPicture *dst, const AVPicture *src,
1895                               int width, int height)
1896 {
1897     int x, y, src_wrap, dst_wrap;
1898     uint16_t *s, *d;
1899     s = src->data[0];
1900     src_wrap = (src->linesize[0] - width * 2)/2;
1901     d = dst->data[0];
1902     dst_wrap = (dst->linesize[0] - width * 2)/2;
1903     for(y=0; y<height; y++){
1904         for(x=0; x<width; x++){
1905             *d++ = bswap_16(*s++);
1906         }
1907         s += src_wrap;
1908         d += dst_wrap;
1909     }
1910 }
1911
1912
1913 typedef struct ConvertEntry {
1914     void (*convert)(AVPicture *dst,
1915                     const AVPicture *src, int width, int height);
1916 } ConvertEntry;
1917
1918 /* Add each new convertion function in this table. In order to be able
1919    to convert from any format to any format, the following constraints
1920    must be satisfied:
1921
1922    - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1923
1924    - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1925
1926    - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1927
1928    - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1929      PIX_FMT_RGB24.
1930
1931    - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1932
1933    The other conversion functions are just optimisations for common cases.
1934 */
1935 static const ConvertEntry convert_table[PIX_FMT_NB][PIX_FMT_NB] = {
1936     [PIX_FMT_YUV420P] = {
1937         [PIX_FMT_YUYV422] = {
1938             .convert = yuv420p_to_yuyv422,
1939         },
1940         [PIX_FMT_RGB555] = {
1941             .convert = yuv420p_to_rgb555
1942         },
1943         [PIX_FMT_RGB565] = {
1944             .convert = yuv420p_to_rgb565
1945         },
1946         [PIX_FMT_BGR24] = {
1947             .convert = yuv420p_to_bgr24
1948         },
1949         [PIX_FMT_RGB24] = {
1950             .convert = yuv420p_to_rgb24
1951         },
1952         [PIX_FMT_RGB32] = {
1953             .convert = yuv420p_to_rgb32
1954         },
1955         [PIX_FMT_UYVY422] = {
1956             .convert = yuv420p_to_uyvy422,
1957         },
1958     },
1959     [PIX_FMT_YUV422P] = {
1960         [PIX_FMT_YUYV422] = {
1961             .convert = yuv422p_to_yuyv422,
1962         },
1963         [PIX_FMT_UYVY422] = {
1964             .convert = yuv422p_to_uyvy422,
1965         },
1966     },
1967     [PIX_FMT_YUV444P] = {
1968         [PIX_FMT_RGB24] = {
1969             .convert = yuv444p_to_rgb24
1970         },
1971     },
1972     [PIX_FMT_YUVJ420P] = {
1973         [PIX_FMT_RGB555] = {
1974             .convert = yuvj420p_to_rgb555
1975         },
1976         [PIX_FMT_RGB565] = {
1977             .convert = yuvj420p_to_rgb565
1978         },
1979         [PIX_FMT_BGR24] = {
1980             .convert = yuvj420p_to_bgr24
1981         },
1982         [PIX_FMT_RGB24] = {
1983             .convert = yuvj420p_to_rgb24
1984         },
1985         [PIX_FMT_RGB32] = {
1986             .convert = yuvj420p_to_rgb32
1987         },
1988     },
1989     [PIX_FMT_YUVJ444P] = {
1990         [PIX_FMT_RGB24] = {
1991             .convert = yuvj444p_to_rgb24
1992         },
1993     },
1994     [PIX_FMT_YUYV422] = {
1995         [PIX_FMT_YUV420P] = {
1996             .convert = yuyv422_to_yuv420p,
1997         },
1998         [PIX_FMT_YUV422P] = {
1999             .convert = yuyv422_to_yuv422p,
2000         },
2001     },
2002     [PIX_FMT_UYVY422] = {
2003         [PIX_FMT_YUV420P] = {
2004             .convert = uyvy422_to_yuv420p,
2005         },
2006         [PIX_FMT_YUV422P] = {
2007             .convert = uyvy422_to_yuv422p,
2008         },
2009     },
2010     [PIX_FMT_RGB24] = {
2011         [PIX_FMT_YUV420P] = {
2012             .convert = rgb24_to_yuv420p
2013         },
2014         [PIX_FMT_RGB565] = {
2015             .convert = rgb24_to_rgb565
2016         },
2017         [PIX_FMT_RGB555] = {
2018             .convert = rgb24_to_rgb555
2019         },
2020         [PIX_FMT_RGB32] = {
2021             .convert = rgb24_to_rgb32
2022         },
2023         [PIX_FMT_BGR24] = {
2024             .convert = rgb24_to_bgr24
2025         },
2026         [PIX_FMT_GRAY8] = {
2027             .convert = rgb24_to_gray
2028         },
2029         [PIX_FMT_PAL8] = {
2030             .convert = rgb24_to_pal8
2031         },
2032         [PIX_FMT_YUV444P] = {
2033             .convert = rgb24_to_yuv444p
2034         },
2035         [PIX_FMT_YUVJ420P] = {
2036             .convert = rgb24_to_yuvj420p
2037         },
2038         [PIX_FMT_YUVJ444P] = {
2039             .convert = rgb24_to_yuvj444p
2040         },
2041     },
2042     [PIX_FMT_RGB32] = {
2043         [PIX_FMT_RGB24] = {
2044             .convert = rgb32_to_rgb24
2045         },
2046         [PIX_FMT_BGR24] = {
2047             .convert = rgb32_to_bgr24
2048         },
2049         [PIX_FMT_RGB565] = {
2050             .convert = rgb32_to_rgb565
2051         },
2052         [PIX_FMT_RGB555] = {
2053             .convert = rgb32_to_rgb555
2054         },
2055         [PIX_FMT_PAL8] = {
2056             .convert = rgb32_to_pal8
2057         },
2058         [PIX_FMT_YUV420P] = {
2059             .convert = rgb32_to_yuv420p
2060         },
2061         [PIX_FMT_GRAY8] = {
2062             .convert = rgb32_to_gray
2063         },
2064     },
2065     [PIX_FMT_BGR24] = {
2066         [PIX_FMT_RGB32] = {
2067             .convert = bgr24_to_rgb32
2068         },
2069         [PIX_FMT_RGB24] = {
2070             .convert = bgr24_to_rgb24
2071         },
2072         [PIX_FMT_YUV420P] = {
2073             .convert = bgr24_to_yuv420p
2074         },
2075         [PIX_FMT_GRAY8] = {
2076             .convert = bgr24_to_gray
2077         },
2078     },
2079     [PIX_FMT_RGB555] = {
2080         [PIX_FMT_RGB24] = {
2081             .convert = rgb555_to_rgb24
2082         },
2083         [PIX_FMT_RGB32] = {
2084             .convert = rgb555_to_rgb32
2085         },
2086         [PIX_FMT_YUV420P] = {
2087             .convert = rgb555_to_yuv420p
2088         },
2089         [PIX_FMT_GRAY8] = {
2090             .convert = rgb555_to_gray
2091         },
2092     },
2093     [PIX_FMT_RGB565] = {
2094         [PIX_FMT_RGB32] = {
2095             .convert = rgb565_to_rgb32
2096         },
2097         [PIX_FMT_RGB24] = {
2098             .convert = rgb565_to_rgb24
2099         },
2100         [PIX_FMT_YUV420P] = {
2101             .convert = rgb565_to_yuv420p
2102         },
2103         [PIX_FMT_GRAY8] = {
2104             .convert = rgb565_to_gray
2105         },
2106     },
2107     [PIX_FMT_GRAY16BE] = {
2108         [PIX_FMT_GRAY8] = {
2109             .convert = gray16be_to_gray
2110         },
2111         [PIX_FMT_GRAY16LE] = {
2112             .convert = gray16_to_gray16
2113         },
2114     },
2115     [PIX_FMT_GRAY16LE] = {
2116         [PIX_FMT_GRAY8] = {
2117             .convert = gray16le_to_gray
2118         },
2119         [PIX_FMT_GRAY16BE] = {
2120             .convert = gray16_to_gray16
2121         },
2122     },
2123     [PIX_FMT_GRAY8] = {
2124         [PIX_FMT_RGB555] = {
2125             .convert = gray_to_rgb555
2126         },
2127         [PIX_FMT_RGB565] = {
2128             .convert = gray_to_rgb565
2129         },
2130         [PIX_FMT_RGB24] = {
2131             .convert = gray_to_rgb24
2132         },
2133         [PIX_FMT_BGR24] = {
2134             .convert = gray_to_bgr24
2135         },
2136         [PIX_FMT_RGB32] = {
2137             .convert = gray_to_rgb32
2138         },
2139         [PIX_FMT_MONOWHITE] = {
2140             .convert = gray_to_monowhite
2141         },
2142         [PIX_FMT_MONOBLACK] = {
2143             .convert = gray_to_monoblack
2144         },
2145         [PIX_FMT_GRAY16LE] = {
2146             .convert = gray_to_gray16
2147         },
2148         [PIX_FMT_GRAY16BE] = {
2149             .convert = gray_to_gray16
2150         },
2151     },
2152     [PIX_FMT_MONOWHITE] = {
2153         [PIX_FMT_GRAY8] = {
2154             .convert = monowhite_to_gray
2155         },
2156     },
2157     [PIX_FMT_MONOBLACK] = {
2158         [PIX_FMT_GRAY8] = {
2159             .convert = monoblack_to_gray
2160         },
2161     },
2162     [PIX_FMT_PAL8] = {
2163         [PIX_FMT_RGB555] = {
2164             .convert = pal8_to_rgb555
2165         },
2166         [PIX_FMT_RGB565] = {
2167             .convert = pal8_to_rgb565
2168         },
2169         [PIX_FMT_BGR24] = {
2170             .convert = pal8_to_bgr24
2171         },
2172         [PIX_FMT_RGB24] = {
2173             .convert = pal8_to_rgb24
2174         },
2175         [PIX_FMT_RGB32] = {
2176             .convert = pal8_to_rgb32
2177         },
2178     },
2179     [PIX_FMT_UYYVYY411] = {
2180         [PIX_FMT_YUV411P] = {
2181             .convert = uyyvyy411_to_yuv411p,
2182         },
2183     },
2184
2185 };
2186
2187 int avpicture_alloc(AVPicture *picture,
2188                            int pix_fmt, int width, int height)
2189 {
2190     int size;
2191     void *ptr;
2192
2193     size = avpicture_get_size(pix_fmt, width, height);
2194     if(size<0)
2195         goto fail;
2196     ptr = av_malloc(size);
2197     if (!ptr)
2198         goto fail;
2199     avpicture_fill(picture, ptr, pix_fmt, width, height);
2200     return 0;
2201  fail:
2202     memset(picture, 0, sizeof(AVPicture));
2203     return -1;
2204 }
2205
2206 void avpicture_free(AVPicture *picture)
2207 {
2208     av_free(picture->data[0]);
2209 }
2210
2211 /* return true if yuv planar */
2212 static inline int is_yuv_planar(const PixFmtInfo *ps)
2213 {
2214     return (ps->color_type == FF_COLOR_YUV ||
2215             ps->color_type == FF_COLOR_YUV_JPEG) &&
2216         ps->pixel_type == FF_PIXEL_PLANAR;
2217 }
2218
2219 int av_picture_crop(AVPicture *dst, const AVPicture *src,
2220               int pix_fmt, int top_band, int left_band)
2221 {
2222     int y_shift;
2223     int x_shift;
2224
2225     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
2226         return -1;
2227
2228     y_shift = pix_fmt_info[pix_fmt].y_chroma_shift;
2229     x_shift = pix_fmt_info[pix_fmt].x_chroma_shift;
2230
2231     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
2232     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
2233     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
2234
2235     dst->linesize[0] = src->linesize[0];
2236     dst->linesize[1] = src->linesize[1];
2237     dst->linesize[2] = src->linesize[2];
2238     return 0;
2239 }
2240
2241 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2242             int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2243             int *color)
2244 {
2245     uint8_t *optr;
2246     int y_shift;
2247     int x_shift;
2248     int yheight;
2249     int i, y;
2250
2251     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
2252         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
2253
2254     for (i = 0; i < 3; i++) {
2255         x_shift = i ? pix_fmt_info[pix_fmt].x_chroma_shift : 0;
2256         y_shift = i ? pix_fmt_info[pix_fmt].y_chroma_shift : 0;
2257
2258         if (padtop || padleft) {
2259             memset(dst->data[i], color[i],
2260                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
2261         }
2262
2263         if (padleft || padright) {
2264             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2265                 (dst->linesize[i] - (padright >> x_shift));
2266             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2267             for (y = 0; y < yheight; y++) {
2268                 memset(optr, color[i], (padleft + padright) >> x_shift);
2269                 optr += dst->linesize[i];
2270             }
2271         }
2272
2273         if (src) { /* first line */
2274             uint8_t *iptr = src->data[i];
2275             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2276                     (padleft >> x_shift);
2277             memcpy(optr, iptr, src->linesize[i]);
2278             iptr += src->linesize[i];
2279             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
2280                 (dst->linesize[i] - (padright >> x_shift));
2281             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
2282             for (y = 0; y < yheight; y++) {
2283                 memset(optr, color[i], (padleft + padright) >> x_shift);
2284                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
2285                     src->linesize[i]);
2286                 iptr += src->linesize[i];
2287                 optr += dst->linesize[i];
2288             }
2289         }
2290
2291         if (padbottom || padright) {
2292             optr = dst->data[i] + dst->linesize[i] *
2293                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
2294             memset(optr, color[i],dst->linesize[i] *
2295                 (padbottom >> y_shift) + (padright >> x_shift));
2296         }
2297     }
2298     return 0;
2299 }
2300
2301 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2302 void img_copy(AVPicture *dst, const AVPicture *src,
2303               int pix_fmt, int width, int height)
2304 {
2305     av_picture_copy(dst, src, pix_fmt, width, height);
2306 }
2307
2308 int img_crop(AVPicture *dst, const AVPicture *src,
2309               int pix_fmt, int top_band, int left_band)
2310 {
2311     return av_picture_crop(dst, src, pix_fmt, top_band, left_band);
2312 }
2313
2314 int img_pad(AVPicture *dst, const AVPicture *src, int height, int width,
2315             int pix_fmt, int padtop, int padbottom, int padleft, int padright,
2316             int *color)
2317 {
2318     return av_picture_pad(dst, src, height, width, pix_fmt, padtop, padbottom, padleft, padright, color);
2319 }
2320 #endif
2321
2322 #ifndef CONFIG_SWSCALER
2323 /* XXX: always use linesize. Return -1 if not supported */
2324 int img_convert(AVPicture *dst, int dst_pix_fmt,
2325                 const AVPicture *src, int src_pix_fmt,
2326                 int src_width, int src_height)
2327 {
2328     static int inited;
2329     int i, ret, dst_width, dst_height, int_pix_fmt;
2330     const PixFmtInfo *src_pix, *dst_pix;
2331     const ConvertEntry *ce;
2332     AVPicture tmp1, *tmp = &tmp1;
2333
2334     if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2335         dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2336         return -1;
2337     if (src_width <= 0 || src_height <= 0)
2338         return 0;
2339
2340     if (!inited) {
2341         inited = 1;
2342         img_convert_init();
2343     }
2344
2345     dst_width = src_width;
2346     dst_height = src_height;
2347
2348     dst_pix = &pix_fmt_info[dst_pix_fmt];
2349     src_pix = &pix_fmt_info[src_pix_fmt];
2350     if (src_pix_fmt == dst_pix_fmt) {
2351         /* no conversion needed: just copy */
2352         av_picture_copy(dst, src, dst_pix_fmt, dst_width, dst_height);
2353         return 0;
2354     }
2355
2356     ce = &convert_table[src_pix_fmt][dst_pix_fmt];
2357     if (ce->convert) {
2358         /* specific conversion routine */
2359         ce->convert(dst, src, dst_width, dst_height);
2360         return 0;
2361     }
2362
2363     /* gray to YUV */
2364     if (is_yuv_planar(dst_pix) &&
2365         src_pix_fmt == PIX_FMT_GRAY8) {
2366         int w, h, y;
2367         uint8_t *d;
2368
2369         if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2370             ff_img_copy_plane(dst->data[0], dst->linesize[0],
2371                      src->data[0], src->linesize[0],
2372                      dst_width, dst_height);
2373         } else {
2374             img_apply_table(dst->data[0], dst->linesize[0],
2375                             src->data[0], src->linesize[0],
2376                             dst_width, dst_height,
2377                             y_jpeg_to_ccir);
2378         }
2379         /* fill U and V with 128 */
2380         w = dst_width;
2381         h = dst_height;
2382         w >>= dst_pix->x_chroma_shift;
2383         h >>= dst_pix->y_chroma_shift;
2384         for(i = 1; i <= 2; i++) {
2385             d = dst->data[i];
2386             for(y = 0; y< h; y++) {
2387                 memset(d, 128, w);
2388                 d += dst->linesize[i];
2389             }
2390         }
2391         return 0;
2392     }
2393
2394     /* YUV to gray */
2395     if (is_yuv_planar(src_pix) &&
2396         dst_pix_fmt == PIX_FMT_GRAY8) {
2397         if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2398             ff_img_copy_plane(dst->data[0], dst->linesize[0],
2399                      src->data[0], src->linesize[0],
2400                      dst_width, dst_height);
2401         } else {
2402             img_apply_table(dst->data[0], dst->linesize[0],
2403                             src->data[0], src->linesize[0],
2404                             dst_width, dst_height,
2405                             y_ccir_to_jpeg);
2406         }
2407         return 0;
2408     }
2409
2410     /* YUV to YUV planar */
2411     if (is_yuv_planar(dst_pix) && is_yuv_planar(src_pix)) {
2412         int x_shift, y_shift, w, h, xy_shift;
2413         void (*resize_func)(uint8_t *dst, int dst_wrap,
2414                             const uint8_t *src, int src_wrap,
2415                             int width, int height);
2416
2417         /* compute chroma size of the smallest dimensions */
2418         w = dst_width;
2419         h = dst_height;
2420         if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2421             w >>= dst_pix->x_chroma_shift;
2422         else
2423             w >>= src_pix->x_chroma_shift;
2424         if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2425             h >>= dst_pix->y_chroma_shift;
2426         else
2427             h >>= src_pix->y_chroma_shift;
2428
2429         x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2430         y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2431         xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2432         /* there must be filters for conversion at least from and to
2433            YUV444 format */
2434         switch(xy_shift) {
2435         case 0x00:
2436             resize_func = ff_img_copy_plane;
2437             break;
2438         case 0x10:
2439             resize_func = shrink21;
2440             break;
2441         case 0x20:
2442             resize_func = shrink41;
2443             break;
2444         case 0x01:
2445             resize_func = shrink12;
2446             break;
2447         case 0x11:
2448             resize_func = ff_shrink22;
2449             break;
2450         case 0x22:
2451             resize_func = ff_shrink44;
2452             break;
2453         case 0xf0:
2454             resize_func = grow21;
2455             break;
2456         case 0xe0:
2457             resize_func = grow41;
2458             break;
2459         case 0xff:
2460             resize_func = grow22;
2461             break;
2462         case 0xee:
2463             resize_func = grow44;
2464             break;
2465         case 0xf1:
2466             resize_func = conv411;
2467             break;
2468         default:
2469             /* currently not handled */
2470             goto no_chroma_filter;
2471         }
2472
2473         ff_img_copy_plane(dst->data[0], dst->linesize[0],
2474                        src->data[0], src->linesize[0],
2475                        dst_width, dst_height);
2476
2477         for(i = 1;i <= 2; i++)
2478             resize_func(dst->data[i], dst->linesize[i],
2479                         src->data[i], src->linesize[i],
2480                         dst_width>>dst_pix->x_chroma_shift, dst_height>>dst_pix->y_chroma_shift);
2481         /* if yuv color space conversion is needed, we do it here on
2482            the destination image */
2483         if (dst_pix->color_type != src_pix->color_type) {
2484             const uint8_t *y_table, *c_table;
2485             if (dst_pix->color_type == FF_COLOR_YUV) {
2486                 y_table = y_jpeg_to_ccir;
2487                 c_table = c_jpeg_to_ccir;
2488             } else {
2489                 y_table = y_ccir_to_jpeg;
2490                 c_table = c_ccir_to_jpeg;
2491             }
2492             img_apply_table(dst->data[0], dst->linesize[0],
2493                             dst->data[0], dst->linesize[0],
2494                             dst_width, dst_height,
2495                             y_table);
2496
2497             for(i = 1;i <= 2; i++)
2498                 img_apply_table(dst->data[i], dst->linesize[i],
2499                                 dst->data[i], dst->linesize[i],
2500                                 dst_width>>dst_pix->x_chroma_shift,
2501                                 dst_height>>dst_pix->y_chroma_shift,
2502                                 c_table);
2503         }
2504         return 0;
2505     }
2506  no_chroma_filter:
2507
2508     /* try to use an intermediate format */
2509     if (src_pix_fmt == PIX_FMT_YUYV422 ||
2510         dst_pix_fmt == PIX_FMT_YUYV422) {
2511         /* specific case: convert to YUV422P first */
2512         int_pix_fmt = PIX_FMT_YUV422P;
2513     } else if (src_pix_fmt == PIX_FMT_UYVY422 ||
2514         dst_pix_fmt == PIX_FMT_UYVY422) {
2515         /* specific case: convert to YUV422P first */
2516         int_pix_fmt = PIX_FMT_YUV422P;
2517     } else if (src_pix_fmt == PIX_FMT_UYYVYY411 ||
2518         dst_pix_fmt == PIX_FMT_UYYVYY411) {
2519         /* specific case: convert to YUV411P first */
2520         int_pix_fmt = PIX_FMT_YUV411P;
2521     } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2522                 src_pix_fmt != PIX_FMT_GRAY8) ||
2523                (dst_pix->color_type == FF_COLOR_GRAY &&
2524                 dst_pix_fmt != PIX_FMT_GRAY8)) {
2525         /* gray8 is the normalized format */
2526         int_pix_fmt = PIX_FMT_GRAY8;
2527     } else if ((is_yuv_planar(src_pix) &&
2528                 src_pix_fmt != PIX_FMT_YUV444P &&
2529                 src_pix_fmt != PIX_FMT_YUVJ444P)) {
2530         /* yuv444 is the normalized format */
2531         if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2532             int_pix_fmt = PIX_FMT_YUVJ444P;
2533         else
2534             int_pix_fmt = PIX_FMT_YUV444P;
2535     } else if ((is_yuv_planar(dst_pix) &&
2536                 dst_pix_fmt != PIX_FMT_YUV444P &&
2537                 dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2538         /* yuv444 is the normalized format */
2539         if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2540             int_pix_fmt = PIX_FMT_YUVJ444P;
2541         else
2542             int_pix_fmt = PIX_FMT_YUV444P;
2543     } else {
2544         /* the two formats are rgb or gray8 or yuv[j]444p */
2545         if (src_pix->is_alpha && dst_pix->is_alpha)
2546             int_pix_fmt = PIX_FMT_RGB32;
2547         else
2548             int_pix_fmt = PIX_FMT_RGB24;
2549     }
2550     if (avpicture_alloc(tmp, int_pix_fmt, dst_width, dst_height) < 0)
2551         return -1;
2552     ret = -1;
2553     if (img_convert(tmp, int_pix_fmt,
2554                     src, src_pix_fmt, src_width, src_height) < 0)
2555         goto fail1;
2556     if (img_convert(dst, dst_pix_fmt,
2557                     tmp, int_pix_fmt, dst_width, dst_height) < 0)
2558         goto fail1;
2559     ret = 0;
2560  fail1:
2561     avpicture_free(tmp);
2562     return ret;
2563 }
2564 #endif
2565
2566 /* NOTE: we scan all the pixels to have an exact information */
2567 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
2568 {
2569     const unsigned char *p;
2570     int src_wrap, ret, x, y;
2571     unsigned int a;
2572     uint32_t *palette = (uint32_t *)src->data[1];
2573
2574     p = src->data[0];
2575     src_wrap = src->linesize[0] - width;
2576     ret = 0;
2577     for(y=0;y<height;y++) {
2578         for(x=0;x<width;x++) {
2579             a = palette[p[0]] >> 24;
2580             if (a == 0x00) {
2581                 ret |= FF_ALPHA_TRANSP;
2582             } else if (a != 0xff) {
2583                 ret |= FF_ALPHA_SEMI_TRANSP;
2584             }
2585             p++;
2586         }
2587         p += src_wrap;
2588     }
2589     return ret;
2590 }
2591
2592 int img_get_alpha_info(const AVPicture *src,
2593                        int pix_fmt, int width, int height)
2594 {
2595     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
2596     int ret;
2597
2598     pf = &pix_fmt_info[pix_fmt];
2599     /* no alpha can be represented in format */
2600     if (!pf->is_alpha)
2601         return 0;
2602     switch(pix_fmt) {
2603     case PIX_FMT_RGB32:
2604         ret = get_alpha_info_rgb32(src, width, height);
2605         break;
2606     case PIX_FMT_PAL8:
2607         ret = get_alpha_info_pal8(src, width, height);
2608         break;
2609     default:
2610         /* we do not know, so everything is indicated */
2611         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2612         break;
2613     }
2614     return ret;
2615 }
2616
2617 #ifdef HAVE_MMX
2618 #define DEINT_INPLACE_LINE_LUM \
2619                     movd_m2r(lum_m4[0],mm0);\
2620                     movd_m2r(lum_m3[0],mm1);\
2621                     movd_m2r(lum_m2[0],mm2);\
2622                     movd_m2r(lum_m1[0],mm3);\
2623                     movd_m2r(lum[0],mm4);\
2624                     punpcklbw_r2r(mm7,mm0);\
2625                     movd_r2m(mm2,lum_m4[0]);\
2626                     punpcklbw_r2r(mm7,mm1);\
2627                     punpcklbw_r2r(mm7,mm2);\
2628                     punpcklbw_r2r(mm7,mm3);\
2629                     punpcklbw_r2r(mm7,mm4);\
2630                     paddw_r2r(mm3,mm1);\
2631                     psllw_i2r(1,mm2);\
2632                     paddw_r2r(mm4,mm0);\
2633                     psllw_i2r(2,mm1);\
2634                     paddw_r2r(mm6,mm2);\
2635                     paddw_r2r(mm2,mm1);\
2636                     psubusw_r2r(mm0,mm1);\
2637                     psrlw_i2r(3,mm1);\
2638                     packuswb_r2r(mm7,mm1);\
2639                     movd_r2m(mm1,lum_m2[0]);
2640
2641 #define DEINT_LINE_LUM \
2642                     movd_m2r(lum_m4[0],mm0);\
2643                     movd_m2r(lum_m3[0],mm1);\
2644                     movd_m2r(lum_m2[0],mm2);\
2645                     movd_m2r(lum_m1[0],mm3);\
2646                     movd_m2r(lum[0],mm4);\
2647                     punpcklbw_r2r(mm7,mm0);\
2648                     punpcklbw_r2r(mm7,mm1);\
2649                     punpcklbw_r2r(mm7,mm2);\
2650                     punpcklbw_r2r(mm7,mm3);\
2651                     punpcklbw_r2r(mm7,mm4);\
2652                     paddw_r2r(mm3,mm1);\
2653                     psllw_i2r(1,mm2);\
2654                     paddw_r2r(mm4,mm0);\
2655                     psllw_i2r(2,mm1);\
2656                     paddw_r2r(mm6,mm2);\
2657                     paddw_r2r(mm2,mm1);\
2658                     psubusw_r2r(mm0,mm1);\
2659                     psrlw_i2r(3,mm1);\
2660                     packuswb_r2r(mm7,mm1);\
2661                     movd_r2m(mm1,dst[0]);
2662 #endif
2663
2664 /* filter parameters: [-1 4 2 4 -1] // 8 */
2665 static void deinterlace_line(uint8_t *dst,
2666                              const uint8_t *lum_m4, const uint8_t *lum_m3,
2667                              const uint8_t *lum_m2, const uint8_t *lum_m1,
2668                              const uint8_t *lum,
2669                              int size)
2670 {
2671 #ifndef HAVE_MMX
2672     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2673     int sum;
2674
2675     for(;size > 0;size--) {
2676         sum = -lum_m4[0];
2677         sum += lum_m3[0] << 2;
2678         sum += lum_m2[0] << 1;
2679         sum += lum_m1[0] << 2;
2680         sum += -lum[0];
2681         dst[0] = cm[(sum + 4) >> 3];
2682         lum_m4++;
2683         lum_m3++;
2684         lum_m2++;
2685         lum_m1++;
2686         lum++;
2687         dst++;
2688     }
2689 #else
2690
2691     {
2692         mmx_t rounder;
2693         rounder.uw[0]=4;
2694         rounder.uw[1]=4;
2695         rounder.uw[2]=4;
2696         rounder.uw[3]=4;
2697         pxor_r2r(mm7,mm7);
2698         movq_m2r(rounder,mm6);
2699     }
2700     for (;size > 3; size-=4) {
2701         DEINT_LINE_LUM
2702         lum_m4+=4;
2703         lum_m3+=4;
2704         lum_m2+=4;
2705         lum_m1+=4;
2706         lum+=4;
2707         dst+=4;
2708     }
2709 #endif
2710 }
2711 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
2712                              int size)
2713 {
2714 #ifndef HAVE_MMX
2715     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
2716     int sum;
2717
2718     for(;size > 0;size--) {
2719         sum = -lum_m4[0];
2720         sum += lum_m3[0] << 2;
2721         sum += lum_m2[0] << 1;
2722         lum_m4[0]=lum_m2[0];
2723         sum += lum_m1[0] << 2;
2724         sum += -lum[0];
2725         lum_m2[0] = cm[(sum + 4) >> 3];
2726         lum_m4++;
2727         lum_m3++;
2728         lum_m2++;
2729         lum_m1++;
2730         lum++;
2731     }
2732 #else
2733
2734     {
2735         mmx_t rounder;
2736         rounder.uw[0]=4;
2737         rounder.uw[1]=4;
2738         rounder.uw[2]=4;
2739         rounder.uw[3]=4;
2740         pxor_r2r(mm7,mm7);
2741         movq_m2r(rounder,mm6);
2742     }
2743     for (;size > 3; size-=4) {
2744         DEINT_INPLACE_LINE_LUM
2745         lum_m4+=4;
2746         lum_m3+=4;
2747         lum_m2+=4;
2748         lum_m1+=4;
2749         lum+=4;
2750     }
2751 #endif
2752 }
2753
2754 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2755    top field is copied as is, but the bottom field is deinterlaced
2756    against the top field. */
2757 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
2758                                     const uint8_t *src1, int src_wrap,
2759                                     int width, int height)
2760 {
2761     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2762     int y;
2763
2764     src_m2 = src1;
2765     src_m1 = src1;
2766     src_0=&src_m1[src_wrap];
2767     src_p1=&src_0[src_wrap];
2768     src_p2=&src_p1[src_wrap];
2769     for(y=0;y<(height-2);y+=2) {
2770         memcpy(dst,src_m1,width);
2771         dst += dst_wrap;
2772         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
2773         src_m2 = src_0;
2774         src_m1 = src_p1;
2775         src_0 = src_p2;
2776         src_p1 += 2*src_wrap;
2777         src_p2 += 2*src_wrap;
2778         dst += dst_wrap;
2779     }
2780     memcpy(dst,src_m1,width);
2781     dst += dst_wrap;
2782     /* do last line */
2783     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
2784 }
2785
2786 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
2787                                              int width, int height)
2788 {
2789     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2790     int y;
2791     uint8_t *buf;
2792     buf = (uint8_t*)av_malloc(width);
2793
2794     src_m1 = src1;
2795     memcpy(buf,src_m1,width);
2796     src_0=&src_m1[src_wrap];
2797     src_p1=&src_0[src_wrap];
2798     src_p2=&src_p1[src_wrap];
2799     for(y=0;y<(height-2);y+=2) {
2800         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
2801         src_m1 = src_p1;
2802         src_0 = src_p2;
2803         src_p1 += 2*src_wrap;
2804         src_p2 += 2*src_wrap;
2805     }
2806     /* do last line */
2807     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
2808     av_free(buf);
2809 }
2810
2811 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
2812                           int pix_fmt, int width, int height)
2813 {
2814     int i;
2815
2816     if (pix_fmt != PIX_FMT_YUV420P &&
2817         pix_fmt != PIX_FMT_YUV422P &&
2818         pix_fmt != PIX_FMT_YUV444P &&
2819         pix_fmt != PIX_FMT_YUV411P)
2820         return -1;
2821     if ((width & 3) != 0 || (height & 3) != 0)
2822         return -1;
2823
2824     for(i=0;i<3;i++) {
2825         if (i == 1) {
2826             switch(pix_fmt) {
2827             case PIX_FMT_YUV420P:
2828                 width >>= 1;
2829                 height >>= 1;
2830                 break;
2831             case PIX_FMT_YUV422P:
2832                 width >>= 1;
2833                 break;
2834             case PIX_FMT_YUV411P:
2835                 width >>= 2;
2836                 break;
2837             default:
2838                 break;
2839             }
2840         }
2841         if (src == dst) {
2842             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
2843                                  width, height);
2844         } else {
2845             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
2846                                         src->data[i], src->linesize[i],
2847                                         width, height);
2848         }
2849     }
2850 #ifdef HAVE_MMX
2851     emms();
2852 #endif
2853     return 0;
2854 }
2855
2856 #undef FIX