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