]> git.sesse.net Git - ffmpeg/blob - libavcodec/imgconvert.c
Define static functions fill_image_linesize() and
[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
40 #if HAVE_MMX
41 #include "x86/mmx.h"
42 #include "x86/dsputil_mmx.h"
43 #endif
44
45 #define xglue(x, y) x ## y
46 #define glue(x, y) xglue(x, y)
47
48 #define FF_COLOR_RGB      0 /**< RGB color space */
49 #define FF_COLOR_GRAY     1 /**< gray color space */
50 #define FF_COLOR_YUV      2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
51 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
52
53 #define FF_PIXEL_PLANAR   0 /**< each channel has one component in AVPicture */
54 #define FF_PIXEL_PACKED   1 /**< only one components containing all the channels */
55 #define FF_PIXEL_PALETTE  2  /**< one components containing indexes for a palette */
56
57 typedef struct PixFmtInfo {
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 depth;           /**< bit depth of the color components */
63 } PixFmtInfo;
64
65 /* this table gives more information about formats */
66 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
67     /* YUV formats */
68     [PIX_FMT_YUV420P] = {
69         .nb_channels = 3,
70         .color_type = FF_COLOR_YUV,
71         .pixel_type = FF_PIXEL_PLANAR,
72         .depth = 8,
73     },
74     [PIX_FMT_YUV422P] = {
75         .nb_channels = 3,
76         .color_type = FF_COLOR_YUV,
77         .pixel_type = FF_PIXEL_PLANAR,
78         .depth = 8,
79     },
80     [PIX_FMT_YUV444P] = {
81         .nb_channels = 3,
82         .color_type = FF_COLOR_YUV,
83         .pixel_type = FF_PIXEL_PLANAR,
84         .depth = 8,
85     },
86     [PIX_FMT_YUYV422] = {
87         .nb_channels = 1,
88         .color_type = FF_COLOR_YUV,
89         .pixel_type = FF_PIXEL_PACKED,
90         .depth = 8,
91     },
92     [PIX_FMT_UYVY422] = {
93         .nb_channels = 1,
94         .color_type = FF_COLOR_YUV,
95         .pixel_type = FF_PIXEL_PACKED,
96         .depth = 8,
97     },
98     [PIX_FMT_YUV410P] = {
99         .nb_channels = 3,
100         .color_type = FF_COLOR_YUV,
101         .pixel_type = FF_PIXEL_PLANAR,
102         .depth = 8,
103     },
104     [PIX_FMT_YUV411P] = {
105         .nb_channels = 3,
106         .color_type = FF_COLOR_YUV,
107         .pixel_type = FF_PIXEL_PLANAR,
108         .depth = 8,
109     },
110     [PIX_FMT_YUV440P] = {
111         .nb_channels = 3,
112         .color_type = FF_COLOR_YUV,
113         .pixel_type = FF_PIXEL_PLANAR,
114         .depth = 8,
115     },
116     [PIX_FMT_YUV420P16LE] = {
117         .nb_channels = 3,
118         .color_type = FF_COLOR_YUV,
119         .pixel_type = FF_PIXEL_PLANAR,
120         .depth = 16,
121     },
122     [PIX_FMT_YUV422P16LE] = {
123         .nb_channels = 3,
124         .color_type = FF_COLOR_YUV,
125         .pixel_type = FF_PIXEL_PLANAR,
126         .depth = 16,
127     },
128     [PIX_FMT_YUV444P16LE] = {
129         .nb_channels = 3,
130         .color_type = FF_COLOR_YUV,
131         .pixel_type = FF_PIXEL_PLANAR,
132         .depth = 16,
133     },
134     [PIX_FMT_YUV420P16BE] = {
135         .nb_channels = 3,
136         .color_type = FF_COLOR_YUV,
137         .pixel_type = FF_PIXEL_PLANAR,
138         .depth = 16,
139     },
140     [PIX_FMT_YUV422P16BE] = {
141         .nb_channels = 3,
142         .color_type = FF_COLOR_YUV,
143         .pixel_type = FF_PIXEL_PLANAR,
144         .depth = 16,
145     },
146     [PIX_FMT_YUV444P16BE] = {
147         .nb_channels = 3,
148         .color_type = FF_COLOR_YUV,
149         .pixel_type = FF_PIXEL_PLANAR,
150         .depth = 16,
151     },
152
153
154     /* YUV formats with alpha plane */
155     [PIX_FMT_YUVA420P] = {
156         .nb_channels = 4,
157         .color_type = FF_COLOR_YUV,
158         .pixel_type = FF_PIXEL_PLANAR,
159         .depth = 8,
160     },
161
162     /* JPEG YUV */
163     [PIX_FMT_YUVJ420P] = {
164         .nb_channels = 3,
165         .color_type = FF_COLOR_YUV_JPEG,
166         .pixel_type = FF_PIXEL_PLANAR,
167         .depth = 8,
168     },
169     [PIX_FMT_YUVJ422P] = {
170         .nb_channels = 3,
171         .color_type = FF_COLOR_YUV_JPEG,
172         .pixel_type = FF_PIXEL_PLANAR,
173         .depth = 8,
174     },
175     [PIX_FMT_YUVJ444P] = {
176         .nb_channels = 3,
177         .color_type = FF_COLOR_YUV_JPEG,
178         .pixel_type = FF_PIXEL_PLANAR,
179         .depth = 8,
180     },
181     [PIX_FMT_YUVJ440P] = {
182         .nb_channels = 3,
183         .color_type = FF_COLOR_YUV_JPEG,
184         .pixel_type = FF_PIXEL_PLANAR,
185         .depth = 8,
186     },
187
188     /* RGB formats */
189     [PIX_FMT_RGB24] = {
190         .nb_channels = 3,
191         .color_type = FF_COLOR_RGB,
192         .pixel_type = FF_PIXEL_PACKED,
193         .depth = 8,
194     },
195     [PIX_FMT_BGR24] = {
196         .nb_channels = 3,
197         .color_type = FF_COLOR_RGB,
198         .pixel_type = FF_PIXEL_PACKED,
199         .depth = 8,
200     },
201     [PIX_FMT_ARGB] = {
202         .nb_channels = 4, .is_alpha = 1,
203         .color_type = FF_COLOR_RGB,
204         .pixel_type = FF_PIXEL_PACKED,
205         .depth = 8,
206     },
207     [PIX_FMT_RGB48BE] = {
208         .nb_channels = 3,
209         .color_type = FF_COLOR_RGB,
210         .pixel_type = FF_PIXEL_PACKED,
211         .depth = 16,
212     },
213     [PIX_FMT_RGB48LE] = {
214         .nb_channels = 3,
215         .color_type = FF_COLOR_RGB,
216         .pixel_type = FF_PIXEL_PACKED,
217         .depth = 16,
218     },
219     [PIX_FMT_RGB565BE] = {
220         .nb_channels = 3,
221         .color_type = FF_COLOR_RGB,
222         .pixel_type = FF_PIXEL_PACKED,
223         .depth = 5,
224     },
225     [PIX_FMT_RGB565LE] = {
226         .nb_channels = 3,
227         .color_type = FF_COLOR_RGB,
228         .pixel_type = FF_PIXEL_PACKED,
229         .depth = 5,
230     },
231     [PIX_FMT_RGB555BE] = {
232         .nb_channels = 3,
233         .color_type = FF_COLOR_RGB,
234         .pixel_type = FF_PIXEL_PACKED,
235         .depth = 5,
236     },
237     [PIX_FMT_RGB555LE] = {
238         .nb_channels = 3,
239         .color_type = FF_COLOR_RGB,
240         .pixel_type = FF_PIXEL_PACKED,
241         .depth = 5,
242     },
243     [PIX_FMT_RGB444BE] = {
244         .nb_channels = 3,
245         .color_type = FF_COLOR_RGB,
246         .pixel_type = FF_PIXEL_PACKED,
247         .depth = 4,
248     },
249     [PIX_FMT_RGB444LE] = {
250         .nb_channels = 3,
251         .color_type = FF_COLOR_RGB,
252         .pixel_type = FF_PIXEL_PACKED,
253         .depth = 4,
254     },
255
256     /* gray / mono formats */
257     [PIX_FMT_GRAY16BE] = {
258         .nb_channels = 1,
259         .color_type = FF_COLOR_GRAY,
260         .pixel_type = FF_PIXEL_PLANAR,
261         .depth = 16,
262     },
263     [PIX_FMT_GRAY16LE] = {
264         .nb_channels = 1,
265         .color_type = FF_COLOR_GRAY,
266         .pixel_type = FF_PIXEL_PLANAR,
267         .depth = 16,
268     },
269     [PIX_FMT_GRAY8] = {
270         .nb_channels = 1,
271         .color_type = FF_COLOR_GRAY,
272         .pixel_type = FF_PIXEL_PLANAR,
273         .depth = 8,
274     },
275     [PIX_FMT_MONOWHITE] = {
276         .nb_channels = 1,
277         .color_type = FF_COLOR_GRAY,
278         .pixel_type = FF_PIXEL_PLANAR,
279         .depth = 1,
280     },
281     [PIX_FMT_MONOBLACK] = {
282         .nb_channels = 1,
283         .color_type = FF_COLOR_GRAY,
284         .pixel_type = FF_PIXEL_PLANAR,
285         .depth = 1,
286     },
287
288     /* paletted formats */
289     [PIX_FMT_PAL8] = {
290         .nb_channels = 4, .is_alpha = 1,
291         .color_type = FF_COLOR_RGB,
292         .pixel_type = FF_PIXEL_PALETTE,
293         .depth = 8,
294     },
295     [PIX_FMT_UYYVYY411] = {
296         .nb_channels = 1,
297         .color_type = FF_COLOR_YUV,
298         .pixel_type = FF_PIXEL_PACKED,
299         .depth = 8,
300     },
301     [PIX_FMT_ABGR] = {
302         .nb_channels = 4, .is_alpha = 1,
303         .color_type = FF_COLOR_RGB,
304         .pixel_type = FF_PIXEL_PACKED,
305         .depth = 8,
306     },
307     [PIX_FMT_BGR565BE] = {
308         .nb_channels = 3,
309         .color_type = FF_COLOR_RGB,
310         .pixel_type = FF_PIXEL_PACKED,
311         .depth = 5,
312     },
313     [PIX_FMT_BGR565LE] = {
314         .nb_channels = 3,
315         .color_type = FF_COLOR_RGB,
316         .pixel_type = FF_PIXEL_PACKED,
317         .depth = 5,
318     },
319     [PIX_FMT_BGR555BE] = {
320         .nb_channels = 3,
321         .color_type = FF_COLOR_RGB,
322         .pixel_type = FF_PIXEL_PACKED,
323         .depth = 5,
324     },
325     [PIX_FMT_BGR555LE] = {
326         .nb_channels = 3,
327         .color_type = FF_COLOR_RGB,
328         .pixel_type = FF_PIXEL_PACKED,
329         .depth = 5,
330     },
331     [PIX_FMT_BGR444BE] = {
332         .nb_channels = 3,
333         .color_type = FF_COLOR_RGB,
334         .pixel_type = FF_PIXEL_PACKED,
335         .depth = 4,
336     },
337     [PIX_FMT_BGR444LE] = {
338         .nb_channels = 3,
339         .color_type = FF_COLOR_RGB,
340         .pixel_type = FF_PIXEL_PACKED,
341         .depth = 4,
342     },
343     [PIX_FMT_RGB8] = {
344         .nb_channels = 1,
345         .color_type = FF_COLOR_RGB,
346         .pixel_type = FF_PIXEL_PACKED,
347         .depth = 8,
348     },
349     [PIX_FMT_RGB4] = {
350         .nb_channels = 1,
351         .color_type = FF_COLOR_RGB,
352         .pixel_type = FF_PIXEL_PACKED,
353         .depth = 4,
354     },
355     [PIX_FMT_RGB4_BYTE] = {
356         .nb_channels = 1,
357         .color_type = FF_COLOR_RGB,
358         .pixel_type = FF_PIXEL_PACKED,
359         .depth = 8,
360     },
361     [PIX_FMT_BGR8] = {
362         .nb_channels = 1,
363         .color_type = FF_COLOR_RGB,
364         .pixel_type = FF_PIXEL_PACKED,
365         .depth = 8,
366     },
367     [PIX_FMT_BGR4] = {
368         .nb_channels = 1,
369         .color_type = FF_COLOR_RGB,
370         .pixel_type = FF_PIXEL_PACKED,
371         .depth = 4,
372     },
373     [PIX_FMT_BGR4_BYTE] = {
374         .nb_channels = 1,
375         .color_type = FF_COLOR_RGB,
376         .pixel_type = FF_PIXEL_PACKED,
377         .depth = 8,
378     },
379     [PIX_FMT_NV12] = {
380         .nb_channels = 2,
381         .color_type = FF_COLOR_YUV,
382         .pixel_type = FF_PIXEL_PLANAR,
383         .depth = 8,
384     },
385     [PIX_FMT_NV21] = {
386         .nb_channels = 2,
387         .color_type = FF_COLOR_YUV,
388         .pixel_type = FF_PIXEL_PLANAR,
389         .depth = 8,
390     },
391
392     [PIX_FMT_BGRA] = {
393         .nb_channels = 4, .is_alpha = 1,
394         .color_type = FF_COLOR_RGB,
395         .pixel_type = FF_PIXEL_PACKED,
396         .depth = 8,
397     },
398     [PIX_FMT_RGBA] = {
399         .nb_channels = 4, .is_alpha = 1,
400         .color_type = FF_COLOR_RGB,
401         .pixel_type = FF_PIXEL_PACKED,
402         .depth = 8,
403     },
404 };
405
406 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
407 {
408     *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
409     *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
410 }
411
412 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
413 {
414     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
415         return NULL;
416     else
417         return av_pix_fmt_descriptors[pix_fmt].name;
418 }
419
420 #if LIBAVCODEC_VERSION_MAJOR < 53
421 enum PixelFormat avcodec_get_pix_fmt(const char *name)
422 {
423     return av_get_pix_fmt(name);
424 }
425 #endif
426
427 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
428 {
429     /* print header */
430     if (pix_fmt < 0)
431         snprintf (buf, buf_size,
432                   "name      " " nb_channels" " depth" " is_alpha"
433             );
434     else{
435         PixFmtInfo info= pix_fmt_info[pix_fmt];
436
437         char is_alpha_char= info.is_alpha ? 'y' : 'n';
438
439         snprintf (buf, buf_size,
440                   "%-11s %5d %9d %6c",
441                   av_pix_fmt_descriptors[pix_fmt].name,
442                   info.nb_channels,
443                   info.depth,
444                   is_alpha_char
445             );
446     }
447 }
448
449 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
450 {
451     return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
452 }
453
454 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
455     int i;
456
457     for(i=0; i<256; i++){
458         int r,g,b;
459
460         switch(pix_fmt) {
461         case PIX_FMT_RGB8:
462             r= (i>>5    )*36;
463             g= ((i>>2)&7)*36;
464             b= (i&3     )*85;
465             break;
466         case PIX_FMT_BGR8:
467             b= (i>>6    )*85;
468             g= ((i>>3)&7)*36;
469             r= (i&7     )*36;
470             break;
471         case PIX_FMT_RGB4_BYTE:
472             r= (i>>3    )*255;
473             g= ((i>>1)&3)*85;
474             b= (i&1     )*255;
475             break;
476         case PIX_FMT_BGR4_BYTE:
477             b= (i>>3    )*255;
478             g= ((i>>1)&3)*85;
479             r= (i&1     )*255;
480             break;
481         case PIX_FMT_GRAY8:
482             r=b=g= i;
483             break;
484         default:
485             return -1;
486         }
487         pal[i] =  b + (g<<8) + (r<<16);
488     }
489
490     return 0;
491 }
492
493 static int fill_image_linesize(int linesize[4], enum PixelFormat pix_fmt, int width)
494 {
495     int i;
496     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
497     int max_plane_step     [4];
498     int max_plane_step_comp[4];
499
500     memset(linesize, 0, 4*sizeof(linesize[0]));
501
502     if (desc->flags & PIX_FMT_HWACCEL)
503         return -1;
504
505     if (desc->flags & PIX_FMT_BITSTREAM) {
506         linesize[0] = (width * (desc->comp[0].step_minus1+1) + 7) >> 3;
507         return 0;
508     }
509
510     memset(max_plane_step     , 0, sizeof(max_plane_step     ));
511     memset(max_plane_step_comp, 0, sizeof(max_plane_step_comp));
512     for (i = 0; i < 4; i++) {
513         const AVComponentDescriptor *comp = &(desc->comp[i]);
514         if ((comp->step_minus1+1) > max_plane_step[comp->plane]) {
515             max_plane_step     [comp->plane] = comp->step_minus1+1;
516             max_plane_step_comp[comp->plane] = i;
517         }
518     }
519
520     for (i = 0; i < 4; i++) {
521         int s = (max_plane_step_comp[i] == 1 || max_plane_step_comp[i] == 2) ? desc->log2_chroma_w : 0;
522         linesize[i] = max_plane_step[i] * (((width + (1 << s) - 1)) >> s);
523     }
524
525     return 0;
526 }
527
528 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
529 {
530     return fill_image_linesize(picture->linesize, pix_fmt, width);
531 }
532
533 static int fill_image_data_ptr(uint8_t *data[4], uint8_t *ptr, enum PixelFormat pix_fmt,
534                                int height, const int linesize[4])
535 {
536     int size, h2, size2;
537     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
538
539     size = linesize[0] * height;
540     switch(pix_fmt) {
541     case PIX_FMT_YUV420P:
542     case PIX_FMT_YUV422P:
543     case PIX_FMT_YUV444P:
544     case PIX_FMT_YUV410P:
545     case PIX_FMT_YUV411P:
546     case PIX_FMT_YUV440P:
547     case PIX_FMT_YUVJ420P:
548     case PIX_FMT_YUVJ422P:
549     case PIX_FMT_YUVJ444P:
550     case PIX_FMT_YUVJ440P:
551     case PIX_FMT_YUV420P16LE:
552     case PIX_FMT_YUV422P16LE:
553     case PIX_FMT_YUV444P16LE:
554     case PIX_FMT_YUV420P16BE:
555     case PIX_FMT_YUV422P16BE:
556     case PIX_FMT_YUV444P16BE:
557         h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
558         size2 = linesize[1] * h2;
559         data[0] = ptr;
560         data[1] = data[0] + size;
561         data[2] = data[1] + size2;
562         data[3] = NULL;
563         return size + 2 * size2;
564     case PIX_FMT_YUVA420P:
565         h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
566         size2 = linesize[1] * h2;
567         data[0] = ptr;
568         data[1] = data[0] + size;
569         data[2] = data[1] + size2;
570         data[3] = data[1] + size2 + size2;
571         return 2 * size + 2 * size2;
572     case PIX_FMT_NV12:
573     case PIX_FMT_NV21:
574         h2 = (height + (1 << desc->log2_chroma_h) - 1) >> desc->log2_chroma_h;
575         size2 = linesize[1] * h2;
576         data[0] = ptr;
577         data[1] = data[0] + size;
578         data[2] = NULL;
579         data[3] = NULL;
580         return size + size2;
581     case PIX_FMT_RGB24:
582     case PIX_FMT_BGR24:
583     case PIX_FMT_ARGB:
584     case PIX_FMT_ABGR:
585     case PIX_FMT_RGBA:
586     case PIX_FMT_BGRA:
587     case PIX_FMT_RGB48BE:
588     case PIX_FMT_RGB48LE:
589     case PIX_FMT_GRAY16BE:
590     case PIX_FMT_GRAY16LE:
591     case PIX_FMT_BGR444BE:
592     case PIX_FMT_BGR444LE:
593     case PIX_FMT_BGR555BE:
594     case PIX_FMT_BGR555LE:
595     case PIX_FMT_BGR565BE:
596     case PIX_FMT_BGR565LE:
597     case PIX_FMT_RGB444BE:
598     case PIX_FMT_RGB444LE:
599     case PIX_FMT_RGB555BE:
600     case PIX_FMT_RGB555LE:
601     case PIX_FMT_RGB565BE:
602     case PIX_FMT_RGB565LE:
603     case PIX_FMT_YUYV422:
604     case PIX_FMT_UYVY422:
605     case PIX_FMT_UYYVYY411:
606     case PIX_FMT_RGB4:
607     case PIX_FMT_BGR4:
608     case PIX_FMT_MONOWHITE:
609     case PIX_FMT_MONOBLACK:
610     case PIX_FMT_Y400A:
611         data[0] = ptr;
612         data[1] = NULL;
613         data[2] = NULL;
614         data[3] = NULL;
615         return size;
616     case PIX_FMT_PAL8:
617     case PIX_FMT_RGB8:
618     case PIX_FMT_BGR8:
619     case PIX_FMT_RGB4_BYTE:
620     case PIX_FMT_BGR4_BYTE:
621     case PIX_FMT_GRAY8:
622         size2 = (size + 3) & ~3;
623         data[0] = ptr;
624         data[1] = ptr + size2; /* palette is stored here as 256 32 bit words */
625         data[2] = NULL;
626         data[3] = NULL;
627         return size2 + 256 * 4;
628     default:
629         data[0] = NULL;
630         data[1] = NULL;
631         data[2] = NULL;
632         data[3] = NULL;
633         return -1;
634     }
635 }
636
637 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
638                     int height)
639 {
640     return fill_image_data_ptr(picture->data, ptr, pix_fmt, height, picture->linesize);
641 }
642
643 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
644                    enum PixelFormat pix_fmt, int width, int height)
645 {
646
647     if(avcodec_check_dimensions(NULL, width, height))
648         return -1;
649
650     if (ff_fill_linesize(picture, pix_fmt, width))
651         return -1;
652
653     return ff_fill_pointer(picture, ptr, pix_fmt, height);
654 }
655
656 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
657                      unsigned char *dest, int dest_size)
658 {
659     const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
660     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
661     int i, j, w, ow, h, oh, data_planes;
662     const unsigned char* s;
663     int size = avpicture_get_size(pix_fmt, width, height);
664
665     if (size > dest_size || size < 0)
666         return -1;
667
668     if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
669         if (pix_fmt == PIX_FMT_YUYV422 ||
670             pix_fmt == PIX_FMT_UYVY422 ||
671             pix_fmt == PIX_FMT_BGR565BE ||
672             pix_fmt == PIX_FMT_BGR565LE ||
673             pix_fmt == PIX_FMT_BGR555BE ||
674             pix_fmt == PIX_FMT_BGR555LE ||
675             pix_fmt == PIX_FMT_BGR444BE ||
676             pix_fmt == PIX_FMT_BGR444LE ||
677             pix_fmt == PIX_FMT_RGB565BE ||
678             pix_fmt == PIX_FMT_RGB565LE ||
679             pix_fmt == PIX_FMT_RGB555BE ||
680             pix_fmt == PIX_FMT_RGB555LE ||
681             pix_fmt == PIX_FMT_RGB444BE ||
682             pix_fmt == PIX_FMT_RGB444LE)
683             w = width * 2;
684         else if (pix_fmt == PIX_FMT_UYYVYY411)
685             w = width + width/2;
686         else if (pix_fmt == PIX_FMT_PAL8)
687             w = width;
688         else
689             w = width * (pf->depth * pf->nb_channels / 8);
690
691         data_planes = 1;
692         h = height;
693     } else {
694         data_planes = pf->nb_channels;
695         w = (width*pf->depth + 7)/8;
696         h = height;
697     }
698
699     ow = w;
700     oh = h;
701
702     for (i=0; i<data_planes; i++) {
703         if (i == 1) {
704             w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
705             h = -((-height) >> desc->log2_chroma_h);
706             if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
707                 w <<= 1;
708         } else if (i == 3) {
709             w = ow;
710             h = oh;
711         }
712         s = src->data[i];
713         for(j=0; j<h; j++) {
714             memcpy(dest, s, w);
715             dest += w;
716             s += src->linesize[i];
717         }
718     }
719
720     if (pf->pixel_type == FF_PIXEL_PALETTE)
721         memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
722
723     return size;
724 }
725
726 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
727 {
728     AVPicture dummy_pict;
729     if(avcodec_check_dimensions(NULL, width, height))
730         return -1;
731     switch (pix_fmt) {
732     case PIX_FMT_RGB8:
733     case PIX_FMT_BGR8:
734     case PIX_FMT_RGB4_BYTE:
735     case PIX_FMT_BGR4_BYTE:
736     case PIX_FMT_GRAY8:
737         // do not include palette for these pseudo-paletted formats
738         return width * height;
739     }
740     return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
741 }
742
743 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
744                              int has_alpha)
745 {
746     const PixFmtInfo *pf, *ps;
747     const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
748     const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
749     int loss;
750
751     ps = &pix_fmt_info[src_pix_fmt];
752
753     /* compute loss */
754     loss = 0;
755     pf = &pix_fmt_info[dst_pix_fmt];
756     if (pf->depth < ps->depth ||
757         ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
758           dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
759          (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
760           src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
761         loss |= FF_LOSS_DEPTH;
762     if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
763         dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
764         loss |= FF_LOSS_RESOLUTION;
765     switch(pf->color_type) {
766     case FF_COLOR_RGB:
767         if (ps->color_type != FF_COLOR_RGB &&
768             ps->color_type != FF_COLOR_GRAY)
769             loss |= FF_LOSS_COLORSPACE;
770         break;
771     case FF_COLOR_GRAY:
772         if (ps->color_type != FF_COLOR_GRAY)
773             loss |= FF_LOSS_COLORSPACE;
774         break;
775     case FF_COLOR_YUV:
776         if (ps->color_type != FF_COLOR_YUV)
777             loss |= FF_LOSS_COLORSPACE;
778         break;
779     case FF_COLOR_YUV_JPEG:
780         if (ps->color_type != FF_COLOR_YUV_JPEG &&
781             ps->color_type != FF_COLOR_YUV &&
782             ps->color_type != FF_COLOR_GRAY)
783             loss |= FF_LOSS_COLORSPACE;
784         break;
785     default:
786         /* fail safe test */
787         if (ps->color_type != pf->color_type)
788             loss |= FF_LOSS_COLORSPACE;
789         break;
790     }
791     if (pf->color_type == FF_COLOR_GRAY &&
792         ps->color_type != FF_COLOR_GRAY)
793         loss |= FF_LOSS_CHROMA;
794     if (!pf->is_alpha && (ps->is_alpha && has_alpha))
795         loss |= FF_LOSS_ALPHA;
796     if (pf->pixel_type == FF_PIXEL_PALETTE &&
797         (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
798         loss |= FF_LOSS_COLORQUANT;
799     return loss;
800 }
801
802 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
803 {
804     int bits;
805     const PixFmtInfo *pf;
806     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
807
808     pf = &pix_fmt_info[pix_fmt];
809     switch(pf->pixel_type) {
810     case FF_PIXEL_PACKED:
811         switch(pix_fmt) {
812         case PIX_FMT_YUYV422:
813         case PIX_FMT_UYVY422:
814         case PIX_FMT_RGB565BE:
815         case PIX_FMT_RGB565LE:
816         case PIX_FMT_RGB555BE:
817         case PIX_FMT_RGB555LE:
818         case PIX_FMT_RGB444BE:
819         case PIX_FMT_RGB444LE:
820         case PIX_FMT_BGR565BE:
821         case PIX_FMT_BGR565LE:
822         case PIX_FMT_BGR555BE:
823         case PIX_FMT_BGR555LE:
824         case PIX_FMT_BGR444BE:
825         case PIX_FMT_BGR444LE:
826             bits = 16;
827             break;
828         case PIX_FMT_UYYVYY411:
829             bits = 12;
830             break;
831         default:
832             bits = pf->depth * pf->nb_channels;
833             break;
834         }
835         break;
836     case FF_PIXEL_PLANAR:
837         if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
838             bits = pf->depth * pf->nb_channels;
839         } else {
840             bits = pf->depth + ((2 * pf->depth) >>
841                                 (desc->log2_chroma_w + desc->log2_chroma_h));
842         }
843         break;
844     case FF_PIXEL_PALETTE:
845         bits = 8;
846         break;
847     default:
848         bits = -1;
849         break;
850     }
851     return bits;
852 }
853
854 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
855                                       enum PixelFormat src_pix_fmt,
856                                       int has_alpha,
857                                       int loss_mask)
858 {
859     int dist, i, loss, min_dist;
860     enum PixelFormat dst_pix_fmt;
861
862     /* find exact color match with smallest size */
863     dst_pix_fmt = PIX_FMT_NONE;
864     min_dist = 0x7fffffff;
865     for(i = 0;i < PIX_FMT_NB; i++) {
866         if (pix_fmt_mask & (1ULL << i)) {
867             loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
868             if (loss == 0) {
869                 dist = avg_bits_per_pixel(i);
870                 if (dist < min_dist) {
871                     min_dist = dist;
872                     dst_pix_fmt = i;
873                 }
874             }
875         }
876     }
877     return dst_pix_fmt;
878 }
879
880 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
881                               int has_alpha, int *loss_ptr)
882 {
883     enum PixelFormat dst_pix_fmt;
884     int loss_mask, i;
885     static const int loss_mask_order[] = {
886         ~0, /* no loss first */
887         ~FF_LOSS_ALPHA,
888         ~FF_LOSS_RESOLUTION,
889         ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
890         ~FF_LOSS_COLORQUANT,
891         ~FF_LOSS_DEPTH,
892         0,
893     };
894
895     /* try with successive loss */
896     i = 0;
897     for(;;) {
898         loss_mask = loss_mask_order[i++];
899         dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
900                                                  has_alpha, loss_mask);
901         if (dst_pix_fmt >= 0)
902             goto found;
903         if (loss_mask == 0)
904             break;
905     }
906     return PIX_FMT_NONE;
907  found:
908     if (loss_ptr)
909         *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
910     return dst_pix_fmt;
911 }
912
913 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
914                            const uint8_t *src, int src_wrap,
915                            int width, int height)
916 {
917     if((!dst) || (!src))
918         return;
919     for(;height > 0; height--) {
920         memcpy(dst, src, width);
921         dst += dst_wrap;
922         src += src_wrap;
923     }
924 }
925
926 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
927 {
928     int bits;
929     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
930     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
931
932     pf = &pix_fmt_info[pix_fmt];
933     switch(pf->pixel_type) {
934     case FF_PIXEL_PACKED:
935         switch(pix_fmt) {
936         case PIX_FMT_YUYV422:
937         case PIX_FMT_UYVY422:
938         case PIX_FMT_RGB565BE:
939         case PIX_FMT_RGB565LE:
940         case PIX_FMT_RGB555BE:
941         case PIX_FMT_RGB555LE:
942         case PIX_FMT_RGB444BE:
943         case PIX_FMT_RGB444LE:
944         case PIX_FMT_BGR565BE:
945         case PIX_FMT_BGR565LE:
946         case PIX_FMT_BGR555BE:
947         case PIX_FMT_BGR555LE:
948         case PIX_FMT_BGR444BE:
949         case PIX_FMT_BGR444LE:
950             bits = 16;
951             break;
952         case PIX_FMT_UYYVYY411:
953             bits = 12;
954             break;
955         default:
956             bits = pf->depth * pf->nb_channels;
957             break;
958         }
959         return (width * bits + 7) >> 3;
960         break;
961     case FF_PIXEL_PLANAR:
962             if ((pix_fmt != PIX_FMT_NV12 && pix_fmt != PIX_FMT_NV21) &&
963                 (plane == 1 || plane == 2))
964                 width= -((-width)>>desc->log2_chroma_w);
965
966             return (width * pf->depth + 7) >> 3;
967         break;
968     case FF_PIXEL_PALETTE:
969         if (plane == 0)
970             return width;
971         break;
972     }
973
974     return -1;
975 }
976
977 void av_picture_copy(AVPicture *dst, const AVPicture *src,
978                      enum PixelFormat pix_fmt, int width, int height)
979 {
980     int i;
981     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
982     const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
983
984     switch(pf->pixel_type) {
985     case FF_PIXEL_PACKED:
986     case FF_PIXEL_PLANAR:
987         for(i = 0; i < pf->nb_channels; i++) {
988             int h;
989             int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
990             h = height;
991             if (i == 1 || i == 2) {
992                 h= -((-height)>>desc->log2_chroma_h);
993             }
994             ff_img_copy_plane(dst->data[i], dst->linesize[i],
995                            src->data[i], src->linesize[i],
996                            bwidth, h);
997         }
998         break;
999     case FF_PIXEL_PALETTE:
1000         ff_img_copy_plane(dst->data[0], dst->linesize[0],
1001                        src->data[0], src->linesize[0],
1002                        width, height);
1003         /* copy the palette */
1004         memcpy(dst->data[1], src->data[1], 4*256);
1005         break;
1006     }
1007 }
1008
1009 /* 2x2 -> 1x1 */
1010 void ff_shrink22(uint8_t *dst, int dst_wrap,
1011                      const uint8_t *src, int src_wrap,
1012                      int width, int height)
1013 {
1014     int w;
1015     const uint8_t *s1, *s2;
1016     uint8_t *d;
1017
1018     for(;height > 0; height--) {
1019         s1 = src;
1020         s2 = s1 + src_wrap;
1021         d = dst;
1022         for(w = width;w >= 4; w-=4) {
1023             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1024             d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1025             d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1026             d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1027             s1 += 8;
1028             s2 += 8;
1029             d += 4;
1030         }
1031         for(;w > 0; w--) {
1032             d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1033             s1 += 2;
1034             s2 += 2;
1035             d++;
1036         }
1037         src += 2 * src_wrap;
1038         dst += dst_wrap;
1039     }
1040 }
1041
1042 /* 4x4 -> 1x1 */
1043 void ff_shrink44(uint8_t *dst, int dst_wrap,
1044                      const uint8_t *src, int src_wrap,
1045                      int width, int height)
1046 {
1047     int w;
1048     const uint8_t *s1, *s2, *s3, *s4;
1049     uint8_t *d;
1050
1051     for(;height > 0; height--) {
1052         s1 = src;
1053         s2 = s1 + src_wrap;
1054         s3 = s2 + src_wrap;
1055         s4 = s3 + src_wrap;
1056         d = dst;
1057         for(w = width;w > 0; w--) {
1058             d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1059                     s2[0] + s2[1] + s2[2] + s2[3] +
1060                     s3[0] + s3[1] + s3[2] + s3[3] +
1061                     s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1062             s1 += 4;
1063             s2 += 4;
1064             s3 += 4;
1065             s4 += 4;
1066             d++;
1067         }
1068         src += 4 * src_wrap;
1069         dst += dst_wrap;
1070     }
1071 }
1072
1073 /* 8x8 -> 1x1 */
1074 void ff_shrink88(uint8_t *dst, int dst_wrap,
1075                      const uint8_t *src, int src_wrap,
1076                      int width, int height)
1077 {
1078     int w, i;
1079
1080     for(;height > 0; height--) {
1081         for(w = width;w > 0; w--) {
1082             int tmp=0;
1083             for(i=0; i<8; i++){
1084                 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
1085                 src += src_wrap;
1086             }
1087             *(dst++) = (tmp + 32)>>6;
1088             src += 8 - 8*src_wrap;
1089         }
1090         src += 8*src_wrap - 8*width;
1091         dst += dst_wrap - width;
1092     }
1093 }
1094
1095
1096 int avpicture_alloc(AVPicture *picture,
1097                     enum PixelFormat pix_fmt, int width, int height)
1098 {
1099     int size;
1100     void *ptr;
1101
1102     size = avpicture_fill(picture, NULL, pix_fmt, width, height);
1103     if(size<0)
1104         goto fail;
1105     ptr = av_malloc(size);
1106     if (!ptr)
1107         goto fail;
1108     avpicture_fill(picture, ptr, pix_fmt, width, height);
1109     if(picture->data[1] && !picture->data[2])
1110         ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
1111
1112     return 0;
1113  fail:
1114     memset(picture, 0, sizeof(AVPicture));
1115     return -1;
1116 }
1117
1118 void avpicture_free(AVPicture *picture)
1119 {
1120     av_free(picture->data[0]);
1121 }
1122
1123 /* return true if yuv planar */
1124 static inline int is_yuv_planar(const PixFmtInfo *ps)
1125 {
1126     return (ps->color_type == FF_COLOR_YUV ||
1127             ps->color_type == FF_COLOR_YUV_JPEG) &&
1128         ps->pixel_type == FF_PIXEL_PLANAR;
1129 }
1130
1131 int av_picture_crop(AVPicture *dst, const AVPicture *src,
1132                     enum PixelFormat pix_fmt, int top_band, int left_band)
1133 {
1134     int y_shift;
1135     int x_shift;
1136
1137     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1138         return -1;
1139
1140     y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1141     x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1142
1143     dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1144     dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1145     dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1146
1147     dst->linesize[0] = src->linesize[0];
1148     dst->linesize[1] = src->linesize[1];
1149     dst->linesize[2] = src->linesize[2];
1150     return 0;
1151 }
1152
1153 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1154                    enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1155             int *color)
1156 {
1157     uint8_t *optr;
1158     int y_shift;
1159     int x_shift;
1160     int yheight;
1161     int i, y;
1162
1163     if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1164         !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1165
1166     for (i = 0; i < 3; i++) {
1167         x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1168         y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1169
1170         if (padtop || padleft) {
1171             memset(dst->data[i], color[i],
1172                 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1173         }
1174
1175         if (padleft || padright) {
1176             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1177                 (dst->linesize[i] - (padright >> x_shift));
1178             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1179             for (y = 0; y < yheight; y++) {
1180                 memset(optr, color[i], (padleft + padright) >> x_shift);
1181                 optr += dst->linesize[i];
1182             }
1183         }
1184
1185         if (src) { /* first line */
1186             uint8_t *iptr = src->data[i];
1187             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1188                     (padleft >> x_shift);
1189             memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1190             iptr += src->linesize[i];
1191             optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1192                 (dst->linesize[i] - (padright >> x_shift));
1193             yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1194             for (y = 0; y < yheight; y++) {
1195                 memset(optr, color[i], (padleft + padright) >> x_shift);
1196                 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1197                        (width - padleft - padright) >> x_shift);
1198                 iptr += src->linesize[i];
1199                 optr += dst->linesize[i];
1200             }
1201         }
1202
1203         if (padbottom || padright) {
1204             optr = dst->data[i] + dst->linesize[i] *
1205                 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1206             memset(optr, color[i],dst->linesize[i] *
1207                 (padbottom >> y_shift) + (padright >> x_shift));
1208         }
1209     }
1210     return 0;
1211 }
1212
1213 /* NOTE: we scan all the pixels to have an exact information */
1214 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1215 {
1216     const unsigned char *p;
1217     int src_wrap, ret, x, y;
1218     unsigned int a;
1219     uint32_t *palette = (uint32_t *)src->data[1];
1220
1221     p = src->data[0];
1222     src_wrap = src->linesize[0] - width;
1223     ret = 0;
1224     for(y=0;y<height;y++) {
1225         for(x=0;x<width;x++) {
1226             a = palette[p[0]] >> 24;
1227             if (a == 0x00) {
1228                 ret |= FF_ALPHA_TRANSP;
1229             } else if (a != 0xff) {
1230                 ret |= FF_ALPHA_SEMI_TRANSP;
1231             }
1232             p++;
1233         }
1234         p += src_wrap;
1235     }
1236     return ret;
1237 }
1238
1239 int img_get_alpha_info(const AVPicture *src,
1240                        enum PixelFormat pix_fmt, int width, int height)
1241 {
1242     const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1243     int ret;
1244
1245     /* no alpha can be represented in format */
1246     if (!pf->is_alpha)
1247         return 0;
1248     switch(pix_fmt) {
1249     case PIX_FMT_PAL8:
1250         ret = get_alpha_info_pal8(src, width, height);
1251         break;
1252     default:
1253         /* we do not know, so everything is indicated */
1254         ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1255         break;
1256     }
1257     return ret;
1258 }
1259
1260 #if HAVE_MMX
1261 #define DEINT_INPLACE_LINE_LUM \
1262                     movd_m2r(lum_m4[0],mm0);\
1263                     movd_m2r(lum_m3[0],mm1);\
1264                     movd_m2r(lum_m2[0],mm2);\
1265                     movd_m2r(lum_m1[0],mm3);\
1266                     movd_m2r(lum[0],mm4);\
1267                     punpcklbw_r2r(mm7,mm0);\
1268                     movd_r2m(mm2,lum_m4[0]);\
1269                     punpcklbw_r2r(mm7,mm1);\
1270                     punpcklbw_r2r(mm7,mm2);\
1271                     punpcklbw_r2r(mm7,mm3);\
1272                     punpcklbw_r2r(mm7,mm4);\
1273                     paddw_r2r(mm3,mm1);\
1274                     psllw_i2r(1,mm2);\
1275                     paddw_r2r(mm4,mm0);\
1276                     psllw_i2r(2,mm1);\
1277                     paddw_r2r(mm6,mm2);\
1278                     paddw_r2r(mm2,mm1);\
1279                     psubusw_r2r(mm0,mm1);\
1280                     psrlw_i2r(3,mm1);\
1281                     packuswb_r2r(mm7,mm1);\
1282                     movd_r2m(mm1,lum_m2[0]);
1283
1284 #define DEINT_LINE_LUM \
1285                     movd_m2r(lum_m4[0],mm0);\
1286                     movd_m2r(lum_m3[0],mm1);\
1287                     movd_m2r(lum_m2[0],mm2);\
1288                     movd_m2r(lum_m1[0],mm3);\
1289                     movd_m2r(lum[0],mm4);\
1290                     punpcklbw_r2r(mm7,mm0);\
1291                     punpcklbw_r2r(mm7,mm1);\
1292                     punpcklbw_r2r(mm7,mm2);\
1293                     punpcklbw_r2r(mm7,mm3);\
1294                     punpcklbw_r2r(mm7,mm4);\
1295                     paddw_r2r(mm3,mm1);\
1296                     psllw_i2r(1,mm2);\
1297                     paddw_r2r(mm4,mm0);\
1298                     psllw_i2r(2,mm1);\
1299                     paddw_r2r(mm6,mm2);\
1300                     paddw_r2r(mm2,mm1);\
1301                     psubusw_r2r(mm0,mm1);\
1302                     psrlw_i2r(3,mm1);\
1303                     packuswb_r2r(mm7,mm1);\
1304                     movd_r2m(mm1,dst[0]);
1305 #endif
1306
1307 /* filter parameters: [-1 4 2 4 -1] // 8 */
1308 static void deinterlace_line(uint8_t *dst,
1309                              const uint8_t *lum_m4, const uint8_t *lum_m3,
1310                              const uint8_t *lum_m2, const uint8_t *lum_m1,
1311                              const uint8_t *lum,
1312                              int size)
1313 {
1314 #if !HAVE_MMX
1315     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1316     int sum;
1317
1318     for(;size > 0;size--) {
1319         sum = -lum_m4[0];
1320         sum += lum_m3[0] << 2;
1321         sum += lum_m2[0] << 1;
1322         sum += lum_m1[0] << 2;
1323         sum += -lum[0];
1324         dst[0] = cm[(sum + 4) >> 3];
1325         lum_m4++;
1326         lum_m3++;
1327         lum_m2++;
1328         lum_m1++;
1329         lum++;
1330         dst++;
1331     }
1332 #else
1333
1334     {
1335         pxor_r2r(mm7,mm7);
1336         movq_m2r(ff_pw_4,mm6);
1337     }
1338     for (;size > 3; size-=4) {
1339         DEINT_LINE_LUM
1340         lum_m4+=4;
1341         lum_m3+=4;
1342         lum_m2+=4;
1343         lum_m1+=4;
1344         lum+=4;
1345         dst+=4;
1346     }
1347 #endif
1348 }
1349 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1350                              int size)
1351 {
1352 #if !HAVE_MMX
1353     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1354     int sum;
1355
1356     for(;size > 0;size--) {
1357         sum = -lum_m4[0];
1358         sum += lum_m3[0] << 2;
1359         sum += lum_m2[0] << 1;
1360         lum_m4[0]=lum_m2[0];
1361         sum += lum_m1[0] << 2;
1362         sum += -lum[0];
1363         lum_m2[0] = cm[(sum + 4) >> 3];
1364         lum_m4++;
1365         lum_m3++;
1366         lum_m2++;
1367         lum_m1++;
1368         lum++;
1369     }
1370 #else
1371
1372     {
1373         pxor_r2r(mm7,mm7);
1374         movq_m2r(ff_pw_4,mm6);
1375     }
1376     for (;size > 3; size-=4) {
1377         DEINT_INPLACE_LINE_LUM
1378         lum_m4+=4;
1379         lum_m3+=4;
1380         lum_m2+=4;
1381         lum_m1+=4;
1382         lum+=4;
1383     }
1384 #endif
1385 }
1386
1387 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1388    top field is copied as is, but the bottom field is deinterlaced
1389    against the top field. */
1390 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1391                                     const uint8_t *src1, int src_wrap,
1392                                     int width, int height)
1393 {
1394     const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1395     int y;
1396
1397     src_m2 = src1;
1398     src_m1 = src1;
1399     src_0=&src_m1[src_wrap];
1400     src_p1=&src_0[src_wrap];
1401     src_p2=&src_p1[src_wrap];
1402     for(y=0;y<(height-2);y+=2) {
1403         memcpy(dst,src_m1,width);
1404         dst += dst_wrap;
1405         deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1406         src_m2 = src_0;
1407         src_m1 = src_p1;
1408         src_0 = src_p2;
1409         src_p1 += 2*src_wrap;
1410         src_p2 += 2*src_wrap;
1411         dst += dst_wrap;
1412     }
1413     memcpy(dst,src_m1,width);
1414     dst += dst_wrap;
1415     /* do last line */
1416     deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1417 }
1418
1419 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1420                                              int width, int height)
1421 {
1422     uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1423     int y;
1424     uint8_t *buf;
1425     buf = (uint8_t*)av_malloc(width);
1426
1427     src_m1 = src1;
1428     memcpy(buf,src_m1,width);
1429     src_0=&src_m1[src_wrap];
1430     src_p1=&src_0[src_wrap];
1431     src_p2=&src_p1[src_wrap];
1432     for(y=0;y<(height-2);y+=2) {
1433         deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1434         src_m1 = src_p1;
1435         src_0 = src_p2;
1436         src_p1 += 2*src_wrap;
1437         src_p2 += 2*src_wrap;
1438     }
1439     /* do last line */
1440     deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1441     av_free(buf);
1442 }
1443
1444 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1445                           enum PixelFormat pix_fmt, int width, int height)
1446 {
1447     int i;
1448
1449     if (pix_fmt != PIX_FMT_YUV420P &&
1450         pix_fmt != PIX_FMT_YUV422P &&
1451         pix_fmt != PIX_FMT_YUV444P &&
1452         pix_fmt != PIX_FMT_YUV411P &&
1453         pix_fmt != PIX_FMT_GRAY8)
1454         return -1;
1455     if ((width & 3) != 0 || (height & 3) != 0)
1456         return -1;
1457
1458     for(i=0;i<3;i++) {
1459         if (i == 1) {
1460             switch(pix_fmt) {
1461             case PIX_FMT_YUV420P:
1462                 width >>= 1;
1463                 height >>= 1;
1464                 break;
1465             case PIX_FMT_YUV422P:
1466                 width >>= 1;
1467                 break;
1468             case PIX_FMT_YUV411P:
1469                 width >>= 2;
1470                 break;
1471             default:
1472                 break;
1473             }
1474             if (pix_fmt == PIX_FMT_GRAY8) {
1475                 break;
1476             }
1477         }
1478         if (src == dst) {
1479             deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1480                                  width, height);
1481         } else {
1482             deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1483                                         src->data[i], src->linesize[i],
1484                                         width, height);
1485         }
1486     }
1487     emms_c();
1488     return 0;
1489 }
1490