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