Make avpicture_fill() directly call av_fill_image_linesizes() and
[libav.git] / libavcodec / imgconvert.c
CommitLineData
de6d9b64 1/*
f1ea5c2a 2 * Misc image conversion routines
406792e7 3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
de6d9b64 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
ff4ec49e
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ff4ec49e
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
ff4ec49e 17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
983e3246
MN
21
22/**
ba87f080 23 * @file
f1ea5c2a 24 * misc image conversion routines
983e3246
MN
25 */
26
c50c0bc8
FB
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 */
983e3246 32
de6d9b64 33#include "avcodec.h"
85c242d8 34#include "dsputil.h"
4443c0e9
CEH
35#include "internal.h"
36#include "imgconvert.h"
2b4abbd6 37#include "libavutil/colorspace.h"
8e861e1b 38#include "libavutil/pixdesc.h"
e7bd48a6 39#include "libavcore/imgutils.h"
de6d9b64 40
b250f9c6 41#if HAVE_MMX
a6493a8f
DB
42#include "x86/mmx.h"
43#include "x86/dsputil_mmx.h"
5981f4e6 44#endif
524c6b63 45
7e7e5940
FB
46#define xglue(x, y) x ## y
47#define glue(x, y) xglue(x, y)
48
bb0f999b
GP
49#define FF_COLOR_RGB 0 /**< RGB color space */
50#define FF_COLOR_GRAY 1 /**< gray color space */
51#define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
52#define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
b6147995 53
bb0f999b
GP
54#define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
55#define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
56#define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
7e7e5940 57
524c6b63 58typedef struct PixFmtInfo {
bb0f999b
GP
59 uint8_t nb_channels; /**< number of channels (including alpha) */
60 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
61 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
62 uint8_t is_alpha : 1; /**< true if alpha can be specified */
bb0f999b 63 uint8_t depth; /**< bit depth of the color components */
524c6b63
FB
64} PixFmtInfo;
65
66/* this table gives more information about formats */
62a05b5b 67static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
524c6b63
FB
68 /* YUV formats */
69 [PIX_FMT_YUV420P] = {
7e7e5940 70 .nb_channels = 3,
b6147995 71 .color_type = FF_COLOR_YUV,
7e7e5940 72 .pixel_type = FF_PIXEL_PLANAR,
b6147995 73 .depth = 8,
524c6b63
FB
74 },
75 [PIX_FMT_YUV422P] = {
7e7e5940 76 .nb_channels = 3,
b6147995 77 .color_type = FF_COLOR_YUV,
7e7e5940 78 .pixel_type = FF_PIXEL_PLANAR,
b6147995 79 .depth = 8,
524c6b63
FB
80 },
81 [PIX_FMT_YUV444P] = {
7e7e5940 82 .nb_channels = 3,
b6147995 83 .color_type = FF_COLOR_YUV,
7e7e5940 84 .pixel_type = FF_PIXEL_PLANAR,
b6147995 85 .depth = 8,
524c6b63 86 },
71e445fc 87 [PIX_FMT_YUYV422] = {
7e7e5940 88 .nb_channels = 1,
b6147995 89 .color_type = FF_COLOR_YUV,
7e7e5940 90 .pixel_type = FF_PIXEL_PACKED,
b6147995 91 .depth = 8,
524c6b63 92 },
ebb177dd 93 [PIX_FMT_UYVY422] = {
ebb177dd
TK
94 .nb_channels = 1,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
97 .depth = 8,
ebb177dd 98 },
524c6b63 99 [PIX_FMT_YUV410P] = {
7e7e5940 100 .nb_channels = 3,
b6147995 101 .color_type = FF_COLOR_YUV,
7e7e5940 102 .pixel_type = FF_PIXEL_PLANAR,
b6147995 103 .depth = 8,
524c6b63
FB
104 },
105 [PIX_FMT_YUV411P] = {
7e7e5940 106 .nb_channels = 3,
b6147995 107 .color_type = FF_COLOR_YUV,
7e7e5940 108 .pixel_type = FF_PIXEL_PLANAR,
b6147995 109 .depth = 8,
524c6b63 110 },
4196cfb7 111 [PIX_FMT_YUV440P] = {
4196cfb7
112 .nb_channels = 3,
113 .color_type = FF_COLOR_YUV,
114 .pixel_type = FF_PIXEL_PLANAR,
115 .depth = 8,
4196cfb7 116 },
35f6c154 117 [PIX_FMT_YUV420P16LE] = {
6c2a8608
MN
118 .nb_channels = 3,
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
121 .depth = 16,
6c2a8608 122 },
35f6c154 123 [PIX_FMT_YUV422P16LE] = {
6c2a8608
MN
124 .nb_channels = 3,
125 .color_type = FF_COLOR_YUV,
126 .pixel_type = FF_PIXEL_PLANAR,
127 .depth = 16,
6c2a8608 128 },
35f6c154 129 [PIX_FMT_YUV444P16LE] = {
6c2a8608
MN
130 .nb_channels = 3,
131 .color_type = FF_COLOR_YUV,
132 .pixel_type = FF_PIXEL_PLANAR,
133 .depth = 16,
6c2a8608 134 },
35f6c154 135 [PIX_FMT_YUV420P16BE] = {
6c2a8608
MN
136 .nb_channels = 3,
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
139 .depth = 16,
6c2a8608 140 },
35f6c154 141 [PIX_FMT_YUV422P16BE] = {
6c2a8608
MN
142 .nb_channels = 3,
143 .color_type = FF_COLOR_YUV,
144 .pixel_type = FF_PIXEL_PLANAR,
145 .depth = 16,
6c2a8608 146 },
35f6c154 147 [PIX_FMT_YUV444P16BE] = {
6c2a8608
MN
148 .nb_channels = 3,
149 .color_type = FF_COLOR_YUV,
150 .pixel_type = FF_PIXEL_PLANAR,
151 .depth = 16,
6c2a8608
MN
152 },
153
524c6b63 154
b70335a2
AJ
155 /* YUV formats with alpha plane */
156 [PIX_FMT_YUVA420P] = {
b70335a2
AJ
157 .nb_channels = 4,
158 .color_type = FF_COLOR_YUV,
159 .pixel_type = FF_PIXEL_PLANAR,
160 .depth = 8,
b70335a2
AJ
161 },
162
b6147995
FB
163 /* JPEG YUV */
164 [PIX_FMT_YUVJ420P] = {
7e7e5940 165 .nb_channels = 3,
b6147995 166 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 167 .pixel_type = FF_PIXEL_PLANAR,
b6147995 168 .depth = 8,
b6147995
FB
169 },
170 [PIX_FMT_YUVJ422P] = {
7e7e5940 171 .nb_channels = 3,
b6147995 172 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 173 .pixel_type = FF_PIXEL_PLANAR,
b6147995 174 .depth = 8,
b6147995
FB
175 },
176 [PIX_FMT_YUVJ444P] = {
7e7e5940 177 .nb_channels = 3,
b6147995 178 .color_type = FF_COLOR_YUV_JPEG,
7e7e5940 179 .pixel_type = FF_PIXEL_PLANAR,
b6147995 180 .depth = 8,
b6147995 181 },
4196cfb7 182 [PIX_FMT_YUVJ440P] = {
4196cfb7
183 .nb_channels = 3,
184 .color_type = FF_COLOR_YUV_JPEG,
185 .pixel_type = FF_PIXEL_PLANAR,
186 .depth = 8,
4196cfb7 187 },
b6147995 188
524c6b63
FB
189 /* RGB formats */
190 [PIX_FMT_RGB24] = {
7e7e5940 191 .nb_channels = 3,
b6147995 192 .color_type = FF_COLOR_RGB,
7e7e5940 193 .pixel_type = FF_PIXEL_PACKED,
b6147995 194 .depth = 8,
524c6b63
FB
195 },
196 [PIX_FMT_BGR24] = {
7e7e5940 197 .nb_channels = 3,
b6147995 198 .color_type = FF_COLOR_RGB,
7e7e5940 199 .pixel_type = FF_PIXEL_PACKED,
b6147995 200 .depth = 8,
524c6b63 201 },
6e08ca9c 202 [PIX_FMT_ARGB] = {
7e7e5940 203 .nb_channels = 4, .is_alpha = 1,
b6147995 204 .color_type = FF_COLOR_RGB,
7e7e5940 205 .pixel_type = FF_PIXEL_PACKED,
b6147995 206 .depth = 8,
524c6b63 207 },
88c21a6f 208 [PIX_FMT_RGB48BE] = {
88c21a6f
PR
209 .nb_channels = 3,
210 .color_type = FF_COLOR_RGB,
211 .pixel_type = FF_PIXEL_PACKED,
212 .depth = 16,
88c21a6f
PR
213 },
214 [PIX_FMT_RGB48LE] = {
88c21a6f
PR
215 .nb_channels = 3,
216 .color_type = FF_COLOR_RGB,
217 .pixel_type = FF_PIXEL_PACKED,
218 .depth = 16,
88c21a6f 219 },
f82674e5 220 [PIX_FMT_RGB565BE] = {
7e7e5940 221 .nb_channels = 3,
b6147995 222 .color_type = FF_COLOR_RGB,
7e7e5940 223 .pixel_type = FF_PIXEL_PACKED,
b6147995 224 .depth = 5,
524c6b63 225 },
f82674e5 226 [PIX_FMT_RGB565LE] = {
f82674e5
SS
227 .nb_channels = 3,
228 .color_type = FF_COLOR_RGB,
229 .pixel_type = FF_PIXEL_PACKED,
230 .depth = 5,
f82674e5
SS
231 },
232 [PIX_FMT_RGB555BE] = {
f82674e5
SS
233 .nb_channels = 3,
234 .color_type = FF_COLOR_RGB,
235 .pixel_type = FF_PIXEL_PACKED,
236 .depth = 5,
f82674e5
SS
237 },
238 [PIX_FMT_RGB555LE] = {
20d46c03 239 .nb_channels = 3,
b6147995 240 .color_type = FF_COLOR_RGB,
7e7e5940 241 .pixel_type = FF_PIXEL_PACKED,
b6147995 242 .depth = 5,
524c6b63 243 },
2b7cf167
JK
244 [PIX_FMT_RGB444BE] = {
245 .nb_channels = 3,
246 .color_type = FF_COLOR_RGB,
247 .pixel_type = FF_PIXEL_PACKED,
248 .depth = 4,
249 },
250 [PIX_FMT_RGB444LE] = {
251 .nb_channels = 3,
252 .color_type = FF_COLOR_RGB,
253 .pixel_type = FF_PIXEL_PACKED,
254 .depth = 4,
255 },
524c6b63
FB
256
257 /* gray / mono formats */
34380af0 258 [PIX_FMT_GRAY16BE] = {
34380af0
KS
259 .nb_channels = 1,
260 .color_type = FF_COLOR_GRAY,
261 .pixel_type = FF_PIXEL_PLANAR,
262 .depth = 16,
263 },
264 [PIX_FMT_GRAY16LE] = {
34380af0
KS
265 .nb_channels = 1,
266 .color_type = FF_COLOR_GRAY,
267 .pixel_type = FF_PIXEL_PLANAR,
268 .depth = 16,
269 },
524c6b63 270 [PIX_FMT_GRAY8] = {
7e7e5940 271 .nb_channels = 1,
b6147995 272 .color_type = FF_COLOR_GRAY,
7e7e5940 273 .pixel_type = FF_PIXEL_PLANAR,
b6147995 274 .depth = 8,
524c6b63
FB
275 },
276 [PIX_FMT_MONOWHITE] = {
7e7e5940 277 .nb_channels = 1,
b6147995 278 .color_type = FF_COLOR_GRAY,
7e7e5940 279 .pixel_type = FF_PIXEL_PLANAR,
b6147995 280 .depth = 1,
524c6b63
FB
281 },
282 [PIX_FMT_MONOBLACK] = {
7e7e5940 283 .nb_channels = 1,
b6147995 284 .color_type = FF_COLOR_GRAY,
7e7e5940 285 .pixel_type = FF_PIXEL_PLANAR,
b6147995 286 .depth = 1,
524c6b63 287 },
7e6d70d0
FB
288
289 /* paletted formats */
290 [PIX_FMT_PAL8] = {
7e7e5940 291 .nb_channels = 4, .is_alpha = 1,
b6147995 292 .color_type = FF_COLOR_RGB,
7e7e5940 293 .pixel_type = FF_PIXEL_PALETTE,
b6147995 294 .depth = 8,
7e6d70d0 295 },
71e445fc 296 [PIX_FMT_UYYVYY411] = {
f02be79d
RS
297 .nb_channels = 1,
298 .color_type = FF_COLOR_YUV,
299 .pixel_type = FF_PIXEL_PACKED,
300 .depth = 8,
f02be79d 301 },
6e08ca9c 302 [PIX_FMT_ABGR] = {
00b2fa86
LA
303 .nb_channels = 4, .is_alpha = 1,
304 .color_type = FF_COLOR_RGB,
305 .pixel_type = FF_PIXEL_PACKED,
306 .depth = 8,
00b2fa86 307 },
f82674e5 308 [PIX_FMT_BGR565BE] = {
f82674e5
SS
309 .nb_channels = 3,
310 .color_type = FF_COLOR_RGB,
311 .pixel_type = FF_PIXEL_PACKED,
312 .depth = 5,
f82674e5
SS
313 },
314 [PIX_FMT_BGR565LE] = {
f82674e5
SS
315 .nb_channels = 3,
316 .color_type = FF_COLOR_RGB,
317 .pixel_type = FF_PIXEL_PACKED,
318 .depth = 5,
f82674e5
SS
319 },
320 [PIX_FMT_BGR555BE] = {
00b2fa86
LA
321 .nb_channels = 3,
322 .color_type = FF_COLOR_RGB,
323 .pixel_type = FF_PIXEL_PACKED,
324 .depth = 5,
00b2fa86 325 },
f82674e5 326 [PIX_FMT_BGR555LE] = {
08d23410 327 .nb_channels = 3,
00b2fa86
LA
328 .color_type = FF_COLOR_RGB,
329 .pixel_type = FF_PIXEL_PACKED,
330 .depth = 5,
00b2fa86 331 },
2b7cf167
JK
332 [PIX_FMT_BGR444BE] = {
333 .nb_channels = 3,
334 .color_type = FF_COLOR_RGB,
335 .pixel_type = FF_PIXEL_PACKED,
336 .depth = 4,
337 },
338 [PIX_FMT_BGR444LE] = {
339 .nb_channels = 3,
340 .color_type = FF_COLOR_RGB,
341 .pixel_type = FF_PIXEL_PACKED,
342 .depth = 4,
343 },
00b2fa86 344 [PIX_FMT_RGB8] = {
00b2fa86
LA
345 .nb_channels = 1,
346 .color_type = FF_COLOR_RGB,
347 .pixel_type = FF_PIXEL_PACKED,
348 .depth = 8,
00b2fa86
LA
349 },
350 [PIX_FMT_RGB4] = {
00b2fa86
LA
351 .nb_channels = 1,
352 .color_type = FF_COLOR_RGB,
353 .pixel_type = FF_PIXEL_PACKED,
354 .depth = 4,
00b2fa86
LA
355 },
356 [PIX_FMT_RGB4_BYTE] = {
00b2fa86
LA
357 .nb_channels = 1,
358 .color_type = FF_COLOR_RGB,
359 .pixel_type = FF_PIXEL_PACKED,
360 .depth = 8,
00b2fa86
LA
361 },
362 [PIX_FMT_BGR8] = {
00b2fa86
LA
363 .nb_channels = 1,
364 .color_type = FF_COLOR_RGB,
365 .pixel_type = FF_PIXEL_PACKED,
366 .depth = 8,
00b2fa86
LA
367 },
368 [PIX_FMT_BGR4] = {
00b2fa86
LA
369 .nb_channels = 1,
370 .color_type = FF_COLOR_RGB,
371 .pixel_type = FF_PIXEL_PACKED,
372 .depth = 4,
00b2fa86
LA
373 },
374 [PIX_FMT_BGR4_BYTE] = {
00b2fa86
LA
375 .nb_channels = 1,
376 .color_type = FF_COLOR_RGB,
377 .pixel_type = FF_PIXEL_PACKED,
378 .depth = 8,
00b2fa86
LA
379 },
380 [PIX_FMT_NV12] = {
00b2fa86
LA
381 .nb_channels = 2,
382 .color_type = FF_COLOR_YUV,
383 .pixel_type = FF_PIXEL_PLANAR,
384 .depth = 8,
00b2fa86
LA
385 },
386 [PIX_FMT_NV21] = {
00b2fa86
LA
387 .nb_channels = 2,
388 .color_type = FF_COLOR_YUV,
389 .pixel_type = FF_PIXEL_PLANAR,
390 .depth = 8,
00b2fa86
LA
391 },
392
6e08ca9c 393 [PIX_FMT_BGRA] = {
00b2fa86
LA
394 .nb_channels = 4, .is_alpha = 1,
395 .color_type = FF_COLOR_RGB,
396 .pixel_type = FF_PIXEL_PACKED,
397 .depth = 8,
00b2fa86 398 },
6e08ca9c 399 [PIX_FMT_RGBA] = {
00b2fa86
LA
400 .nb_channels = 4, .is_alpha = 1,
401 .color_type = FF_COLOR_RGB,
402 .pixel_type = FF_PIXEL_PACKED,
403 .depth = 8,
00b2fa86 404 },
524c6b63
FB
405};
406
a61ec8e7 407void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
524c6b63 408{
38264abb
SS
409 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
410 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
524c6b63
FB
411}
412
a61ec8e7 413const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
524c6b63
FB
414{
415 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
42a45a03 416 return NULL;
524c6b63 417 else
797229a6 418 return av_pix_fmt_descriptors[pix_fmt].name;
524c6b63
FB
419}
420
80a07f6e 421#if LIBAVCODEC_VERSION_MAJOR < 53
d89f692f
SS
422enum PixelFormat avcodec_get_pix_fmt(const char *name)
423{
80a07f6e 424 return av_get_pix_fmt(name);
d89f692f 425}
80a07f6e 426#endif
d89f692f 427
a61ec8e7 428void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
c3b95b1d 429{
c3b95b1d
SS
430 /* print header */
431 if (pix_fmt < 0)
432 snprintf (buf, buf_size,
433 "name " " nb_channels" " depth" " is_alpha"
434 );
8e6f8869
MN
435 else{
436 PixFmtInfo info= pix_fmt_info[pix_fmt];
437
438 char is_alpha_char= info.is_alpha ? 'y' : 'n';
439
c3b95b1d 440 snprintf (buf, buf_size,
65935bc7 441 "%-11s %5d %9d %6c",
106c72fa 442 av_pix_fmt_descriptors[pix_fmt].name,
c3b95b1d
SS
443 info.nb_channels,
444 info.depth,
445 is_alpha_char
446 );
8e6f8869 447 }
c3b95b1d
SS
448}
449
c269cf68
MN
450int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
451{
8e861e1b 452 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
c269cf68
MN
453}
454
6337178b
MN
455int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
456 int i;
457
458 for(i=0; i<256; i++){
459 int r,g,b;
460
461 switch(pix_fmt) {
462 case PIX_FMT_RGB8:
463 r= (i>>5 )*36;
464 g= ((i>>2)&7)*36;
465 b= (i&3 )*85;
466 break;
467 case PIX_FMT_BGR8:
468 b= (i>>6 )*85;
469 g= ((i>>3)&7)*36;
470 r= (i&7 )*36;
471 break;
472 case PIX_FMT_RGB4_BYTE:
473 r= (i>>3 )*255;
474 g= ((i>>1)&3)*85;
475 b= (i&1 )*255;
476 break;
477 case PIX_FMT_BGR4_BYTE:
478 b= (i>>3 )*255;
479 g= ((i>>1)&3)*85;
480 r= (i&1 )*255;
481 break;
482 case PIX_FMT_GRAY8:
483 r=b=g= i;
484 break;
c7efffcb
PK
485 default:
486 return -1;
6337178b
MN
487 }
488 pal[i] = b + (g<<8) + (r<<16);
489 }
490
491 return 0;
492}
493
9dd9d67b
HM
494int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
495{
e7bd48a6 496 return av_fill_image_linesizes(picture->linesize, pix_fmt, width);
2a877875
FB
497}
498
9dd9d67b
HM
499int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
500 int height)
501{
e7bd48a6 502 return av_fill_image_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
9dd9d67b
HM
503}
504
cc9853d3 505int avpicture_fill(AVPicture *picture, uint8_t *ptr,
a61ec8e7 506 enum PixelFormat pix_fmt, int width, int height)
cc9853d3
VS
507{
508
509 if(avcodec_check_dimensions(NULL, width, height))
510 return -1;
511
fc2db52e 512 if (av_fill_image_linesizes(picture->linesize, pix_fmt, width))
cc9853d3
VS
513 return -1;
514
fc2db52e 515 return av_fill_image_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
cc9853d3
VS
516}
517
a61ec8e7 518int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
63167088
RS
519 unsigned char *dest, int dest_size)
520{
62a05b5b 521 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
14b903f3 522 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
5f59207d 523 int i, j, w, ow, h, oh, data_planes;
115329f1 524 const unsigned char* s;
63167088
RS
525 int size = avpicture_get_size(pix_fmt, width, height);
526
0ecca7a4 527 if (size > dest_size || size < 0)
63167088
RS
528 return -1;
529
affd55a1 530 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
71e445fc 531 if (pix_fmt == PIX_FMT_YUYV422 ||
115329f1 532 pix_fmt == PIX_FMT_UYVY422 ||
ffd38d8b
AB
533 pix_fmt == PIX_FMT_BGR565BE ||
534 pix_fmt == PIX_FMT_BGR565LE ||
535 pix_fmt == PIX_FMT_BGR555BE ||
536 pix_fmt == PIX_FMT_BGR555LE ||
2b7cf167
JK
537 pix_fmt == PIX_FMT_BGR444BE ||
538 pix_fmt == PIX_FMT_BGR444LE ||
ffd38d8b
AB
539 pix_fmt == PIX_FMT_RGB565BE ||
540 pix_fmt == PIX_FMT_RGB565LE ||
541 pix_fmt == PIX_FMT_RGB555BE ||
2b7cf167
JK
542 pix_fmt == PIX_FMT_RGB555LE ||
543 pix_fmt == PIX_FMT_RGB444BE ||
544 pix_fmt == PIX_FMT_RGB444LE)
ebb177dd 545 w = width * 2;
71e445fc 546 else if (pix_fmt == PIX_FMT_UYYVYY411)
b16569d2 547 w = width + width/2;
bb270c08 548 else if (pix_fmt == PIX_FMT_PAL8)
b16569d2 549 w = width;
bb270c08 550 else
b16569d2 551 w = width * (pf->depth * pf->nb_channels / 8);
bb270c08
DB
552
553 data_planes = 1;
554 h = height;
63167088
RS
555 } else {
556 data_planes = pf->nb_channels;
bb270c08
DB
557 w = (width*pf->depth + 7)/8;
558 h = height;
63167088 559 }
115329f1 560
5f59207d
CS
561 ow = w;
562 oh = h;
563
63167088 564 for (i=0; i<data_planes; i++) {
b16569d2 565 if (i == 1) {
09ab8859
VS
566 w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
567 h = -((-height) >> desc->log2_chroma_h);
f2526204
RP
568 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
569 w <<= 1;
b16569d2
RP
570 } else if (i == 3) {
571 w = ow;
572 h = oh;
573 }
574 s = src->data[i];
575 for(j=0; j<h; j++) {
576 memcpy(dest, s, w);
577 dest += w;
578 s += src->linesize[i];
579 }
63167088 580 }
115329f1 581
affd55a1 582 if (pf->pixel_type == FF_PIXEL_PALETTE)
bb270c08 583 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
115329f1 584
63167088
RS
585 return size;
586}
587
a61ec8e7 588int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
2a877875
FB
589{
590 AVPicture dummy_pict;
899a8fa1
RD
591 if(avcodec_check_dimensions(NULL, width, height))
592 return -1;
593 switch (pix_fmt) {
594 case PIX_FMT_RGB8:
595 case PIX_FMT_BGR8:
596 case PIX_FMT_RGB4_BYTE:
597 case PIX_FMT_BGR4_BYTE:
598 case PIX_FMT_GRAY8:
599 // do not include palette for these pseudo-paletted formats
600 return width * height;
601 }
2a877875
FB
602 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
603}
604
a61ec8e7 605int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
b6147995
FB
606 int has_alpha)
607{
608 const PixFmtInfo *pf, *ps;
14b903f3
SS
609 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
610 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
b6147995
FB
611 int loss;
612
613 ps = &pix_fmt_info[src_pix_fmt];
b6147995
FB
614
615 /* compute loss */
616 loss = 0;
617 pf = &pix_fmt_info[dst_pix_fmt];
0a9ad8d1 618 if (pf->depth < ps->depth ||
4ef82b17
JK
619 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
620 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
621 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
622 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
b6147995 623 loss |= FF_LOSS_DEPTH;
14b903f3
SS
624 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
625 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
b6147995
FB
626 loss |= FF_LOSS_RESOLUTION;
627 switch(pf->color_type) {
628 case FF_COLOR_RGB:
629 if (ps->color_type != FF_COLOR_RGB &&
630 ps->color_type != FF_COLOR_GRAY)
631 loss |= FF_LOSS_COLORSPACE;
632 break;
633 case FF_COLOR_GRAY:
634 if (ps->color_type != FF_COLOR_GRAY)
635 loss |= FF_LOSS_COLORSPACE;
636 break;
637 case FF_COLOR_YUV:
638 if (ps->color_type != FF_COLOR_YUV)
639 loss |= FF_LOSS_COLORSPACE;
640 break;
641 case FF_COLOR_YUV_JPEG:
642 if (ps->color_type != FF_COLOR_YUV_JPEG &&
115329f1 643 ps->color_type != FF_COLOR_YUV &&
0a9ad8d1 644 ps->color_type != FF_COLOR_GRAY)
b6147995
FB
645 loss |= FF_LOSS_COLORSPACE;
646 break;
647 default:
648 /* fail safe test */
649 if (ps->color_type != pf->color_type)
650 loss |= FF_LOSS_COLORSPACE;
651 break;
652 }
653 if (pf->color_type == FF_COLOR_GRAY &&
654 ps->color_type != FF_COLOR_GRAY)
655 loss |= FF_LOSS_CHROMA;
656 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
657 loss |= FF_LOSS_ALPHA;
115329f1 658 if (pf->pixel_type == FF_PIXEL_PALETTE &&
7e7e5940 659 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
b6147995
FB
660 loss |= FF_LOSS_COLORQUANT;
661 return loss;
662}
663
a61ec8e7 664static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
b6147995
FB
665{
666 int bits;
667 const PixFmtInfo *pf;
14b903f3 668 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
b6147995
FB
669
670 pf = &pix_fmt_info[pix_fmt];
7e7e5940
FB
671 switch(pf->pixel_type) {
672 case FF_PIXEL_PACKED:
b6147995 673 switch(pix_fmt) {
71e445fc 674 case PIX_FMT_YUYV422:
ebb177dd 675 case PIX_FMT_UYVY422:
ffd38d8b
AB
676 case PIX_FMT_RGB565BE:
677 case PIX_FMT_RGB565LE:
678 case PIX_FMT_RGB555BE:
679 case PIX_FMT_RGB555LE:
2b7cf167
JK
680 case PIX_FMT_RGB444BE:
681 case PIX_FMT_RGB444LE:
ffd38d8b
AB
682 case PIX_FMT_BGR565BE:
683 case PIX_FMT_BGR565LE:
684 case PIX_FMT_BGR555BE:
685 case PIX_FMT_BGR555LE:
2b7cf167
JK
686 case PIX_FMT_BGR444BE:
687 case PIX_FMT_BGR444LE:
b6147995 688 bits = 16;
b6147995 689 break;
71e445fc 690 case PIX_FMT_UYYVYY411:
bb270c08
DB
691 bits = 12;
692 break;
b6147995 693 default:
7e7e5940 694 bits = pf->depth * pf->nb_channels;
b6147995
FB
695 break;
696 }
7e7e5940
FB
697 break;
698 case FF_PIXEL_PLANAR:
14b903f3 699 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
7e7e5940
FB
700 bits = pf->depth * pf->nb_channels;
701 } else {
115329f1 702 bits = pf->depth + ((2 * pf->depth) >>
14b903f3 703 (desc->log2_chroma_w + desc->log2_chroma_h));
7e7e5940
FB
704 }
705 break;
706 case FF_PIXEL_PALETTE:
707 bits = 8;
708 break;
709 default:
710 bits = -1;
711 break;
b6147995
FB
712 }
713 return bits;
714}
715
a61ec8e7
SS
716static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
717 enum PixelFormat src_pix_fmt,
b6147995
FB
718 int has_alpha,
719 int loss_mask)
720{
a61ec8e7
SS
721 int dist, i, loss, min_dist;
722 enum PixelFormat dst_pix_fmt;
b6147995
FB
723
724 /* find exact color match with smallest size */
f0bc8449 725 dst_pix_fmt = PIX_FMT_NONE;
b6147995
FB
726 min_dist = 0x7fffffff;
727 for(i = 0;i < PIX_FMT_NB; i++) {
31c3a4dc 728 if (pix_fmt_mask & (1ULL << i)) {
b6147995
FB
729 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
730 if (loss == 0) {
731 dist = avg_bits_per_pixel(i);
732 if (dist < min_dist) {
733 min_dist = dist;
734 dst_pix_fmt = i;
735 }
736 }
737 }
738 }
739 return dst_pix_fmt;
740}
741
a61ec8e7 742enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
b6147995
FB
743 int has_alpha, int *loss_ptr)
744{
a61ec8e7
SS
745 enum PixelFormat dst_pix_fmt;
746 int loss_mask, i;
b6147995
FB
747 static const int loss_mask_order[] = {
748 ~0, /* no loss first */
749 ~FF_LOSS_ALPHA,
750 ~FF_LOSS_RESOLUTION,
751 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
752 ~FF_LOSS_COLORQUANT,
753 ~FF_LOSS_DEPTH,
754 0,
755 };
756
757 /* try with successive loss */
758 i = 0;
759 for(;;) {
760 loss_mask = loss_mask_order[i++];
115329f1 761 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
b6147995
FB
762 has_alpha, loss_mask);
763 if (dst_pix_fmt >= 0)
764 goto found;
765 if (loss_mask == 0)
766 break;
767 }
f0bc8449 768 return PIX_FMT_NONE;
b6147995
FB
769 found:
770 if (loss_ptr)
771 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
772 return dst_pix_fmt;
773}
774
54009d42 775void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
e352ff08
FB
776 const uint8_t *src, int src_wrap,
777 int width, int height)
7e7e5940 778{
115329f1 779 if((!dst) || (!src))
5b2ad9f5 780 return;
7e7e5940
FB
781 for(;height > 0; height--) {
782 memcpy(dst, src, width);
783 dst += dst_wrap;
784 src += src_wrap;
785 }
786}
787
6dd36490 788int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
7e7e5940 789{
22ed53e5 790 int bits;
62a05b5b 791 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
14b903f3 792 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
115329f1 793
7e7e5940
FB
794 pf = &pix_fmt_info[pix_fmt];
795 switch(pf->pixel_type) {
796 case FF_PIXEL_PACKED:
797 switch(pix_fmt) {
71e445fc 798 case PIX_FMT_YUYV422:
ebb177dd 799 case PIX_FMT_UYVY422:
ffd38d8b
AB
800 case PIX_FMT_RGB565BE:
801 case PIX_FMT_RGB565LE:
802 case PIX_FMT_RGB555BE:
803 case PIX_FMT_RGB555LE:
2b7cf167
JK
804 case PIX_FMT_RGB444BE:
805 case PIX_FMT_RGB444LE:
ffd38d8b
AB
806 case PIX_FMT_BGR565BE:
807 case PIX_FMT_BGR565LE:
808 case PIX_FMT_BGR555BE:
809 case PIX_FMT_BGR555LE:
2b7cf167
JK
810 case PIX_FMT_BGR444BE:
811 case PIX_FMT_BGR444LE:
7e7e5940
FB
812 bits = 16;
813 break;
71e445fc 814 case PIX_FMT_UYYVYY411:
bb270c08
DB
815 bits = 12;
816 break;
7e7e5940
FB
817 default:
818 bits = pf->depth * pf->nb_channels;
819 break;
820 }
22ed53e5
VS
821 return (width * bits + 7) >> 3;
822 break;
823 case FF_PIXEL_PLANAR:
2839c56e
SS
824 if ((pix_fmt != PIX_FMT_NV12 && pix_fmt != PIX_FMT_NV21) &&
825 (plane == 1 || plane == 2))
14b903f3 826 width= -((-width)>>desc->log2_chroma_w);
22ed53e5
VS
827
828 return (width * pf->depth + 7) >> 3;
829 break;
830 case FF_PIXEL_PALETTE:
831 if (plane == 0)
832 return width;
7e7e5940 833 break;
22ed53e5
VS
834 }
835
836 return -1;
837}
838
839void av_picture_copy(AVPicture *dst, const AVPicture *src,
a61ec8e7 840 enum PixelFormat pix_fmt, int width, int height)
22ed53e5
VS
841{
842 int i;
843 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
14b903f3 844 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
22ed53e5 845
22ed53e5
VS
846 switch(pf->pixel_type) {
847 case FF_PIXEL_PACKED:
7e7e5940
FB
848 case FF_PIXEL_PLANAR:
849 for(i = 0; i < pf->nb_channels; i++) {
bc48bb08 850 int h;
6dd36490 851 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
7e7e5940
FB
852 h = height;
853 if (i == 1 || i == 2) {
14b903f3 854 h= -((-height)>>desc->log2_chroma_h);
7e7e5940 855 }
54009d42 856 ff_img_copy_plane(dst->data[i], dst->linesize[i],
7e7e5940
FB
857 src->data[i], src->linesize[i],
858 bwidth, h);
859 }
860 break;
861 case FF_PIXEL_PALETTE:
54009d42 862 ff_img_copy_plane(dst->data[0], dst->linesize[0],
7e7e5940
FB
863 src->data[0], src->linesize[0],
864 width, height);
865 /* copy the palette */
51ae2142 866 memcpy(dst->data[1], src->data[1], 4*256);
7e7e5940
FB
867 break;
868 }
869}
2a877875 870
85c242d8 871/* 2x2 -> 1x1 */
54009d42 872void ff_shrink22(uint8_t *dst, int dst_wrap,
e352ff08 873 const uint8_t *src, int src_wrap,
85c242d8
FB
874 int width, int height)
875{
876 int w;
e352ff08
FB
877 const uint8_t *s1, *s2;
878 uint8_t *d;
85c242d8
FB
879
880 for(;height > 0; height--) {
881 s1 = src;
882 s2 = s1 + src_wrap;
883 d = dst;
884 for(w = width;w >= 4; w-=4) {
0a9ad8d1
FB
885 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
886 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
887 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
888 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
85c242d8
FB
889 s1 += 8;
890 s2 += 8;
891 d += 4;
892 }
893 for(;w > 0; w--) {
0a9ad8d1 894 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
85c242d8
FB
895 s1 += 2;
896 s2 += 2;
897 d++;
898 }
899 src += 2 * src_wrap;
900 dst += dst_wrap;
901 }
902}
903
e352ff08 904/* 4x4 -> 1x1 */
54009d42 905void ff_shrink44(uint8_t *dst, int dst_wrap,
e352ff08 906 const uint8_t *src, int src_wrap,
6742d95d
FR
907 int width, int height)
908{
909 int w;
e352ff08
FB
910 const uint8_t *s1, *s2, *s3, *s4;
911 uint8_t *d;
6742d95d
FR
912
913 for(;height > 0; height--) {
914 s1 = src;
e352ff08
FB
915 s2 = s1 + src_wrap;
916 s3 = s2 + src_wrap;
917 s4 = s3 + src_wrap;
6742d95d 918 d = dst;
e352ff08
FB
919 for(w = width;w > 0; w--) {
920 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
921 s2[0] + s2[1] + s2[2] + s2[3] +
922 s3[0] + s3[1] + s3[2] + s3[3] +
923 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
924 s1 += 4;
925 s2 += 4;
926 s3 += 4;
927 s4 += 4;
6742d95d
FR
928 d++;
929 }
e352ff08
FB
930 src += 4 * src_wrap;
931 dst += dst_wrap;
932 }
933}
934
54009d42
MN
935/* 8x8 -> 1x1 */
936void ff_shrink88(uint8_t *dst, int dst_wrap,
937 const uint8_t *src, int src_wrap,
938 int width, int height)
939{
940 int w, i;
941
942 for(;height > 0; height--) {
943 for(w = width;w > 0; w--) {
944 int tmp=0;
945 for(i=0; i<8; i++){
946 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
947 src += src_wrap;
948 }
949 *(dst++) = (tmp + 32)>>6;
950 src += 8 - 8*src_wrap;
951 }
952 src += 8*src_wrap - 8*width;
953 dst += dst_wrap - width;
954 }
955}
956
524c6b63 957
75917b88 958int avpicture_alloc(AVPicture *picture,
a61ec8e7 959 enum PixelFormat pix_fmt, int width, int height)
524c6b63 960{
2d5545c3 961 int size;
524c6b63
FB
962 void *ptr;
963
899a8fa1 964 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
0ecca7a4
MN
965 if(size<0)
966 goto fail;
524c6b63
FB
967 ptr = av_malloc(size);
968 if (!ptr)
969 goto fail;
970 avpicture_fill(picture, ptr, pix_fmt, width, height);
6337178b
MN
971 if(picture->data[1] && !picture->data[2])
972 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
973
524c6b63
FB
974 return 0;
975 fail:
976 memset(picture, 0, sizeof(AVPicture));
977 return -1;
978}
979
75917b88 980void avpicture_free(AVPicture *picture)
524c6b63 981{
8e1e6f31 982 av_free(picture->data[0]);
524c6b63
FB
983}
984
c50c0bc8 985/* return true if yuv planar */
62a05b5b 986static inline int is_yuv_planar(const PixFmtInfo *ps)
c50c0bc8
FB
987{
988 return (ps->color_type == FF_COLOR_YUV ||
115329f1 989 ps->color_type == FF_COLOR_YUV_JPEG) &&
7e7e5940 990 ps->pixel_type == FF_PIXEL_PLANAR;
c50c0bc8
FB
991}
992
636d6a4a 993int av_picture_crop(AVPicture *dst, const AVPicture *src,
4a30fff6 994 enum PixelFormat pix_fmt, int top_band, int left_band)
f2651e7a
BC
995{
996 int y_shift;
997 int x_shift;
998
999 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1000 return -1;
1001
14b903f3
SS
1002 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1003 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
f2651e7a
BC
1004
1005 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1006 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1007 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1008
1009 dst->linesize[0] = src->linesize[0];
1010 dst->linesize[1] = src->linesize[1];
1011 dst->linesize[2] = src->linesize[2];
1012 return 0;
1013}
1014
636d6a4a 1015int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
a61ec8e7 1016 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
6845801f 1017 int *color)
5341c209 1018{
79acfb0e 1019 uint8_t *optr;
5341c209
LA
1020 int y_shift;
1021 int x_shift;
1022 int yheight;
1023 int i, y;
1024
6845801f
LB
1025 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1026 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
5341c209
LA
1027
1028 for (i = 0; i < 3; i++) {
14b903f3
SS
1029 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1030 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
5341c209
LA
1031
1032 if (padtop || padleft) {
6845801f
LB
1033 memset(dst->data[i], color[i],
1034 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
5341c209
LA
1035 }
1036
79acfb0e
LB
1037 if (padleft || padright) {
1038 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1039 (dst->linesize[i] - (padright >> x_shift));
1040 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1041 for (y = 0; y < yheight; y++) {
1042 memset(optr, color[i], (padleft + padright) >> x_shift);
1043 optr += dst->linesize[i];
5341c209 1044 }
79acfb0e
LB
1045 }
1046
1047 if (src) { /* first line */
1048 uint8_t *iptr = src->data[i];
1049 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1050 (padleft >> x_shift);
1be97a21 1051 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
79acfb0e 1052 iptr += src->linesize[i];
6845801f
LB
1053 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1054 (dst->linesize[i] - (padright >> x_shift));
5341c209
LA
1055 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1056 for (y = 0; y < yheight; y++) {
1057 memset(optr, color[i], (padleft + padright) >> x_shift);
79acfb0e 1058 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1be97a21 1059 (width - padleft - padright) >> x_shift);
79acfb0e 1060 iptr += src->linesize[i];
5341c209
LA
1061 optr += dst->linesize[i];
1062 }
1063 }
1064
1065 if (padbottom || padright) {
6845801f
LB
1066 optr = dst->data[i] + dst->linesize[i] *
1067 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1068 memset(optr, color[i],dst->linesize[i] *
1069 (padbottom >> y_shift) + (padright >> x_shift));
5341c209
LA
1070 }
1071 }
1072 return 0;
1073}
1074
0469baf1 1075/* NOTE: we scan all the pixels to have an exact information */
da64ecc3 1076static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
0469baf1
FB
1077{
1078 const unsigned char *p;
1079 int src_wrap, ret, x, y;
1080 unsigned int a;
1081 uint32_t *palette = (uint32_t *)src->data[1];
115329f1 1082
0469baf1
FB
1083 p = src->data[0];
1084 src_wrap = src->linesize[0] - width;
1085 ret = 0;
1086 for(y=0;y<height;y++) {
1087 for(x=0;x<width;x++) {
1088 a = palette[p[0]] >> 24;
1089 if (a == 0x00) {
1090 ret |= FF_ALPHA_TRANSP;
1091 } else if (a != 0xff) {
1092 ret |= FF_ALPHA_SEMI_TRANSP;
1093 }
1094 p++;
1095 }
1096 p += src_wrap;
1097 }
1098 return ret;
1099}
1100
da64ecc3 1101int img_get_alpha_info(const AVPicture *src,
a61ec8e7 1102 enum PixelFormat pix_fmt, int width, int height)
0469baf1 1103{
62a05b5b 1104 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
0469baf1
FB
1105 int ret;
1106
0469baf1
FB
1107 /* no alpha can be represented in format */
1108 if (!pf->is_alpha)
1109 return 0;
1110 switch(pix_fmt) {
0469baf1
FB
1111 case PIX_FMT_PAL8:
1112 ret = get_alpha_info_pal8(src, width, height);
1113 break;
1114 default:
1115 /* we do not know, so everything is indicated */
1116 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1117 break;
1118 }
1119 return ret;
1120}
5981f4e6 1121
b250f9c6 1122#if HAVE_MMX
5981f4e6
F
1123#define DEINT_INPLACE_LINE_LUM \
1124 movd_m2r(lum_m4[0],mm0);\
1125 movd_m2r(lum_m3[0],mm1);\
1126 movd_m2r(lum_m2[0],mm2);\
1127 movd_m2r(lum_m1[0],mm3);\
1128 movd_m2r(lum[0],mm4);\
1129 punpcklbw_r2r(mm7,mm0);\
1130 movd_r2m(mm2,lum_m4[0]);\
1131 punpcklbw_r2r(mm7,mm1);\
1132 punpcklbw_r2r(mm7,mm2);\
1133 punpcklbw_r2r(mm7,mm3);\
1134 punpcklbw_r2r(mm7,mm4);\
1135 paddw_r2r(mm3,mm1);\
1136 psllw_i2r(1,mm2);\
1137 paddw_r2r(mm4,mm0);\
1138 psllw_i2r(2,mm1);\
1139 paddw_r2r(mm6,mm2);\
1140 paddw_r2r(mm2,mm1);\
1141 psubusw_r2r(mm0,mm1);\
1142 psrlw_i2r(3,mm1);\
1143 packuswb_r2r(mm7,mm1);\
1144 movd_r2m(mm1,lum_m2[0]);
1145
1146#define DEINT_LINE_LUM \
1147 movd_m2r(lum_m4[0],mm0);\
1148 movd_m2r(lum_m3[0],mm1);\
1149 movd_m2r(lum_m2[0],mm2);\
1150 movd_m2r(lum_m1[0],mm3);\
1151 movd_m2r(lum[0],mm4);\
1152 punpcklbw_r2r(mm7,mm0);\
1153 punpcklbw_r2r(mm7,mm1);\
1154 punpcklbw_r2r(mm7,mm2);\
1155 punpcklbw_r2r(mm7,mm3);\
1156 punpcklbw_r2r(mm7,mm4);\
1157 paddw_r2r(mm3,mm1);\
1158 psllw_i2r(1,mm2);\
1159 paddw_r2r(mm4,mm0);\
1160 psllw_i2r(2,mm1);\
1161 paddw_r2r(mm6,mm2);\
1162 paddw_r2r(mm2,mm1);\
1163 psubusw_r2r(mm0,mm1);\
1164 psrlw_i2r(3,mm1);\
1165 packuswb_r2r(mm7,mm1);\
1166 movd_r2m(mm1,dst[0]);
1167#endif
1168
85c242d8 1169/* filter parameters: [-1 4 2 4 -1] // 8 */
115329f1 1170static void deinterlace_line(uint8_t *dst,
bb270c08
DB
1171 const uint8_t *lum_m4, const uint8_t *lum_m3,
1172 const uint8_t *lum_m2, const uint8_t *lum_m1,
1173 const uint8_t *lum,
1174 int size)
85c242d8 1175{
b250f9c6 1176#if !HAVE_MMX
55fde95e 1177 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
85c242d8 1178 int sum;
85c242d8
FB
1179
1180 for(;size > 0;size--) {
5981f4e6
F
1181 sum = -lum_m4[0];
1182 sum += lum_m3[0] << 2;
1183 sum += lum_m2[0] << 1;
1184 sum += lum_m1[0] << 2;
1185 sum += -lum[0];
85c242d8 1186 dst[0] = cm[(sum + 4) >> 3];
5981f4e6
F
1187 lum_m4++;
1188 lum_m3++;
1189 lum_m2++;
1190 lum_m1++;
1191 lum++;
85c242d8 1192 dst++;
85c242d8 1193 }
5981f4e6
F
1194#else
1195
782c5984 1196 {
782c5984 1197 pxor_r2r(mm7,mm7);
d08ea32c 1198 movq_m2r(ff_pw_4,mm6);
782c5984 1199 }
5981f4e6
F
1200 for (;size > 3; size-=4) {
1201 DEINT_LINE_LUM
1202 lum_m4+=4;
1203 lum_m3+=4;
1204 lum_m2+=4;
1205 lum_m1+=4;
1206 lum+=4;
1207 dst+=4;
1208 }
1209#endif
1210}
0c1a9eda 1211static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
5981f4e6
F
1212 int size)
1213{
b250f9c6 1214#if !HAVE_MMX
55fde95e 1215 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
5981f4e6
F
1216 int sum;
1217
1218 for(;size > 0;size--) {
1219 sum = -lum_m4[0];
1220 sum += lum_m3[0] << 2;
1221 sum += lum_m2[0] << 1;
1222 lum_m4[0]=lum_m2[0];
1223 sum += lum_m1[0] << 2;
1224 sum += -lum[0];
1225 lum_m2[0] = cm[(sum + 4) >> 3];
1226 lum_m4++;
1227 lum_m3++;
1228 lum_m2++;
1229 lum_m1++;
1230 lum++;
1231 }
1232#else
1233
782c5984 1234 {
782c5984 1235 pxor_r2r(mm7,mm7);
d08ea32c 1236 movq_m2r(ff_pw_4,mm6);
782c5984 1237 }
5981f4e6
F
1238 for (;size > 3; size-=4) {
1239 DEINT_INPLACE_LINE_LUM
1240 lum_m4+=4;
1241 lum_m3+=4;
1242 lum_m2+=4;
1243 lum_m1+=4;
1244 lum+=4;
1245 }
1246#endif
85c242d8
FB
1247}
1248
1249/* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1250 top field is copied as is, but the bottom field is deinterlaced
1251 against the top field. */
0c1a9eda 1252static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
da64ecc3 1253 const uint8_t *src1, int src_wrap,
5981f4e6 1254 int width, int height)
85c242d8 1255{
da64ecc3 1256 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
5981f4e6
F
1257 int y;
1258
1259 src_m2 = src1;
1260 src_m1 = src1;
1261 src_0=&src_m1[src_wrap];
1262 src_p1=&src_0[src_wrap];
1263 src_p2=&src_p1[src_wrap];
1264 for(y=0;y<(height-2);y+=2) {
1265 memcpy(dst,src_m1,width);
85c242d8 1266 dst += dst_wrap;
5981f4e6
F
1267 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1268 src_m2 = src_0;
1269 src_m1 = src_p1;
1270 src_0 = src_p2;
1271 src_p1 += 2*src_wrap;
1272 src_p2 += 2*src_wrap;
85c242d8 1273 dst += dst_wrap;
85c242d8 1274 }
5981f4e6
F
1275 memcpy(dst,src_m1,width);
1276 dst += dst_wrap;
1277 /* do last line */
1278 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1279}
1280
0c1a9eda 1281static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
bb270c08 1282 int width, int height)
5981f4e6 1283{
0c1a9eda 1284 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
5981f4e6 1285 int y;
0c1a9eda
ZK
1286 uint8_t *buf;
1287 buf = (uint8_t*)av_malloc(width);
5981f4e6
F
1288
1289 src_m1 = src1;
1290 memcpy(buf,src_m1,width);
1291 src_0=&src_m1[src_wrap];
1292 src_p1=&src_0[src_wrap];
1293 src_p2=&src_p1[src_wrap];
1294 for(y=0;y<(height-2);y+=2) {
1295 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1296 src_m1 = src_p1;
1297 src_0 = src_p2;
1298 src_p1 += 2*src_wrap;
1299 src_p2 += 2*src_wrap;
1300 }
1301 /* do last line */
1302 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
6000abfa 1303 av_free(buf);
85c242d8
FB
1304}
1305
da64ecc3 1306int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
a61ec8e7 1307 enum PixelFormat pix_fmt, int width, int height)
de6d9b64 1308{
85c242d8
FB
1309 int i;
1310
1311 if (pix_fmt != PIX_FMT_YUV420P &&
1312 pix_fmt != PIX_FMT_YUV422P &&
47017dd8 1313 pix_fmt != PIX_FMT_YUV444P &&
2a7feb18
AG
1314 pix_fmt != PIX_FMT_YUV411P &&
1315 pix_fmt != PIX_FMT_GRAY8)
85c242d8 1316 return -1;
5981f4e6 1317 if ((width & 3) != 0 || (height & 3) != 0)
85c242d8 1318 return -1;
5981f4e6 1319
85c242d8
FB
1320 for(i=0;i<3;i++) {
1321 if (i == 1) {
1322 switch(pix_fmt) {
1323 case PIX_FMT_YUV420P:
1324 width >>= 1;
1325 height >>= 1;
1326 break;
1327 case PIX_FMT_YUV422P:
1328 width >>= 1;
1329 break;
47017dd8
RS
1330 case PIX_FMT_YUV411P:
1331 width >>= 2;
1332 break;
85c242d8
FB
1333 default:
1334 break;
1335 }
2a7feb18
AG
1336 if (pix_fmt == PIX_FMT_GRAY8) {
1337 break;
1338 }
85c242d8 1339 }
5981f4e6 1340 if (src == dst) {
da64ecc3 1341 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
85c242d8 1342 width, height);
5981f4e6
F
1343 } else {
1344 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1345 src->data[i], src->linesize[i],
1346 width, height);
1347 }
de6d9b64 1348 }
55ffe9df 1349 emms_c();
85c242d8 1350 return 0;
de6d9b64 1351}
cd4af68a 1352