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