2 * Misc image convertion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
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.
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.
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
22 * Misc image convertion routines.
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
33 #include "gstffmpegcodecmap.h"
38 #define xglue(x, y) x ## y
39 #define glue(x, y) xglue(x, y)
41 /* this table gives more information about formats */
42 /* FIXME, this table is also in ffmpegcodecmap */
43 static PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
45 /* [PIX_FMT_YUV420P] = */ {
46 /* .format = */ PIX_FMT_YUV420P,
47 /* .name = */ "yuv420p",
48 /* .nb_channels = */ 3,
49 /* .color_type = */ FF_COLOR_YUV,
50 /* .pixel_type = */ FF_PIXEL_PLANAR,
52 /* .x_chroma_shift = */ 1,
53 /* .y_chroma_shift = */ 1,
56 /* [PIX_FMT_YVU420P] = */ {
57 /* .format = */ PIX_FMT_YVU420P,
58 /* .name = */ "yvu420p",
59 /* .nb_channels = */ 3,
60 /* .color_type = */ FF_COLOR_YUV,
61 /* .pixel_type = */ FF_PIXEL_PLANAR,
63 /* .x_chroma_shift = */ 1,
64 /* .y_chroma_shift = */ 1,
67 /* [PIX_FMT_NV12] = */ {
68 /* .format = */ PIX_FMT_NV12,
70 /* .nb_channels = */ 2,
71 /* .color_type = */ FF_COLOR_YUV,
72 /* .pixel_type = */ FF_PIXEL_PACKED,
74 /* .x_chroma_shift = */ 1,
75 /* .y_chroma_shift = */ 1,
78 /* [PIX_FMT_NV21] = */ {
79 /* .format = */ PIX_FMT_NV21,
81 /* .nb_channels = */ 2,
82 /* .color_type = */ FF_COLOR_YUV,
83 /* .pixel_type = */ FF_PIXEL_PACKED,
85 /* .x_chroma_shift = */ 1,
86 /* .y_chroma_shift = */ 1,
89 /* [PIX_FMT_YUV422P] = */ {
90 /* .format = */ PIX_FMT_YUV422P,
91 /* .name = */ "yuv422p",
92 /* .nb_channels = */ 3,
93 /* .color_type = */ FF_COLOR_YUV,
94 /* .pixel_type = */ FF_PIXEL_PLANAR,
96 /* .x_chroma_shift = */ 1,
97 /* .y_chroma_shift = */ 0,
100 /* [PIX_FMT_YUV444P] = */ {
101 /* .format = */ PIX_FMT_YUV444P,
102 /* .name = */ "yuv444p",
103 /* .nb_channels = */ 3,
104 /* .color_type = */ FF_COLOR_YUV,
105 /* .pixel_type = */ FF_PIXEL_PLANAR,
107 /* .x_chroma_shift = */ 0,
108 /* .y_chroma_shift = */ 0,
111 /* [PIX_FMT_YUV422] = */ {
112 /* .format = */ PIX_FMT_YUV422,
113 /* .name = */ "yuv422",
114 /* .nb_channels = */ 1,
115 /* .color_type = */ FF_COLOR_YUV,
116 /* .pixel_type = */ FF_PIXEL_PACKED,
118 /* .x_chroma_shift = */ 1,
119 /* .y_chroma_shift = */ 0,
122 /* [PIX_FMT_UYVY422] = */ {
123 /* .format = */ PIX_FMT_UYVY422,
124 /* .name = */ "uyvy422",
125 /* .nb_channels = */ 1,
126 /* .color_type = */ FF_COLOR_YUV,
127 /* .pixel_type = */ FF_PIXEL_PACKED,
129 /* .x_chroma_shift = */ 1,
130 /* .y_chroma_shift = */ 0,
133 /* [PIX_FMT_V308] = */ {
134 /* .format = */ PIX_FMT_V308,
135 /* .name = */ "v308",
136 /* .nb_channels = */ 1,
137 /* .color_type = */ FF_COLOR_YUV,
138 /* .pixel_type = */ FF_PIXEL_PACKED,
140 /* .x_chroma_shift = */ 0,
141 /* .y_chroma_shift = */ 0,
144 /* [PIX_FMT_YUV410P] = */ {
145 /* .format = */ PIX_FMT_YUV410P,
146 /* .name = */ "yuv410p",
147 /* .nb_channels = */ 3,
148 /* .color_type = */ FF_COLOR_YUV,
149 /* .pixel_type = */ FF_PIXEL_PLANAR,
151 /* .x_chroma_shift = */ 2,
152 /* .y_chroma_shift = */ 2,
155 /* [PIX_FMT_YVU410P] = */ {
156 /* .format = */ PIX_FMT_YVU410P,
157 /* .name = */ "yvu410p",
158 /* .nb_channels = */ 3,
159 /* .color_type = */ FF_COLOR_YUV,
160 /* .pixel_type = */ FF_PIXEL_PLANAR,
162 /* .x_chroma_shift = */ 2,
163 /* .y_chroma_shift = */ 2,
166 /* [PIX_FMT_YUV411P] = */ {
167 /* .format = */ PIX_FMT_YUV411P,
168 /* .name = */ "yuv411p",
169 /* .nb_channels = */ 3,
170 /* .color_type = */ FF_COLOR_YUV,
171 /* .pixel_type = */ FF_PIXEL_PLANAR,
173 /* .x_chroma_shift = */ 2,
174 /* .y_chroma_shift = */ 0,
179 /* [PIX_FMT_YUVJ420P] = */ {
180 /* .format = */ PIX_FMT_YUVJ420P,
181 /* .name = */ "yuvj420p",
182 /* .nb_channels = */ 3,
183 /* .color_type = */ FF_COLOR_YUV_JPEG,
184 /* .pixel_type = */ FF_PIXEL_PLANAR,
186 /* .x_chroma_shift = */ 1,
187 /* .y_chroma_shift = */ 1,
190 /* [PIX_FMT_YUVJ422P] = */ {
191 /* .format = */ PIX_FMT_YUVJ422P,
192 /* .name = */ "yuvj422p",
193 /* .nb_channels = */ 3,
194 /* .color_type = */ FF_COLOR_YUV_JPEG,
195 /* .pixel_type = */ FF_PIXEL_PLANAR,
197 /* .x_chroma_shift = */ 1,
198 /* .y_chroma_shift = */ 0,
201 /* [PIX_FMT_YUVJ444P] = */ {
202 /* .format = */ PIX_FMT_YUVJ444P,
203 /* .name = */ "yuvj444p",
204 /* .nb_channels = */ 3,
205 /* .color_type = */ FF_COLOR_YUV_JPEG,
206 /* .pixel_type = */ FF_PIXEL_PLANAR,
208 /* .x_chroma_shift = */ 0,
209 /* .y_chroma_shift = */ 0,
214 /* [PIX_FMT_RGB24] = */ {
215 /* .format = */ PIX_FMT_RGB24,
216 /* .name = */ "rgb24",
217 /* .nb_channels = */ 3,
218 /* .color_type = */ FF_COLOR_RGB,
219 /* .pixel_type = */ FF_PIXEL_PACKED,
221 /* .x_chroma_shift = */ 0,
222 /* .y_chroma_shift = */ 0,
225 /* [PIX_FMT_BGR24] = */ {
226 /* .format = */ PIX_FMT_BGR24,
227 /* .name = */ "bgr24",
228 /* .nb_channels = */ 3,
229 /* .color_type = */ FF_COLOR_RGB,
230 /* .pixel_type = */ FF_PIXEL_PACKED,
232 /* .x_chroma_shift = */ 0,
233 /* .y_chroma_shift = */ 0,
236 /* [PIX_FMT_RGB32] = */ {
237 /* .format = */ PIX_FMT_RGB32,
238 /* .name = */ "rgb32",
239 /* .nb_channels = */ 4,
240 /* .color_type = */ FF_COLOR_RGB,
241 /* .pixel_type = */ FF_PIXEL_PACKED,
243 /* .x_chroma_shift = */ 0,
244 /* .y_chroma_shift = */ 0,
247 /* [PIX_FMT_BGR32] = */ {
248 /* .format = */ PIX_FMT_BGR32,
249 /* .name = */ "bgr32",
250 /* .nb_channels = */ 4,
251 /* .color_type = */ FF_COLOR_RGB,
252 /* .pixel_type = */ FF_PIXEL_PACKED,
254 /* .x_chroma_shift = */ 0,
255 /* .y_chroma_shift = */ 0,
258 /* [PIX_FMT_RGB32] = */ {
259 /* .format = */ PIX_FMT_xRGB32,
260 /* .name = */ "xrgb32",
261 /* .nb_channels = */ 4,
262 /* .color_type = */ FF_COLOR_RGB,
263 /* .pixel_type = */ FF_PIXEL_PACKED,
265 /* .x_chroma_shift = */ 0,
266 /* .y_chroma_shift = */ 0,
269 /* [PIX_FMT_BGR32] = */ {
270 /* .format = */ PIX_FMT_BGRx32,
271 /* .name = */ "bgrx32",
272 /* .nb_channels = */ 4,
273 /* .color_type = */ FF_COLOR_RGB,
274 /* .pixel_type = */ FF_PIXEL_PACKED,
276 /* .x_chroma_shift = */ 0,
277 /* .y_chroma_shift = */ 0,
280 /* [PIX_FMT_RGBA32] = */ {
281 /* .format = */ PIX_FMT_RGBA32,
282 /* .name = */ "rgba32",
283 /* .nb_channels = */ 4,
284 /* .color_type = */ FF_COLOR_RGB,
285 /* .pixel_type = */ FF_PIXEL_PACKED,
287 /* .x_chroma_shift = */ 0,
288 /* .y_chroma_shift = */ 0,
291 /* [PIX_FMT_BGRA32] = */ {
292 /* .format = */ PIX_FMT_BGRA32,
293 /* .name = */ "bgra32",
294 /* .nb_channels = */ 4,
295 /* .color_type = */ FF_COLOR_RGB,
296 /* .pixel_type = */ FF_PIXEL_PACKED,
298 /* .x_chroma_shift = */ 0,
299 /* .y_chroma_shift = */ 0,
302 /* [PIX_FMT_ARGB32] = */ {
303 /* .format = */ PIX_FMT_ARGB32,
304 /* .name = */ "argb32",
305 /* .nb_channels = */ 4,
306 /* .color_type = */ FF_COLOR_RGB,
307 /* .pixel_type = */ FF_PIXEL_PACKED,
309 /* .x_chroma_shift = */ 0,
310 /* .y_chroma_shift = */ 0,
313 /* [PIX_FMT_ABGR32] = */ {
314 /* .format = */ PIX_FMT_ABGR32,
315 /* .name = */ "abgr32",
316 /* .nb_channels = */ 4,
317 /* .color_type = */ FF_COLOR_RGB,
318 /* .pixel_type = */ FF_PIXEL_PACKED,
320 /* .x_chroma_shift = */ 0,
321 /* .y_chroma_shift = */ 0,
324 /* [PIX_FMT_RGB565] = */ {
325 /* .format = */ PIX_FMT_RGB565,
326 /* .name = */ "rgb565",
327 /* .nb_channels = */ 3,
328 /* .color_type = */ FF_COLOR_RGB,
329 /* .pixel_type = */ FF_PIXEL_PACKED,
331 /* .x_chroma_shift = */ 0,
332 /* .y_chroma_shift = */ 0,
335 /* [PIX_FMT_RGB555] = */ {
336 /* .format = */ PIX_FMT_RGB555,
337 /* .name = */ "rgb555",
338 /* .nb_channels = */ 4,
339 /* .color_type = */ FF_COLOR_RGB,
340 /* .pixel_type = */ FF_PIXEL_PACKED,
342 /* .x_chroma_shift = */ 0,
343 /* .y_chroma_shift = */ 0,
347 /* gray / mono formats */
348 /* [PIX_FMT_GRAY8] = */ {
349 /* .format = */ PIX_FMT_GRAY8,
350 /* .name = */ "gray",
351 /* .nb_channels = */ 1,
352 /* .color_type = */ FF_COLOR_GRAY,
353 /* .pixel_type = */ FF_PIXEL_PLANAR,
355 /* .x_chroma_shift = */ 0,
356 /* .y_chroma_shift = */ 0,
359 /* [PIX_FMT_MONOWHITE] = */ {
360 /* .format = */ PIX_FMT_MONOWHITE,
361 /* .name = */ "monow",
362 /* .nb_channels = */ 1,
363 /* .color_type = */ FF_COLOR_GRAY,
364 /* .pixel_type = */ FF_PIXEL_PLANAR,
366 /* .x_chroma_shift = */ 0,
367 /* .y_chroma_shift = */ 0,
370 /* [PIX_FMT_MONOBLACK] = */ {
371 /* .format = */ PIX_FMT_MONOBLACK,
372 /* .name = */ "monob",
373 /* .nb_channels = */ 1,
374 /* .color_type = */ FF_COLOR_GRAY,
375 /* .pixel_type = */ FF_PIXEL_PLANAR,
377 /* .x_chroma_shift = */ 0,
378 /* .y_chroma_shift = */ 0,
382 /* paletted formats */
383 /* [PIX_FMT_PAL8] = */ {
384 /* .format = */ PIX_FMT_PAL8,
385 /* .name = */ "pal8",
386 /* .nb_channels = */ 4,
387 /* .color_type = */ FF_COLOR_RGB,
388 /* .pixel_type = */ FF_PIXEL_PALETTE,
390 /* .x_chroma_shift = */ 0,
391 /* .y_chroma_shift = */ 0,
394 /* [PIX_FMT_XVMC_MPEG2_MC] = */ {
395 /* .format = */ PIX_FMT_XVMC_MPEG2_MC,
396 /* .name = */ "xvmcmc",
398 /* [PIX_FMT_XVMC_MPEG2_IDCT] = */ {
399 /* .format = */ PIX_FMT_XVMC_MPEG2_IDCT,
400 /* .name = */ "xvmcidct",
402 /* [PIX_FMT_UYVY411] = */ {
403 /* .format = */ PIX_FMT_UYVY411,
404 /* .name = */ "uyvy411",
405 /* .nb_channels = */ 1,
406 /* .color_type = */ FF_COLOR_YUV,
407 /* .pixel_type = */ FF_PIXEL_PACKED,
409 /* .x_chroma_shift = */ 2,
410 /* .y_chroma_shift = */ 0,
413 /* [PIX_FMT_AYUV4444] = */ {
414 /* .format = */ PIX_FMT_AYUV4444,
415 /* .name = */ "ayuv4444",
416 /* .nb_channels = */ 1,
417 /* .color_type = */ FF_COLOR_YUV,
418 /* .pixel_type = */ FF_PIXEL_PACKED,
420 /* .x_chroma_shift = */ 0,
421 /* .y_chroma_shift = */ 0,
427 /* returns NULL if not found */
428 /* undid static since this is also used in gstffmpegcodecmap.c */
430 get_pix_fmt_info (enum PixelFormat format)
434 for (i = 0; i < sizeof (pix_fmt_info) / sizeof (pix_fmt_info[0]); i++) {
435 if (pix_fmt_info[i].format == format) {
436 return pix_fmt_info + i;
440 /* since this doesn't get checked *anywhere*, we might as well warn
441 here if we return NULL so you have *some* idea what's going on */
443 ("Could not find info for pixel format %d out of %d known pixel formats. One segfault coming up",
449 avcodec_get_chroma_sub_sample (int pix_fmt, int *h_shift, int *v_shift)
451 *h_shift = get_pix_fmt_info (pix_fmt)->x_chroma_shift;
452 *v_shift = get_pix_fmt_info (pix_fmt)->y_chroma_shift;
456 avcodec_get_pix_fmt_name (int pix_fmt)
458 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
461 return get_pix_fmt_info (pix_fmt)->name;
465 avcodec_get_pix_fmt (const char *name)
469 for (i = 0; i < PIX_FMT_NB; i++)
470 if (!strcmp (pix_fmt_info[i].name, name))
472 return pix_fmt_info[i].format;
477 avpicture_layout (const AVPicture * src, int pix_fmt, int width, int height,
478 unsigned char *dest, int dest_size)
480 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
481 int i, j, w, h, data_planes;
482 const unsigned char *s;
483 int size = avpicture_get_size (pix_fmt, width, height);
485 if (size > dest_size)
488 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
489 if (pix_fmt == PIX_FMT_YUV422 ||
490 pix_fmt == PIX_FMT_UYVY422 ||
491 pix_fmt == PIX_FMT_RGB565 || pix_fmt == PIX_FMT_RGB555)
493 else if (pix_fmt == PIX_FMT_UYVY411)
494 w = width + width / 2;
495 else if (pix_fmt == PIX_FMT_PAL8)
498 w = width * (pf->depth * pf->nb_channels / 8);
503 data_planes = pf->nb_channels;
504 w = (width * pf->depth + 7) / 8;
508 for (i = 0; i < data_planes; i++) {
510 w = width >> pf->x_chroma_shift;
511 h = height >> pf->y_chroma_shift;
514 for (j = 0; j < h; j++) {
517 s += src->linesize[i];
521 if (pf->pixel_type == FF_PIXEL_PALETTE)
522 memcpy ((unsigned char *) (((size_t) dest + 3) & ~3), src->data[1],
530 avpicture_get_size (int pix_fmt, int width, int height)
532 AVPicture dummy_pict;
534 return gst_ffmpegcsp_avpicture_fill (&dummy_pict, NULL, pix_fmt, width,
539 * compute the loss when converting from a pixel format to another
542 avcodec_get_pix_fmt_loss (int dst_pix_fmt, int src_pix_fmt, int has_alpha)
544 const PixFmtInfo *pf, *ps;
547 ps = get_pix_fmt_info (src_pix_fmt);
548 pf = get_pix_fmt_info (dst_pix_fmt);
552 if (pf->depth < ps->depth ||
553 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
554 loss |= FF_LOSS_DEPTH;
555 if (pf->x_chroma_shift > ps->x_chroma_shift ||
556 pf->y_chroma_shift > ps->y_chroma_shift)
557 loss |= FF_LOSS_RESOLUTION;
558 switch (pf->color_type) {
560 if (ps->color_type != FF_COLOR_RGB && ps->color_type != FF_COLOR_GRAY)
561 loss |= FF_LOSS_COLORSPACE;
564 if (ps->color_type != FF_COLOR_GRAY)
565 loss |= FF_LOSS_COLORSPACE;
568 if (ps->color_type != FF_COLOR_YUV)
569 loss |= FF_LOSS_COLORSPACE;
571 case FF_COLOR_YUV_JPEG:
572 if (ps->color_type != FF_COLOR_YUV_JPEG &&
573 ps->color_type != FF_COLOR_YUV && ps->color_type != FF_COLOR_GRAY)
574 loss |= FF_LOSS_COLORSPACE;
578 if (ps->color_type != pf->color_type)
579 loss |= FF_LOSS_COLORSPACE;
582 if (pf->color_type == FF_COLOR_GRAY && ps->color_type != FF_COLOR_GRAY)
583 loss |= FF_LOSS_CHROMA;
584 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
585 loss |= FF_LOSS_ALPHA;
586 if (pf->pixel_type == FF_PIXEL_PALETTE &&
587 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
588 loss |= FF_LOSS_COLORQUANT;
593 avg_bits_per_pixel (int pix_fmt)
596 const PixFmtInfo *pf;
598 pf = get_pix_fmt_info (pix_fmt);
599 switch (pf->pixel_type) {
600 case FF_PIXEL_PACKED:
603 case PIX_FMT_UYVY422:
608 case PIX_FMT_UYVY411:
612 bits = pf->depth * pf->nb_channels;
616 case FF_PIXEL_PLANAR:
617 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
618 bits = pf->depth * pf->nb_channels;
620 bits = pf->depth + ((2 * pf->depth) >>
621 (pf->x_chroma_shift + pf->y_chroma_shift));
624 case FF_PIXEL_PALETTE:
635 avcodec_find_best_pix_fmt1 (int pix_fmt_mask,
636 int src_pix_fmt, int has_alpha, int loss_mask)
638 int dist, i, loss, min_dist, dst_pix_fmt;
640 /* find exact color match with smallest size */
642 min_dist = 0x7fffffff;
643 for (i = 0; i < PIX_FMT_NB; i++) {
644 if (pix_fmt_mask & (1 << i)) {
645 loss = avcodec_get_pix_fmt_loss (i, src_pix_fmt, has_alpha) & loss_mask;
647 dist = avg_bits_per_pixel (i);
648 if (dist < min_dist) {
659 * find best pixel format to convert to. Return -1 if none found
662 avcodec_find_best_pix_fmt (int pix_fmt_mask, int src_pix_fmt,
663 int has_alpha, int *loss_ptr)
665 int dst_pix_fmt, loss_mask, i;
666 static const int loss_mask_order[] = {
667 ~0, /* no loss first */
670 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
676 /* try with successive loss */
679 loss_mask = loss_mask_order[i++];
680 dst_pix_fmt = avcodec_find_best_pix_fmt1 (pix_fmt_mask, src_pix_fmt,
681 has_alpha, loss_mask);
682 if (dst_pix_fmt >= 0)
690 *loss_ptr = avcodec_get_pix_fmt_loss (dst_pix_fmt, src_pix_fmt, has_alpha);
695 img_copy_plane (uint8_t * dst, int dst_wrap,
696 const uint8_t * src, int src_wrap, int width, int height)
698 for (; height > 0; height--) {
699 memcpy (dst, src, width);
706 * Copy image 'src' to 'dst'.
709 img_copy (AVPicture * dst, const AVPicture * src,
710 int pix_fmt, int width, int height)
713 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
715 pf = get_pix_fmt_info (pix_fmt);
716 switch (pf->pixel_type) {
717 case FF_PIXEL_PACKED:
720 case PIX_FMT_UYVY422:
725 case PIX_FMT_UYVY411:
729 bits = pf->depth * pf->nb_channels;
732 bwidth = (width * bits + 7) >> 3;
733 img_copy_plane (dst->data[0], dst->linesize[0],
734 src->data[0], src->linesize[0], bwidth, height);
736 case FF_PIXEL_PLANAR:
737 for (i = 0; i < pf->nb_channels; i++) {
742 if (i == 1 || i == 2) {
743 w >>= pf->x_chroma_shift;
744 h >>= pf->y_chroma_shift;
746 bwidth = (w * pf->depth + 7) >> 3;
747 img_copy_plane (dst->data[i], dst->linesize[i],
748 src->data[i], src->linesize[i], bwidth, h);
751 case FF_PIXEL_PALETTE:
752 img_copy_plane (dst->data[0], dst->linesize[0],
753 src->data[0], src->linesize[0], width, height);
754 /* copy the palette */
755 img_copy_plane (dst->data[1], dst->linesize[1],
756 src->data[1], src->linesize[1], 4, 256);
761 /* XXX: totally non optimized */
764 yuv422_to_yuv420p (AVPicture * dst, const AVPicture * src,
765 int width, int height)
767 const uint8_t *p, *p1;
768 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
776 for (; height >= 1; height -= 2) {
781 for (w = width; w >= 2; w -= 2) {
798 p1 += src->linesize[0];
799 lum1 += dst->linesize[0];
803 for (w = width; w >= 2; w -= 2) {
812 p1 += src->linesize[0];
813 lum1 += dst->linesize[0];
815 cb1 += dst->linesize[1];
816 cr1 += dst->linesize[2];
821 uyvy422_to_gray (AVPicture * dst, const AVPicture * src, int width, int height)
823 const uint8_t *p, *p1;
829 for (; height > 0; height--) {
833 for (w = width; w >= 2; w -= 2) {
839 p1 += src->linesize[0];
840 lum1 += dst->linesize[0];
846 uyvy422_to_yuv420p (AVPicture * dst, const AVPicture * src,
847 int width, int height)
849 const uint8_t *p, *p1;
850 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
859 for (; height >= 1; height -= 2) {
864 for (w = width; w >= 2; w -= 2) {
881 p1 += src->linesize[0];
882 lum1 += dst->linesize[0];
886 for (w = width; w >= 2; w -= 2) {
895 p1 += src->linesize[0];
896 lum1 += dst->linesize[0];
898 cb1 += dst->linesize[1];
899 cr1 += dst->linesize[2];
905 uyvy422_to_yuv422p (AVPicture * dst, const AVPicture * src,
906 int width, int height)
908 const uint8_t *p, *p1;
909 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
916 for (; height > 0; height--) {
921 for (w = width; w >= 2; w -= 2) {
931 p1 += src->linesize[0];
932 lum1 += dst->linesize[0];
933 cb1 += dst->linesize[1];
934 cr1 += dst->linesize[2];
940 yuv422_to_yuv422p (AVPicture * dst, const AVPicture * src,
941 int width, int height)
943 const uint8_t *p, *p1;
944 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
951 for (; height > 0; height--) {
956 for (w = width; w >= 2; w -= 2) {
966 p1 += src->linesize[0];
967 lum1 += dst->linesize[0];
968 cb1 += dst->linesize[1];
969 cr1 += dst->linesize[2];
974 yuv422p_to_yuv422 (AVPicture * dst, const AVPicture * src,
975 int width, int height)
978 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
985 for (; height > 0; height--) {
990 for (w = width; w >= 2; w -= 2) {
1004 p1 += dst->linesize[0];
1005 lum1 += src->linesize[0];
1006 cb1 += src->linesize[1];
1007 cr1 += src->linesize[2];
1012 yuv422p_to_uyvy422 (AVPicture * dst, const AVPicture * src,
1013 int width, int height)
1016 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1020 lum1 = src->data[0];
1023 for (; height > 0; height--) {
1028 for (w = width; w >= 2; w -= 2) {
1038 p1 += dst->linesize[0];
1039 lum1 += src->linesize[0];
1040 cb1 += src->linesize[1];
1041 cr1 += src->linesize[2];
1046 uyvy411_to_yuv411p (AVPicture * dst, const AVPicture * src,
1047 int width, int height)
1049 const uint8_t *p, *p1;
1050 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1054 lum1 = dst->data[0];
1057 for (; height > 0; height--) {
1062 for (w = width; w >= 4; w -= 4) {
1074 p1 += src->linesize[0];
1075 lum1 += dst->linesize[0];
1076 cb1 += dst->linesize[1];
1077 cr1 += dst->linesize[2];
1083 yuv420p_to_yuv422 (AVPicture * dst, const AVPicture * src,
1084 int width, int height)
1087 uint8_t *line1, *line2, *linesrc = dst->data[0];
1088 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1089 uint8_t *cb1, *cb2 = src->data[1];
1090 uint8_t *cr1, *cr2 = src->data[2];
1092 for (h = height / 2; h--;) {
1094 line2 = linesrc + dst->linesize[0];
1097 lum2 = lumsrc + src->linesize[0];
1102 for (w = width / 2; w--;) {
1105 *line1++ = *line2++ = *cb1++;
1108 *line1++ = *line2++ = *cr1++;
1111 if (width % 2 != 0) {
1114 *line1++ = *line2++ = *cb1++;
1117 linesrc += dst->linesize[0] * 2;
1118 lumsrc += src->linesize[0] * 2;
1119 cb2 += src->linesize[1];
1120 cr2 += src->linesize[2];
1123 if (height % 2 != 0) {
1129 for (w = width / 2; w--;) {
1136 if (width % 2 != 0) {
1144 nv12_to_nv21 (AVPicture * dst, const AVPicture * src, int width, int height)
1146 const uint8_t *s_c_ptr;
1150 s_c_ptr = src->data[1];
1151 d_c_ptr = dst->data[1];
1153 for (; height >= 2; height -= 2) {
1154 for (w = width; w >= 2; w -= 2) {
1155 d_c_ptr[0] = s_c_ptr[1];
1156 d_c_ptr[1] = s_c_ptr[0];
1161 /* handle odd width */
1163 d_c_ptr[0] = s_c_ptr[1];
1164 d_c_ptr[1] = s_c_ptr[0];
1168 s_c_ptr += src->linesize[1] - width;
1169 d_c_ptr += src->linesize[1] - width;
1171 /* handle odd height */
1173 for (w = width; w >= 2; w -= 2) {
1174 d_c_ptr[0] = s_c_ptr[1];
1175 d_c_ptr[1] = s_c_ptr[0];
1180 /* handle odd width */
1182 d_c_ptr[0] = s_c_ptr[1];
1183 d_c_ptr[1] = s_c_ptr[0];
1190 #define nv21_to_nv12 nv12_to_nv21
1192 #define SCALEBITS 10
1193 #define ONE_HALF (1 << (SCALEBITS - 1))
1194 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1196 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1200 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1201 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1203 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1206 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1208 y = ((y1) - 16) * FIX(255.0/219.0);\
1209 r = cm[(y + r_add) >> SCALEBITS];\
1210 g = cm[(y + g_add) >> SCALEBITS];\
1211 b = cm[(y + b_add) >> SCALEBITS];\
1214 #define YUV_TO_RGB1(cb1, cr1)\
1218 r_add = FIX(1.40200) * cr + ONE_HALF;\
1219 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1220 b_add = FIX(1.77200) * cb + ONE_HALF;\
1223 #define YUV_TO_RGB2(r, g, b, y1)\
1225 y = (y1) << SCALEBITS;\
1226 r = cm[(y + r_add) >> SCALEBITS];\
1227 g = cm[(y + g_add) >> SCALEBITS];\
1228 b = cm[(y + b_add) >> SCALEBITS];\
1231 #define Y_CCIR_TO_JPEG(y)\
1232 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1234 #define Y_JPEG_TO_CCIR(y)\
1235 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1237 #define C_CCIR_TO_JPEG(y)\
1238 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1240 /* NOTE: the clamp is really necessary! */
1242 C_JPEG_TO_CCIR (int y)
1244 y = (((y - 128) * FIX (112.0 / 127.0) + (ONE_HALF +
1245 (128 << SCALEBITS))) >> SCALEBITS);
1252 #define RGB_TO_Y(r, g, b) \
1253 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1254 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1256 #define RGB_TO_U(r1, g1, b1, shift)\
1257 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1258 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1260 #define RGB_TO_V(r1, g1, b1, shift)\
1261 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1262 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1264 #define RGB_TO_Y_CCIR(r, g, b) \
1265 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1266 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1268 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1269 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1270 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1272 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1273 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1274 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1276 static uint8_t y_ccir_to_jpeg[256];
1277 static uint8_t y_jpeg_to_ccir[256];
1278 static uint8_t c_ccir_to_jpeg[256];
1279 static uint8_t c_jpeg_to_ccir[256];
1281 /* init various conversion tables */
1283 img_convert_init (void)
1286 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1288 for (i = 0; i < 256; i++) {
1289 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG (i);
1290 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR (i);
1291 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG (i);
1292 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR (i);
1296 /* apply to each pixel the given table */
1298 img_apply_table (uint8_t * dst, int dst_wrap,
1299 const uint8_t * src, int src_wrap,
1300 int width, int height, const uint8_t * table1)
1305 const uint8_t *table;
1308 for (; height > 0; height--) {
1332 /* XXX: use generic filter ? */
1333 /* XXX: in most cases, the sampling position is incorrect */
1337 shrink41 (uint8_t * dst, int dst_wrap,
1338 const uint8_t * src, int src_wrap, int width, int height)
1344 for (; height > 0; height--) {
1347 for (w = width; w > 0; w--) {
1348 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1359 shrink21 (uint8_t * dst, int dst_wrap,
1360 const uint8_t * src, int src_wrap, int width, int height)
1366 for (; height > 0; height--) {
1369 for (w = width; w > 0; w--) {
1370 d[0] = (s[0] + s[1]) >> 1;
1381 shrink12 (uint8_t * dst, int dst_wrap,
1382 const uint8_t * src, int src_wrap, int width, int height)
1386 const uint8_t *s1, *s2;
1388 for (; height > 0; height--) {
1392 for (w = width; w >= 4; w -= 4) {
1393 d[0] = (s1[0] + s2[0]) >> 1;
1394 d[1] = (s1[1] + s2[1]) >> 1;
1395 d[2] = (s1[2] + s2[2]) >> 1;
1396 d[3] = (s1[3] + s2[3]) >> 1;
1401 for (; w > 0; w--) {
1402 d[0] = (s1[0] + s2[0]) >> 1;
1407 src += 2 * src_wrap;
1414 shrink22 (uint8_t * dst, int dst_wrap,
1415 const uint8_t * src, int src_wrap, int width, int height)
1418 const uint8_t *s1, *s2;
1421 for (; height > 0; height--) {
1425 for (w = width; w >= 4; w -= 4) {
1426 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1427 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1428 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1429 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1434 for (; w > 0; w--) {
1435 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1440 src += 2 * src_wrap;
1447 shrink44 (uint8_t * dst, int dst_wrap,
1448 const uint8_t * src, int src_wrap, int width, int height)
1451 const uint8_t *s1, *s2, *s3, *s4;
1454 for (; height > 0; height--) {
1460 for (w = width; w > 0; w--) {
1461 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1462 s2[0] + s2[1] + s2[2] + s2[3] +
1463 s3[0] + s3[1] + s3[2] + s3[3] +
1464 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1471 src += 4 * src_wrap;
1477 grow21_line (uint8_t * dst, const uint8_t * src, int width)
1485 for (w = width; w >= 4; w -= 4) {
1486 d[1] = d[0] = s1[0];
1487 d[3] = d[2] = s1[1];
1491 for (; w >= 2; w -= 2) {
1492 d[1] = d[0] = s1[0];
1496 /* only needed if width is not a multiple of two */
1497 /* XXX: veryfy that */
1504 grow41_line (uint8_t * dst, const uint8_t * src, int width)
1512 for (w = width; w >= 4; w -= 4) {
1525 grow21 (uint8_t * dst, int dst_wrap,
1526 const uint8_t * src, int src_wrap, int width, int height)
1528 for (; height > 0; height--) {
1529 grow21_line (dst, src, width);
1537 grow22 (uint8_t * dst, int dst_wrap,
1538 const uint8_t * src, int src_wrap, int width, int height)
1540 for (; height > 0; height--) {
1541 grow21_line (dst, src, width);
1550 grow41 (uint8_t * dst, int dst_wrap,
1551 const uint8_t * src, int src_wrap, int width, int height)
1553 for (; height > 0; height--) {
1554 grow41_line (dst, src, width);
1562 grow44 (uint8_t * dst, int dst_wrap,
1563 const uint8_t * src, int src_wrap, int width, int height)
1565 for (; height > 0; height--) {
1566 grow41_line (dst, src, width);
1567 if ((height & 3) == 1)
1575 conv411 (uint8_t * dst, int dst_wrap,
1576 const uint8_t * src, int src_wrap, int width, int height)
1579 const uint8_t *s1, *s2;
1584 for (; height > 0; height--) {
1586 s2 = src + src_wrap;
1588 for (w = width; w > 0; w--) {
1589 c = (s1[0] + s2[0]) >> 1;
1596 src += src_wrap * 2;
1601 /* XXX: add jpeg quantize code */
1603 #define TRANSP_INDEX (6*6*6)
1605 /* this is maybe slow, but allows for extensions */
1606 static inline unsigned char
1607 gif_clut_index (uint8_t r, uint8_t g, uint8_t b)
1609 return ((((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6));
1613 build_rgb_palette (uint8_t * palette, int has_alpha)
1616 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1619 pal = (uint32_t *) palette;
1621 for (r = 0; r < 6; r++) {
1622 for (g = 0; g < 6; g++) {
1623 for (b = 0; b < 6; b++) {
1624 pal[i++] = (0xffU << 24) | (pal_value[r] << 16) |
1625 (pal_value[g] << 8) | pal_value[b];
1632 pal[i++] = 0xff000000;
1635 /* copy bit n to bits 0 ... n - 1 */
1636 static inline unsigned int
1637 bitcopy_n (unsigned int a, int n)
1641 mask = (1 << n) - 1;
1642 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1645 /* rgb555 handling */
1647 #define RGB_NAME rgb555
1649 #define RGB_IN(r, g, b, s)\
1651 unsigned int v = ((const uint16_t *)(s))[0];\
1652 r = bitcopy_n(v >> (10 - 3), 3);\
1653 g = bitcopy_n(v >> (5 - 3), 3);\
1654 b = bitcopy_n(v << 3, 3);\
1657 #define RGBA_IN(r, g, b, a, s)\
1659 unsigned int v = ((const uint16_t *)(s))[0];\
1660 r = bitcopy_n(v >> (10 - 3), 3);\
1661 g = bitcopy_n(v >> (5 - 3), 3);\
1662 b = bitcopy_n(v << 3, 3);\
1663 a = (-(v >> 15)) & 0xff;\
1666 #define RGBA_OUT(d, r, g, b, a)\
1668 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1669 ((a << 8) & 0x8000);\
1674 #include "imgconvert_template.h"
1676 /* rgb565 handling */
1678 #define RGB_NAME rgb565
1680 #define RGB_IN(r, g, b, s)\
1682 unsigned int v = ((const uint16_t *)(s))[0];\
1683 r = bitcopy_n(v >> (11 - 3), 3);\
1684 g = bitcopy_n(v >> (5 - 2), 2);\
1685 b = bitcopy_n(v << 3, 3);\
1688 #define RGB_OUT(d, r, g, b)\
1690 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1695 #include "imgconvert_template.h"
1697 /* bgr24 handling */
1699 #define RGB_NAME bgr24
1701 #define RGB_IN(r, g, b, s)\
1708 #define RGB_OUT(d, r, g, b)\
1717 #include "imgconvert_template.h"
1723 /* rgb24 handling */
1725 #define RGB_NAME rgb24
1728 #define RGB_IN(r, g, b, s)\
1735 #define RGB_OUT(d, r, g, b)\
1744 #include "imgconvert_template.h"
1746 /* rgb32 handling */
1748 #define RGB_NAME rgb32
1751 #define RGB_IN(r, g, b, s)\
1753 unsigned int v = ((const uint32_t *)(s))[0];\
1754 r = (v >> 16) & 0xff;\
1755 g = (v >> 8) & 0xff;\
1759 #define RGBA_IN(r, g, b, a, s)\
1761 unsigned int v = ((const uint32_t *)(s))[0];\
1763 r = (v >> 16) & 0xff;\
1764 g = (v >> 8) & 0xff;\
1768 #define RGBA_OUT(d, r, g, b, a)\
1770 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1775 #include "imgconvert_template.h"
1777 /* bgr32 handling */
1779 #define RGB_NAME bgr32
1781 #define RGB_IN(r, g, b, s)\
1783 unsigned int v = ((const uint32_t *)(s))[0];\
1784 r = (v >> 8) & 0xff;\
1785 g = (v >> 16) & 0xff;\
1786 b = (v >> 24) & 0xff;\
1789 #define RGBA_IN(r, g, b, a, s)\
1791 unsigned int v = ((const uint32_t *)(s))[0];\
1793 r = (v >> 8) & 0xff;\
1794 g = (v >> 16) & 0xff;\
1795 b = (v >> 24) & 0xff;\
1798 #define RGBA_OUT(d, r, g, b, a)\
1800 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24);\
1805 #include "imgconvert_template.h"
1807 /* xrgb32 handling */
1809 #define RGB_NAME xrgb32
1812 #define RGB_IN(r, g, b, s)\
1814 unsigned int v = ((const uint32_t *)(s))[0];\
1815 r = (v >> 24) & 0xff;\
1816 g = (v >> 16) & 0xff;\
1817 b = (v >> 8) & 0xff;\
1820 #define RGBA_IN(r, g, b, a, s)\
1822 unsigned int v = ((const uint32_t *)(s))[0];\
1824 r = (v >> 24) & 0xff;\
1825 g = (v >> 16) & 0xff;\
1826 b = (v >> 8) & 0xff;\
1829 #define RGBA_OUT(d, r, g, b, a)\
1831 ((uint32_t *)(d))[0] = (r << 24) | (g << 16) | (b << 8) | a;\
1836 #include "imgconvert_template.h"
1838 /* bgrx32 handling */
1840 #define RGB_NAME bgrx32
1842 #define RGB_IN(r, g, b, s)\
1844 unsigned int v = ((const uint32_t *)(s))[0];\
1846 g = (v >> 8) & 0xff;\
1847 b = (v >> 16) & 0xff;\
1850 #define RGBA_IN(r, g, b, a, s)\
1852 unsigned int v = ((const uint32_t *)(s))[0];\
1855 g = (v >> 8) & 0xff;\
1856 b = (v >> 16) & 0xff;\
1859 #define RGBA_OUT(d, r, g, b, a)\
1861 ((uint32_t *)(d))[0] = r | (g << 8) | (b << 16) | (a << 24);\
1866 #include "imgconvert_template.h"
1868 /* rgba32 handling */
1870 #define RGB_NAME rgba32
1873 #define RGB_IN(r, g, b, s)\
1875 unsigned int v = ((const uint32_t *)(s))[0];\
1876 r = (v >> 16) & 0xff;\
1877 g = (v >> 8) & 0xff;\
1881 #define RGBA_IN(r, g, b, a, s)\
1883 unsigned int v = ((const uint32_t *)(s))[0];\
1884 a = (v >> 24) & 0xff;\
1885 r = (v >> 16) & 0xff;\
1886 g = (v >> 8) & 0xff;\
1890 #define RGBA_OUT(d, r, g, b, a)\
1892 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1897 #include "imgconvert_template.h"
1899 /* bgra32 handling */
1901 #define RGB_NAME bgra32
1904 #define RGB_IN(r, g, b, s)\
1906 unsigned int v = ((const uint32_t *)(s))[0];\
1907 r = (v >> 8) & 0xff;\
1908 g = (v >> 16) & 0xff;\
1909 b = (v >> 24) & 0xff;\
1912 #define RGBA_IN(r, g, b, a, s)\
1914 unsigned int v = ((const uint32_t *)(s))[0];\
1916 r = (v >> 8) & 0xff;\
1917 g = (v >> 16) & 0xff;\
1918 b = (v >> 24) & 0xff;\
1921 #define RGBA_OUT(d, r, g, b, a)\
1923 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24 );\
1928 #include "imgconvert_template.h"
1930 /* argb32 handling */
1932 #define RGB_NAME argb32
1935 #define RGB_IN(r, g, b, s)\
1937 unsigned int v = ((const uint32_t *)(s))[0];\
1938 r = (v >> 24) & 0xff;\
1939 g = (v >> 16) & 0xff;\
1940 b = (v >> 8) & 0xff;\
1943 #define RGBA_IN(r, g, b, a, s)\
1945 unsigned int v = ((const uint32_t *)(s))[0];\
1946 r = (v >> 24) & 0xff;\
1947 g = (v >> 16) & 0xff;\
1948 b = (v >> 8) & 0xff;\
1952 #define RGBA_OUT(d, r, g, b, a)\
1954 ((uint32_t *)(d))[0] = (r << 24) | (g << 16) | (b << 8) | a;\
1959 #include "imgconvert_template.h"
1961 /* abgr32 handling */
1963 #define RGB_NAME abgr32
1966 #define RGB_IN(r, g, b, s)\
1968 unsigned int v = ((const uint32_t *)(s))[0];\
1970 g = (v >> 8) & 0xff;\
1971 b = (v >> 16) & 0xff;\
1974 #define RGBA_IN(r, g, b, a, s)\
1976 unsigned int v = ((const uint32_t *)(s))[0];\
1978 g = (v >> 8) & 0xff;\
1979 b = (v >> 16) & 0xff;\
1980 a = (v >> 24) & 0xff;\
1983 #define RGBA_OUT(d, r, g, b, a)\
1985 ((uint32_t *)(d))[0] = r | (g << 8) | (b << 16) | (a << 24 );\
1990 #include "imgconvert_template.h"
1993 mono_to_gray (AVPicture * dst, const AVPicture * src,
1994 int width, int height, int xor_mask)
1996 const unsigned char *p;
1998 int v, dst_wrap, src_wrap;
2002 src_wrap = src->linesize[0] - ((width + 7) >> 3);
2005 dst_wrap = dst->linesize[0] - width;
2006 for (y = 0; y < height; y++) {
2009 v = *p++ ^ xor_mask;
2011 q[1] = -((v >> 6) & 1);
2012 q[2] = -((v >> 5) & 1);
2013 q[3] = -((v >> 4) & 1);
2014 q[4] = -((v >> 3) & 1);
2015 q[5] = -((v >> 2) & 1);
2016 q[6] = -((v >> 1) & 1);
2017 q[7] = -((v >> 0) & 1);
2022 v = *p++ ^ xor_mask;
2024 q[0] = -((v >> 7) & 1);
2035 monowhite_to_gray (AVPicture * dst, const AVPicture * src,
2036 int width, int height)
2038 mono_to_gray (dst, src, width, height, 0xff);
2042 monoblack_to_gray (AVPicture * dst, const AVPicture * src,
2043 int width, int height)
2045 mono_to_gray (dst, src, width, height, 0x00);
2049 gray_to_mono (AVPicture * dst, const AVPicture * src,
2050 int width, int height, int xor_mask)
2055 int j, b, v, n1, src_wrap, dst_wrap, y;
2058 src_wrap = src->linesize[0] - width;
2061 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
2063 for (y = 0; y < height; y++) {
2067 for (j = 0; j < 8; j++) {
2070 v = (v << 1) | (b >> 7);
2072 d[0] = v ^ xor_mask;
2082 v = (v << 1) | (b >> 7);
2085 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
2094 gray_to_monowhite (AVPicture * dst, const AVPicture * src,
2095 int width, int height)
2097 gray_to_mono (dst, src, width, height, 0xff);
2101 gray_to_monoblack (AVPicture * dst, const AVPicture * src,
2102 int width, int height)
2104 gray_to_mono (dst, src, width, height, 0x00);
2107 typedef struct ConvertEntry
2109 enum PixelFormat src;
2110 enum PixelFormat dest;
2111 void (*convert) (AVPicture * dst,
2112 const AVPicture * src, int width, int height);
2115 /* Add each new convertion function in this table. In order to be able
2116 to convert from any format to any format, the following constraints
2119 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
2121 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
2123 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
2125 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
2128 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
2130 The other conversion functions are just optimisations for common cases.
2132 static ConvertEntry convert_table[] = {
2133 {PIX_FMT_YUV420P, PIX_FMT_YUV422, yuv420p_to_yuv422},
2134 {PIX_FMT_YUV420P, PIX_FMT_RGB555, yuv420p_to_rgb555},
2135 {PIX_FMT_YUV420P, PIX_FMT_RGB565, yuv420p_to_rgb565},
2136 {PIX_FMT_YUV420P, PIX_FMT_BGR24, yuv420p_to_bgr24},
2137 {PIX_FMT_YUV420P, PIX_FMT_RGB24, yuv420p_to_rgb24},
2138 {PIX_FMT_YUV420P, PIX_FMT_RGB32, yuv420p_to_rgb32},
2139 {PIX_FMT_YUV420P, PIX_FMT_BGR32, yuv420p_to_bgr32},
2140 {PIX_FMT_YUV420P, PIX_FMT_xRGB32, yuv420p_to_xrgb32},
2141 {PIX_FMT_YUV420P, PIX_FMT_BGRx32, yuv420p_to_bgrx32},
2142 {PIX_FMT_YUV420P, PIX_FMT_RGBA32, yuv420p_to_rgba32},
2143 {PIX_FMT_YUV420P, PIX_FMT_BGRA32, yuv420p_to_bgra32},
2144 {PIX_FMT_YUV420P, PIX_FMT_ARGB32, yuv420p_to_argb32},
2145 {PIX_FMT_YUV420P, PIX_FMT_ABGR32, yuv420p_to_abgr32},
2147 {PIX_FMT_NV12, PIX_FMT_RGB555, nv12_to_rgb555},
2148 {PIX_FMT_NV12, PIX_FMT_RGB565, nv12_to_rgb565},
2149 {PIX_FMT_NV12, PIX_FMT_BGR24, nv12_to_bgr24},
2150 {PIX_FMT_NV12, PIX_FMT_RGB24, nv12_to_rgb24},
2151 {PIX_FMT_NV12, PIX_FMT_RGB32, nv12_to_rgb32},
2152 {PIX_FMT_NV12, PIX_FMT_BGR32, nv12_to_bgr32},
2153 {PIX_FMT_NV12, PIX_FMT_xRGB32, nv12_to_xrgb32},
2154 {PIX_FMT_NV12, PIX_FMT_BGRx32, nv12_to_bgrx32},
2155 {PIX_FMT_NV12, PIX_FMT_RGBA32, nv12_to_rgba32},
2156 {PIX_FMT_NV12, PIX_FMT_BGRA32, nv12_to_bgra32},
2157 {PIX_FMT_NV12, PIX_FMT_ARGB32, nv12_to_argb32},
2158 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv12_to_abgr32},
2159 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv12_to_nv21},
2161 {PIX_FMT_NV21, PIX_FMT_RGB555, nv21_to_rgb555},
2162 {PIX_FMT_NV21, PIX_FMT_RGB565, nv21_to_rgb565},
2163 {PIX_FMT_NV21, PIX_FMT_BGR24, nv21_to_bgr24},
2164 {PIX_FMT_NV21, PIX_FMT_RGB24, nv21_to_rgb24},
2165 {PIX_FMT_NV21, PIX_FMT_RGB32, nv21_to_rgb32},
2166 {PIX_FMT_NV21, PIX_FMT_BGR32, nv21_to_bgr32},
2167 {PIX_FMT_NV21, PIX_FMT_xRGB32, nv21_to_xrgb32},
2168 {PIX_FMT_NV21, PIX_FMT_BGRx32, nv21_to_bgrx32},
2169 {PIX_FMT_NV21, PIX_FMT_RGBA32, nv21_to_rgba32},
2170 {PIX_FMT_NV21, PIX_FMT_BGRA32, nv21_to_bgra32},
2171 {PIX_FMT_NV21, PIX_FMT_ARGB32, nv21_to_argb32},
2172 {PIX_FMT_NV21, PIX_FMT_ABGR32, nv21_to_abgr32},
2173 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv21_to_nv12},
2175 {PIX_FMT_YUV422P, PIX_FMT_YUV422, yuv422p_to_yuv422},
2176 {PIX_FMT_YUV422P, PIX_FMT_UYVY422, yuv422p_to_uyvy422},
2178 {PIX_FMT_YUV444P, PIX_FMT_RGB24, yuv444p_to_rgb24},
2180 {PIX_FMT_YUVJ420P, PIX_FMT_RGB555, yuvj420p_to_rgb555},
2181 {PIX_FMT_YUVJ420P, PIX_FMT_RGB565, yuvj420p_to_rgb565},
2182 {PIX_FMT_YUVJ420P, PIX_FMT_BGR24, yuvj420p_to_bgr24},
2183 {PIX_FMT_YUVJ420P, PIX_FMT_RGB24, yuvj420p_to_rgb24},
2184 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_rgb32},
2185 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgr32},
2186 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_xrgb32},
2187 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgrx32},
2188 {PIX_FMT_YUVJ420P, PIX_FMT_RGBA32, yuvj420p_to_rgba32},
2189 {PIX_FMT_YUVJ420P, PIX_FMT_BGRA32, yuvj420p_to_bgra32},
2190 {PIX_FMT_YUVJ420P, PIX_FMT_ARGB32, yuvj420p_to_argb32},
2191 {PIX_FMT_YUVJ420P, PIX_FMT_ABGR32, yuvj420p_to_abgr32},
2193 {PIX_FMT_YUVJ444P, PIX_FMT_RGB24, yuvj444p_to_rgb24},
2195 {PIX_FMT_YUV422, PIX_FMT_YUV420P, yuv422_to_yuv420p},
2196 {PIX_FMT_YUV422, PIX_FMT_YUV422P, yuv422_to_yuv422p},
2198 {PIX_FMT_UYVY422, PIX_FMT_YUV420P, uyvy422_to_yuv420p},
2199 {PIX_FMT_UYVY422, PIX_FMT_YUV422P, uyvy422_to_yuv422p},
2200 {PIX_FMT_UYVY422, PIX_FMT_GRAY8, uyvy422_to_gray},
2202 {PIX_FMT_RGB24, PIX_FMT_YUV420P, rgb24_to_yuv420p},
2203 {PIX_FMT_RGB24, PIX_FMT_NV12, rgb24_to_nv12},
2204 {PIX_FMT_RGB24, PIX_FMT_NV21, rgb24_to_nv21},
2205 {PIX_FMT_RGB24, PIX_FMT_RGB565, rgb24_to_rgb565},
2206 {PIX_FMT_RGB24, PIX_FMT_RGB555, rgb24_to_rgb555},
2207 {PIX_FMT_RGB24, PIX_FMT_RGB32, rgb24_to_rgb32},
2208 {PIX_FMT_RGB24, PIX_FMT_BGR32, rgb24_to_bgr32},
2209 {PIX_FMT_RGB24, PIX_FMT_xRGB32, rgb24_to_xrgb32},
2210 {PIX_FMT_RGB24, PIX_FMT_BGRx32, rgb24_to_bgrx32},
2211 {PIX_FMT_RGB24, PIX_FMT_RGBA32, rgb24_to_rgba32},
2212 {PIX_FMT_RGB24, PIX_FMT_BGR24, rgb24_to_bgr24},
2213 {PIX_FMT_RGB24, PIX_FMT_BGRA32, rgb24_to_bgra32},
2214 {PIX_FMT_RGB24, PIX_FMT_ARGB32, rgb24_to_argb32},
2215 {PIX_FMT_RGB24, PIX_FMT_ABGR32, rgb24_to_abgr32},
2216 {PIX_FMT_RGB24, PIX_FMT_GRAY8, rgb24_to_gray},
2217 {PIX_FMT_RGB24, PIX_FMT_PAL8, rgb24_to_pal8},
2218 {PIX_FMT_RGB24, PIX_FMT_YUV444P, rgb24_to_yuv444p},
2219 {PIX_FMT_RGB24, PIX_FMT_YUVJ420P, rgb24_to_yuvj420p},
2220 {PIX_FMT_RGB24, PIX_FMT_YUVJ444P, rgb24_to_yuvj444p},
2221 {PIX_FMT_RGB24, PIX_FMT_AYUV4444, rgb24_to_ayuv4444},
2222 {PIX_FMT_RGB24, PIX_FMT_V308, rgb24_to_v308},
2224 {PIX_FMT_RGB32, PIX_FMT_RGB24, rgb32_to_rgb24},
2225 {PIX_FMT_RGB32, PIX_FMT_RGB555, rgba32_to_rgb555},
2226 {PIX_FMT_RGB32, PIX_FMT_PAL8, rgb32_to_pal8},
2227 {PIX_FMT_RGB32, PIX_FMT_YUV420P, rgb32_to_yuv420p},
2228 {PIX_FMT_RGB32, PIX_FMT_NV12, rgb32_to_nv12},
2229 {PIX_FMT_RGB32, PIX_FMT_NV21, rgb32_to_nv21},
2230 {PIX_FMT_RGB32, PIX_FMT_GRAY8, rgb32_to_gray},
2232 {PIX_FMT_xRGB32, PIX_FMT_RGB24, xrgb32_to_rgb24},
2233 {PIX_FMT_xRGB32, PIX_FMT_PAL8, xrgb32_to_pal8},
2234 {PIX_FMT_xRGB32, PIX_FMT_YUV420P, xrgb32_to_yuv420p},
2235 {PIX_FMT_xRGB32, PIX_FMT_NV12, xrgb32_to_nv12},
2236 {PIX_FMT_xRGB32, PIX_FMT_NV21, xrgb32_to_nv21},
2237 {PIX_FMT_xRGB32, PIX_FMT_GRAY8, xrgb32_to_gray},
2239 {PIX_FMT_RGBA32, PIX_FMT_BGRA32, rgba32_to_bgra32},
2240 {PIX_FMT_RGBA32, PIX_FMT_ABGR32, rgba32_to_abgr32},
2241 {PIX_FMT_RGBA32, PIX_FMT_ARGB32, rgba32_to_argb32},
2242 {PIX_FMT_RGBA32, PIX_FMT_BGR32, rgba32_to_bgr32},
2243 {PIX_FMT_RGBA32, PIX_FMT_BGRx32, rgba32_to_bgrx32},
2244 {PIX_FMT_RGBA32, PIX_FMT_ABGR32, rgba32_to_abgr32},
2245 {PIX_FMT_RGBA32, PIX_FMT_RGB24, rgba32_to_rgb24},
2246 {PIX_FMT_RGBA32, PIX_FMT_RGB555, rgba32_to_rgb555},
2247 {PIX_FMT_RGBA32, PIX_FMT_PAL8, rgba32_to_pal8},
2248 {PIX_FMT_RGBA32, PIX_FMT_YUV420P, rgba32_to_yuv420p},
2249 {PIX_FMT_RGBA32, PIX_FMT_NV12, rgba32_to_nv12},
2250 {PIX_FMT_RGBA32, PIX_FMT_NV21, rgba32_to_nv21},
2251 {PIX_FMT_RGBA32, PIX_FMT_GRAY8, rgba32_to_gray},
2252 {PIX_FMT_RGBA32, PIX_FMT_AYUV4444, rgba32_to_ayuv4444},
2254 {PIX_FMT_BGR24, PIX_FMT_RGB24, bgr24_to_rgb24},
2255 {PIX_FMT_BGR24, PIX_FMT_YUV420P, bgr24_to_yuv420p},
2256 {PIX_FMT_BGR24, PIX_FMT_NV12, bgr24_to_nv12},
2257 {PIX_FMT_BGR24, PIX_FMT_NV21, bgr24_to_nv21},
2258 {PIX_FMT_BGR24, PIX_FMT_GRAY8, bgr24_to_gray},
2260 {PIX_FMT_BGR32, PIX_FMT_RGB24, bgr32_to_rgb24},
2261 {PIX_FMT_BGR32, PIX_FMT_RGBA32, bgr32_to_rgba32},
2262 {PIX_FMT_BGR32, PIX_FMT_YUV420P, bgr32_to_yuv420p},
2263 {PIX_FMT_BGR32, PIX_FMT_NV12, bgr32_to_nv12},
2264 {PIX_FMT_BGR32, PIX_FMT_NV21, bgr32_to_nv21},
2265 {PIX_FMT_BGR32, PIX_FMT_GRAY8, bgr32_to_gray},
2267 {PIX_FMT_BGRx32, PIX_FMT_RGB24, bgrx32_to_rgb24},
2268 {PIX_FMT_BGRx32, PIX_FMT_RGBA32, bgrx32_to_rgba32},
2269 {PIX_FMT_BGRx32, PIX_FMT_YUV420P, bgrx32_to_yuv420p},
2270 {PIX_FMT_BGRx32, PIX_FMT_NV12, bgrx32_to_nv12},
2271 {PIX_FMT_BGRx32, PIX_FMT_NV21, bgrx32_to_nv21},
2272 {PIX_FMT_BGRx32, PIX_FMT_GRAY8, bgrx32_to_gray},
2274 {PIX_FMT_BGRA32, PIX_FMT_RGB24, bgra32_to_rgb24},
2275 {PIX_FMT_BGRA32, PIX_FMT_RGBA32, bgra32_to_rgba32},
2276 {PIX_FMT_BGRA32, PIX_FMT_YUV420P, bgra32_to_yuv420p},
2277 {PIX_FMT_BGRA32, PIX_FMT_NV12, bgra32_to_nv12},
2278 {PIX_FMT_BGRA32, PIX_FMT_NV21, bgra32_to_nv21},
2279 {PIX_FMT_BGRA32, PIX_FMT_GRAY8, bgra32_to_gray},
2280 {PIX_FMT_BGRA32, PIX_FMT_AYUV4444, bgra32_to_ayuv4444},
2282 {PIX_FMT_ABGR32, PIX_FMT_RGB24, abgr32_to_rgb24},
2283 {PIX_FMT_ABGR32, PIX_FMT_RGBA32, abgr32_to_rgba32},
2284 {PIX_FMT_ABGR32, PIX_FMT_YUV420P, abgr32_to_yuv420p},
2285 {PIX_FMT_ABGR32, PIX_FMT_NV12, abgr32_to_nv12},
2286 {PIX_FMT_ABGR32, PIX_FMT_NV21, abgr32_to_nv21},
2287 {PIX_FMT_ABGR32, PIX_FMT_GRAY8, abgr32_to_gray},
2289 {PIX_FMT_ARGB32, PIX_FMT_RGB24, argb32_to_rgb24},
2290 {PIX_FMT_ARGB32, PIX_FMT_RGBA32, argb32_to_rgba32},
2291 {PIX_FMT_ARGB32, PIX_FMT_YUV420P, argb32_to_yuv420p},
2292 {PIX_FMT_ARGB32, PIX_FMT_NV12, argb32_to_nv12},
2293 {PIX_FMT_ARGB32, PIX_FMT_NV21, argb32_to_nv21},
2294 {PIX_FMT_ARGB32, PIX_FMT_GRAY8, argb32_to_gray},
2296 {PIX_FMT_RGB555, PIX_FMT_RGB24, rgb555_to_rgb24},
2297 {PIX_FMT_RGB555, PIX_FMT_RGB32, rgb555_to_rgba32},
2298 {PIX_FMT_RGB555, PIX_FMT_RGBA32, rgb555_to_rgba32},
2299 {PIX_FMT_RGB555, PIX_FMT_YUV420P, rgb555_to_yuv420p},
2300 {PIX_FMT_RGB555, PIX_FMT_NV12, rgb555_to_nv12},
2301 {PIX_FMT_RGB555, PIX_FMT_NV21, rgb555_to_nv21},
2302 {PIX_FMT_RGB555, PIX_FMT_GRAY8, rgb555_to_gray},
2304 {PIX_FMT_RGB565, PIX_FMT_RGB24, rgb565_to_rgb24},
2305 {PIX_FMT_RGB565, PIX_FMT_YUV420P, rgb565_to_yuv420p},
2306 {PIX_FMT_RGB565, PIX_FMT_NV12, rgb565_to_nv12},
2307 {PIX_FMT_RGB565, PIX_FMT_NV21, rgb565_to_nv21},
2308 {PIX_FMT_RGB565, PIX_FMT_GRAY8, rgb565_to_gray},
2310 {PIX_FMT_GRAY8, PIX_FMT_RGB555, gray_to_rgb555},
2311 {PIX_FMT_GRAY8, PIX_FMT_RGB565, gray_to_rgb565},
2312 {PIX_FMT_GRAY8, PIX_FMT_RGB24, gray_to_rgb24},
2313 {PIX_FMT_GRAY8, PIX_FMT_BGR24, gray_to_bgr24},
2314 {PIX_FMT_GRAY8, PIX_FMT_RGB32, gray_to_rgb32},
2315 {PIX_FMT_GRAY8, PIX_FMT_BGR32, gray_to_bgr32},
2316 {PIX_FMT_GRAY8, PIX_FMT_xRGB32, gray_to_xrgb32},
2317 {PIX_FMT_GRAY8, PIX_FMT_BGRx32, gray_to_bgrx32},
2318 {PIX_FMT_GRAY8, PIX_FMT_RGBA32, gray_to_rgba32},
2319 {PIX_FMT_GRAY8, PIX_FMT_BGRA32, gray_to_bgra32},
2320 {PIX_FMT_GRAY8, PIX_FMT_ARGB32, gray_to_argb32},
2321 {PIX_FMT_GRAY8, PIX_FMT_ABGR32, gray_to_abgr32},
2322 {PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, gray_to_monowhite},
2323 {PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, gray_to_monoblack},
2325 {PIX_FMT_MONOWHITE, PIX_FMT_GRAY8, monowhite_to_gray},
2327 {PIX_FMT_MONOBLACK, PIX_FMT_GRAY8, monoblack_to_gray},
2329 {PIX_FMT_PAL8, PIX_FMT_RGB555, pal8_to_rgb555},
2330 {PIX_FMT_PAL8, PIX_FMT_RGB565, pal8_to_rgb565},
2331 {PIX_FMT_PAL8, PIX_FMT_BGR24, pal8_to_bgr24},
2332 {PIX_FMT_PAL8, PIX_FMT_RGB24, pal8_to_rgb24},
2333 {PIX_FMT_PAL8, PIX_FMT_RGB32, pal8_to_rgb32},
2334 {PIX_FMT_PAL8, PIX_FMT_BGR32, pal8_to_bgr32},
2335 {PIX_FMT_PAL8, PIX_FMT_xRGB32, pal8_to_xrgb32},
2336 {PIX_FMT_PAL8, PIX_FMT_BGRx32, pal8_to_bgrx32},
2337 {PIX_FMT_PAL8, PIX_FMT_RGBA32, pal8_to_rgba32},
2338 {PIX_FMT_PAL8, PIX_FMT_BGRA32, pal8_to_bgra32},
2339 {PIX_FMT_PAL8, PIX_FMT_ARGB32, pal8_to_argb32},
2340 {PIX_FMT_PAL8, PIX_FMT_ABGR32, pal8_to_abgr32},
2342 {PIX_FMT_UYVY411, PIX_FMT_YUV411P, uyvy411_to_yuv411p},
2344 {PIX_FMT_V308, PIX_FMT_RGB24, v308_to_rgb24},
2346 {PIX_FMT_AYUV4444, PIX_FMT_RGBA32, ayuv4444_to_rgba32},
2347 {PIX_FMT_AYUV4444, PIX_FMT_RGB24, ayuv4444_to_rgb24},
2350 static ConvertEntry *
2351 get_convert_table_entry (int src_pix_fmt, int dst_pix_fmt)
2355 for (i = 0; i < sizeof (convert_table) / sizeof (convert_table[0]); i++) {
2356 if (convert_table[i].src == src_pix_fmt &&
2357 convert_table[i].dest == dst_pix_fmt) {
2358 return convert_table + i;
2366 avpicture_alloc (AVPicture * picture, int pix_fmt, int width, int height)
2371 size = avpicture_get_size (pix_fmt, width, height);
2372 ptr = av_malloc (size);
2375 gst_ffmpegcsp_avpicture_fill (picture, ptr, pix_fmt, width, height);
2378 memset (picture, 0, sizeof (AVPicture));
2383 avpicture_free (AVPicture * picture)
2385 av_free (picture->data[0]);
2388 /* return true if yuv planar */
2390 is_yuv_planar (PixFmtInfo * ps)
2392 return (ps->color_type == FF_COLOR_YUV ||
2393 ps->color_type == FF_COLOR_YUV_JPEG) && ps->pixel_type == FF_PIXEL_PLANAR;
2396 /* XXX: always use linesize. Return -1 if not supported */
2398 img_convert (AVPicture * dst, int dst_pix_fmt,
2399 const AVPicture * src, int src_pix_fmt, int src_width, int src_height)
2402 int i, ret, dst_width, dst_height, int_pix_fmt;
2403 PixFmtInfo *src_pix, *dst_pix;
2405 AVPicture tmp1, *tmp = &tmp1;
2407 if (G_UNLIKELY (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2408 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB))
2410 if (G_UNLIKELY (src_width <= 0 || src_height <= 0))
2413 if (G_UNLIKELY (!inited)) {
2415 img_convert_init ();
2418 dst_width = src_width;
2419 dst_height = src_height;
2421 dst_pix = get_pix_fmt_info (dst_pix_fmt);
2422 src_pix = get_pix_fmt_info (src_pix_fmt);
2423 if (G_UNLIKELY (src_pix_fmt == dst_pix_fmt)) {
2424 /* no conversion needed: just copy */
2425 img_copy (dst, src, dst_pix_fmt, dst_width, dst_height);
2429 ce = get_convert_table_entry (src_pix_fmt, dst_pix_fmt);
2430 if (ce && ce->convert) {
2431 /* specific conversion routine */
2432 ce->convert (dst, src, dst_width, dst_height);
2437 if (is_yuv_planar (dst_pix) && src_pix_fmt == PIX_FMT_GRAY8) {
2441 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2442 img_copy_plane (dst->data[0], dst->linesize[0],
2443 src->data[0], src->linesize[0], dst_width, dst_height);
2445 img_apply_table (dst->data[0], dst->linesize[0],
2446 src->data[0], src->linesize[0],
2447 dst_width, dst_height, y_jpeg_to_ccir);
2449 /* fill U and V with 128 */
2452 w >>= dst_pix->x_chroma_shift;
2453 h >>= dst_pix->y_chroma_shift;
2454 for (i = 1; i <= 2; i++) {
2456 for (y = 0; y < h; y++) {
2458 d += dst->linesize[i];
2465 if (is_yuv_planar (src_pix) && dst_pix_fmt == PIX_FMT_GRAY8) {
2466 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2467 img_copy_plane (dst->data[0], dst->linesize[0],
2468 src->data[0], src->linesize[0], dst_width, dst_height);
2470 img_apply_table (dst->data[0], dst->linesize[0],
2471 src->data[0], src->linesize[0],
2472 dst_width, dst_height, y_ccir_to_jpeg);
2477 /* YUV to YUV planar */
2478 if (is_yuv_planar (dst_pix) && is_yuv_planar (src_pix)) {
2479 int x_shift, y_shift, w, h, xy_shift;
2480 void (*resize_func) (uint8_t * dst, int dst_wrap,
2481 const uint8_t * src, int src_wrap, int width, int height);
2483 /* compute chroma size of the smallest dimensions */
2486 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2487 w >>= dst_pix->x_chroma_shift;
2489 w >>= src_pix->x_chroma_shift;
2490 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2491 h >>= dst_pix->y_chroma_shift;
2493 h >>= src_pix->y_chroma_shift;
2495 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2496 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2497 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2499 /* there must be filters for conversion at least from and to
2503 resize_func = img_copy_plane;
2506 resize_func = shrink21;
2509 resize_func = shrink41;
2512 resize_func = shrink12;
2515 resize_func = shrink22;
2518 resize_func = shrink44;
2521 resize_func = grow21;
2524 resize_func = grow41;
2527 resize_func = grow22;
2530 resize_func = grow44;
2533 resize_func = conv411;
2536 /* currently not handled */
2537 goto no_chroma_filter;
2540 img_copy_plane (dst->data[0], dst->linesize[0],
2541 src->data[0], src->linesize[0], dst_width, dst_height);
2543 #define GEN_MASK(x) ((1<<(x))-1)
2544 #define DIV_ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) >> (x))
2546 for (i = 1; i <= 2; i++) {
2549 w = DIV_ROUND_UP_X (dst_width, dst_pix->x_chroma_shift);
2550 h = DIV_ROUND_UP_X (dst_height, dst_pix->y_chroma_shift);
2552 resize_func (dst->data[i], dst->linesize[i],
2553 src->data[i], src->linesize[i], w, h);
2555 /* if yuv color space conversion is needed, we do it here on
2556 the destination image */
2557 if (dst_pix->color_type != src_pix->color_type) {
2558 const uint8_t *y_table, *c_table;
2560 if (dst_pix->color_type == FF_COLOR_YUV) {
2561 y_table = y_jpeg_to_ccir;
2562 c_table = c_jpeg_to_ccir;
2564 y_table = y_ccir_to_jpeg;
2565 c_table = c_ccir_to_jpeg;
2567 img_apply_table (dst->data[0], dst->linesize[0],
2568 dst->data[0], dst->linesize[0], dst_width, dst_height, y_table);
2570 for (i = 1; i <= 2; i++)
2571 img_apply_table (dst->data[i], dst->linesize[i],
2572 dst->data[i], dst->linesize[i],
2573 dst_width >> dst_pix->x_chroma_shift,
2574 dst_height >> dst_pix->y_chroma_shift, c_table);
2580 /* try to use an intermediate format */
2581 if (src_pix_fmt == PIX_FMT_YUV422 || dst_pix_fmt == PIX_FMT_YUV422) {
2582 /* specific case: convert to YUV422P first */
2583 int_pix_fmt = PIX_FMT_YUV422P;
2584 } else if (src_pix_fmt == PIX_FMT_UYVY422 || dst_pix_fmt == PIX_FMT_UYVY422) {
2585 /* specific case: convert to YUV422P first */
2586 int_pix_fmt = PIX_FMT_YUV422P;
2587 } else if (src_pix_fmt == PIX_FMT_UYVY411 || dst_pix_fmt == PIX_FMT_UYVY411) {
2588 /* specific case: convert to YUV411P first */
2589 int_pix_fmt = PIX_FMT_YUV411P;
2590 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2591 src_pix_fmt != PIX_FMT_GRAY8) ||
2592 (dst_pix->color_type == FF_COLOR_GRAY && dst_pix_fmt != PIX_FMT_GRAY8)) {
2593 /* gray8 is the normalized format */
2594 int_pix_fmt = PIX_FMT_GRAY8;
2595 } else if ((is_yuv_planar (src_pix) &&
2596 src_pix_fmt != PIX_FMT_YUV444P && src_pix_fmt != PIX_FMT_YUVJ444P)) {
2597 /* yuv444 is the normalized format */
2598 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2599 int_pix_fmt = PIX_FMT_YUVJ444P;
2601 int_pix_fmt = PIX_FMT_YUV444P;
2602 } else if ((is_yuv_planar (dst_pix) &&
2603 dst_pix_fmt != PIX_FMT_YUV444P && dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2604 /* yuv444 is the normalized format */
2605 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2606 int_pix_fmt = PIX_FMT_YUVJ444P;
2608 int_pix_fmt = PIX_FMT_YUV444P;
2610 /* the two formats are rgb or gray8 or yuv[j]444p */
2611 if (src_pix->is_alpha && dst_pix->is_alpha)
2612 int_pix_fmt = PIX_FMT_RGBA32;
2614 int_pix_fmt = PIX_FMT_RGB24;
2616 if (avpicture_alloc (tmp, int_pix_fmt, dst_width, dst_height) < 0)
2619 if (img_convert (tmp, int_pix_fmt,
2620 src, src_pix_fmt, src_width, src_height) < 0)
2623 if (img_convert (dst, dst_pix_fmt,
2624 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2628 avpicture_free (tmp);
2632 /* NOTE: we scan all the pixels to have an exact information */
2634 get_alpha_info_pal8 (const AVPicture * src, int width, int height)
2636 const unsigned char *p;
2637 int src_wrap, ret, x, y;
2639 uint32_t *palette = (uint32_t *) src->data[1];
2642 src_wrap = src->linesize[0] - width;
2644 for (y = 0; y < height; y++) {
2645 for (x = 0; x < width; x++) {
2646 a = palette[p[0]] >> 24;
2648 ret |= FF_ALPHA_TRANSP;
2649 } else if (a != 0xff) {
2650 ret |= FF_ALPHA_SEMI_TRANSP;
2660 * Tell if an image really has transparent alpha values.
2661 * @return ored mask of FF_ALPHA_xxx constants
2664 img_get_alpha_info (const AVPicture * src, int pix_fmt, int width, int height)
2666 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
2669 pf = get_pix_fmt_info (pix_fmt);
2670 /* no alpha can be represented in format */
2675 ret = get_alpha_info_rgb32 (src, width, height);
2678 ret = get_alpha_info_bgr32 (src, width, height);
2680 case PIX_FMT_xRGB32:
2681 ret = get_alpha_info_xrgb32 (src, width, height);
2683 case PIX_FMT_BGRx32:
2684 ret = get_alpha_info_bgrx32 (src, width, height);
2686 case PIX_FMT_RGBA32:
2687 ret = get_alpha_info_rgba32 (src, width, height);
2689 case PIX_FMT_BGRA32:
2690 ret = get_alpha_info_bgra32 (src, width, height);
2692 case PIX_FMT_ARGB32:
2693 ret = get_alpha_info_argb32 (src, width, height);
2695 case PIX_FMT_ABGR32:
2696 ret = get_alpha_info_abgr32 (src, width, height);
2698 case PIX_FMT_RGB555:
2699 ret = get_alpha_info_rgb555 (src, width, height);
2702 ret = get_alpha_info_pal8 (src, width, height);
2705 /* we do not know, so everything is indicated */
2706 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2713 #define DEINT_INPLACE_LINE_LUM \
2714 movd_m2r(lum_m4[0],mm0);\
2715 movd_m2r(lum_m3[0],mm1);\
2716 movd_m2r(lum_m2[0],mm2);\
2717 movd_m2r(lum_m1[0],mm3);\
2718 movd_m2r(lum[0],mm4);\
2719 punpcklbw_r2r(mm7,mm0);\
2720 movd_r2m(mm2,lum_m4[0]);\
2721 punpcklbw_r2r(mm7,mm1);\
2722 punpcklbw_r2r(mm7,mm2);\
2723 punpcklbw_r2r(mm7,mm3);\
2724 punpcklbw_r2r(mm7,mm4);\
2725 paddw_r2r(mm3,mm1);\
2727 paddw_r2r(mm4,mm0);\
2729 paddw_r2r(mm6,mm2);\
2730 paddw_r2r(mm2,mm1);\
2731 psubusw_r2r(mm0,mm1);\
2733 packuswb_r2r(mm7,mm1);\
2734 movd_r2m(mm1,lum_m2[0]);
2736 #define DEINT_LINE_LUM \
2737 movd_m2r(lum_m4[0],mm0);\
2738 movd_m2r(lum_m3[0],mm1);\
2739 movd_m2r(lum_m2[0],mm2);\
2740 movd_m2r(lum_m1[0],mm3);\
2741 movd_m2r(lum[0],mm4);\
2742 punpcklbw_r2r(mm7,mm0);\
2743 punpcklbw_r2r(mm7,mm1);\
2744 punpcklbw_r2r(mm7,mm2);\
2745 punpcklbw_r2r(mm7,mm3);\
2746 punpcklbw_r2r(mm7,mm4);\
2747 paddw_r2r(mm3,mm1);\
2749 paddw_r2r(mm4,mm0);\
2751 paddw_r2r(mm6,mm2);\
2752 paddw_r2r(mm2,mm1);\
2753 psubusw_r2r(mm0,mm1);\
2755 packuswb_r2r(mm7,mm1);\
2756 movd_r2m(mm1,dst[0]);
2759 /* filter parameters: [-1 4 2 4 -1] // 8 */
2762 deinterlace_line (uint8_t * dst,
2763 const uint8_t * lum_m4, const uint8_t * lum_m3,
2764 const uint8_t * lum_m2, const uint8_t * lum_m1,
2765 const uint8_t * lum, int size)
2768 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2771 for (; size > 0; size--) {
2773 sum += lum_m3[0] << 2;
2774 sum += lum_m2[0] << 1;
2775 sum += lum_m1[0] << 2;
2777 dst[0] = cm[(sum + 4) >> 3];
2794 pxor_r2r (mm7, mm7);
2795 movq_m2r (rounder, mm6);
2797 for (; size > 3; size -= 4) {
2798 DEINT_LINE_LUM lum_m4 += 4;
2810 deinterlace_line_inplace (uint8_t * lum_m4, uint8_t * lum_m3, uint8_t * lum_m2,
2811 uint8_t * lum_m1, uint8_t * lum, int size)
2814 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2817 for (; size > 0; size--) {
2819 sum += lum_m3[0] << 2;
2820 sum += lum_m2[0] << 1;
2821 lum_m4[0] = lum_m2[0];
2822 sum += lum_m1[0] << 2;
2824 lum_m2[0] = cm[(sum + 4) >> 3];
2840 pxor_r2r (mm7, mm7);
2841 movq_m2r (rounder, mm6);
2843 for (; size > 3; size -= 4) {
2844 DEINT_INPLACE_LINE_LUM lum_m4 += 4;
2855 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2856 top field is copied as is, but the bottom field is deinterlaced
2857 against the top field. */
2860 deinterlace_bottom_field (uint8_t * dst, int dst_wrap,
2861 const uint8_t * src1, int src_wrap, int width, int height)
2863 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2868 src_0 = &src_m1[src_wrap];
2869 src_p1 = &src_0[src_wrap];
2870 src_p2 = &src_p1[src_wrap];
2871 for (y = 0; y < (height - 2); y += 2) {
2872 memcpy (dst, src_m1, width);
2874 deinterlace_line (dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
2878 src_p1 += 2 * src_wrap;
2879 src_p2 += 2 * src_wrap;
2882 memcpy (dst, src_m1, width);
2885 deinterlace_line (dst, src_m2, src_m1, src_0, src_0, src_0, width);
2889 deinterlace_bottom_field_inplace (uint8_t * src1, int src_wrap,
2890 int width, int height)
2892 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2896 buf = (uint8_t *) av_malloc (width);
2899 memcpy (buf, src_m1, width);
2900 src_0 = &src_m1[src_wrap];
2901 src_p1 = &src_0[src_wrap];
2902 src_p2 = &src_p1[src_wrap];
2903 for (y = 0; y < (height - 2); y += 2) {
2904 deinterlace_line_inplace (buf, src_m1, src_0, src_p1, src_p2, width);
2907 src_p1 += 2 * src_wrap;
2908 src_p2 += 2 * src_wrap;
2911 deinterlace_line_inplace (buf, src_m1, src_0, src_0, src_0, width);
2916 /* deinterlace - if not supported return -1 */
2919 avpicture_deinterlace (AVPicture * dst, const AVPicture * src,
2920 int pix_fmt, int width, int height)
2924 if (pix_fmt != PIX_FMT_YUV420P &&
2925 pix_fmt != PIX_FMT_YUV422P &&
2926 pix_fmt != PIX_FMT_YUV444P && pix_fmt != PIX_FMT_YUV411P)
2928 if ((width & 3) != 0 || (height & 3) != 0)
2931 for (i = 0; i < 3; i++) {
2934 case PIX_FMT_YUV420P:
2938 case PIX_FMT_YUV422P:
2941 case PIX_FMT_YUV411P:
2949 deinterlace_bottom_field_inplace (dst->data[i], dst->linesize[i],
2952 deinterlace_bottom_field (dst->data[i], dst->linesize[i],
2953 src->data[i], src->linesize[i], width, height);