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_yuv420p (AVPicture * dst, const AVPicture * src,
822 int width, int height)
824 const uint8_t *p, *p1;
825 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
834 for (; height >= 1; height -= 2) {
839 for (w = width; w >= 2; w -= 2) {
856 p1 += src->linesize[0];
857 lum1 += dst->linesize[0];
861 for (w = width; w >= 2; w -= 2) {
870 p1 += src->linesize[0];
871 lum1 += dst->linesize[0];
873 cb1 += dst->linesize[1];
874 cr1 += dst->linesize[2];
880 uyvy422_to_yuv422p (AVPicture * dst, const AVPicture * src,
881 int width, int height)
883 const uint8_t *p, *p1;
884 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
891 for (; height > 0; height--) {
896 for (w = width; w >= 2; w -= 2) {
906 p1 += src->linesize[0];
907 lum1 += dst->linesize[0];
908 cb1 += dst->linesize[1];
909 cr1 += dst->linesize[2];
915 yuv422_to_yuv422p (AVPicture * dst, const AVPicture * src,
916 int width, int height)
918 const uint8_t *p, *p1;
919 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
926 for (; height > 0; height--) {
931 for (w = width; w >= 2; w -= 2) {
941 p1 += src->linesize[0];
942 lum1 += dst->linesize[0];
943 cb1 += dst->linesize[1];
944 cr1 += dst->linesize[2];
949 yuv422p_to_yuv422 (AVPicture * dst, const AVPicture * src,
950 int width, int height)
953 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
960 for (; height > 0; height--) {
965 for (w = width; w >= 2; w -= 2) {
979 p1 += dst->linesize[0];
980 lum1 += src->linesize[0];
981 cb1 += src->linesize[1];
982 cr1 += src->linesize[2];
987 yuv422p_to_uyvy422 (AVPicture * dst, const AVPicture * src,
988 int width, int height)
991 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
998 for (; height > 0; height--) {
1003 for (w = width; w >= 2; w -= 2) {
1013 p1 += dst->linesize[0];
1014 lum1 += src->linesize[0];
1015 cb1 += src->linesize[1];
1016 cr1 += src->linesize[2];
1021 uyvy411_to_yuv411p (AVPicture * dst, const AVPicture * src,
1022 int width, int height)
1024 const uint8_t *p, *p1;
1025 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1029 lum1 = dst->data[0];
1032 for (; height > 0; height--) {
1037 for (w = width; w >= 4; w -= 4) {
1049 p1 += src->linesize[0];
1050 lum1 += dst->linesize[0];
1051 cb1 += dst->linesize[1];
1052 cr1 += dst->linesize[2];
1058 yuv420p_to_yuv422 (AVPicture * dst, const AVPicture * src,
1059 int width, int height)
1062 uint8_t *line1, *line2, *linesrc = dst->data[0];
1063 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1064 uint8_t *cb1, *cb2 = src->data[1];
1065 uint8_t *cr1, *cr2 = src->data[2];
1067 for (h = height / 2; h--;) {
1069 line2 = linesrc + dst->linesize[0];
1072 lum2 = lumsrc + src->linesize[0];
1077 for (w = width / 2; w--;) {
1080 *line1++ = *line2++ = *cb1++;
1083 *line1++ = *line2++ = *cr1++;
1086 if (width % 2 != 0) {
1089 *line1++ = *line2++ = *cb1++;
1092 linesrc += dst->linesize[0] * 2;
1093 lumsrc += src->linesize[0] * 2;
1094 cb2 += src->linesize[1];
1095 cr2 += src->linesize[2];
1098 if (height % 2 != 0) {
1104 for (w = width / 2; w--;) {
1111 if (width % 2 != 0) {
1119 nv12_to_nv21 (AVPicture * dst, const AVPicture * src, int width, int height)
1121 const uint8_t *s_c_ptr;
1125 s_c_ptr = src->data[1];
1126 d_c_ptr = dst->data[1];
1128 for (; height >= 2; height -= 2) {
1129 for (w = width; w >= 2; w -= 2) {
1130 d_c_ptr[0] = s_c_ptr[1];
1131 d_c_ptr[1] = s_c_ptr[0];
1136 /* handle odd width */
1138 d_c_ptr[0] = s_c_ptr[1];
1139 d_c_ptr[1] = s_c_ptr[0];
1143 s_c_ptr += src->linesize[1] - width;
1144 d_c_ptr += src->linesize[1] - width;
1146 /* handle odd height */
1148 for (w = width; w >= 2; w -= 2) {
1149 d_c_ptr[0] = s_c_ptr[1];
1150 d_c_ptr[1] = s_c_ptr[0];
1155 /* handle odd width */
1157 d_c_ptr[0] = s_c_ptr[1];
1158 d_c_ptr[1] = s_c_ptr[0];
1165 #define nv21_to_nv12 nv12_to_nv21
1167 #define SCALEBITS 10
1168 #define ONE_HALF (1 << (SCALEBITS - 1))
1169 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1171 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1175 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1176 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1178 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1181 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1183 y = ((y1) - 16) * FIX(255.0/219.0);\
1184 r = cm[(y + r_add) >> SCALEBITS];\
1185 g = cm[(y + g_add) >> SCALEBITS];\
1186 b = cm[(y + b_add) >> SCALEBITS];\
1189 #define YUV_TO_RGB1(cb1, cr1)\
1193 r_add = FIX(1.40200) * cr + ONE_HALF;\
1194 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1195 b_add = FIX(1.77200) * cb + ONE_HALF;\
1198 #define YUV_TO_RGB2(r, g, b, y1)\
1200 y = (y1) << SCALEBITS;\
1201 r = cm[(y + r_add) >> SCALEBITS];\
1202 g = cm[(y + g_add) >> SCALEBITS];\
1203 b = cm[(y + b_add) >> SCALEBITS];\
1206 #define Y_CCIR_TO_JPEG(y)\
1207 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1209 #define Y_JPEG_TO_CCIR(y)\
1210 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1212 #define C_CCIR_TO_JPEG(y)\
1213 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1215 /* NOTE: the clamp is really necessary! */
1217 C_JPEG_TO_CCIR (int y)
1219 y = (((y - 128) * FIX (112.0 / 127.0) + (ONE_HALF +
1220 (128 << SCALEBITS))) >> SCALEBITS);
1227 #define RGB_TO_Y(r, g, b) \
1228 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1229 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1231 #define RGB_TO_U(r1, g1, b1, shift)\
1232 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1233 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1235 #define RGB_TO_V(r1, g1, b1, shift)\
1236 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1237 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1239 #define RGB_TO_Y_CCIR(r, g, b) \
1240 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1241 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1243 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1244 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1245 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1247 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1248 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1249 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1251 static uint8_t y_ccir_to_jpeg[256];
1252 static uint8_t y_jpeg_to_ccir[256];
1253 static uint8_t c_ccir_to_jpeg[256];
1254 static uint8_t c_jpeg_to_ccir[256];
1256 /* init various conversion tables */
1258 img_convert_init (void)
1261 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1263 for (i = 0; i < 256; i++) {
1264 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG (i);
1265 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR (i);
1266 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG (i);
1267 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR (i);
1271 /* apply to each pixel the given table */
1273 img_apply_table (uint8_t * dst, int dst_wrap,
1274 const uint8_t * src, int src_wrap,
1275 int width, int height, const uint8_t * table1)
1280 const uint8_t *table;
1283 for (; height > 0; height--) {
1307 /* XXX: use generic filter ? */
1308 /* XXX: in most cases, the sampling position is incorrect */
1312 shrink41 (uint8_t * dst, int dst_wrap,
1313 const uint8_t * src, int src_wrap, int width, int height)
1319 for (; height > 0; height--) {
1322 for (w = width; w > 0; w--) {
1323 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1334 shrink21 (uint8_t * dst, int dst_wrap,
1335 const uint8_t * src, int src_wrap, int width, int height)
1341 for (; height > 0; height--) {
1344 for (w = width; w > 0; w--) {
1345 d[0] = (s[0] + s[1]) >> 1;
1356 shrink12 (uint8_t * dst, int dst_wrap,
1357 const uint8_t * src, int src_wrap, int width, int height)
1361 const uint8_t *s1, *s2;
1363 for (; height > 0; height--) {
1367 for (w = width; w >= 4; w -= 4) {
1368 d[0] = (s1[0] + s2[0]) >> 1;
1369 d[1] = (s1[1] + s2[1]) >> 1;
1370 d[2] = (s1[2] + s2[2]) >> 1;
1371 d[3] = (s1[3] + s2[3]) >> 1;
1376 for (; w > 0; w--) {
1377 d[0] = (s1[0] + s2[0]) >> 1;
1382 src += 2 * src_wrap;
1389 shrink22 (uint8_t * dst, int dst_wrap,
1390 const uint8_t * src, int src_wrap, int width, int height)
1393 const uint8_t *s1, *s2;
1396 for (; height > 0; height--) {
1400 for (w = width; w >= 4; w -= 4) {
1401 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1402 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1403 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1404 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1409 for (; w > 0; w--) {
1410 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1415 src += 2 * src_wrap;
1422 shrink44 (uint8_t * dst, int dst_wrap,
1423 const uint8_t * src, int src_wrap, int width, int height)
1426 const uint8_t *s1, *s2, *s3, *s4;
1429 for (; height > 0; height--) {
1435 for (w = width; w > 0; w--) {
1436 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1437 s2[0] + s2[1] + s2[2] + s2[3] +
1438 s3[0] + s3[1] + s3[2] + s3[3] +
1439 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1446 src += 4 * src_wrap;
1452 grow21_line (uint8_t * dst, const uint8_t * src, int width)
1460 for (w = width; w >= 4; w -= 4) {
1461 d[1] = d[0] = s1[0];
1462 d[3] = d[2] = s1[1];
1466 for (; w >= 2; w -= 2) {
1467 d[1] = d[0] = s1[0];
1471 /* only needed if width is not a multiple of two */
1472 /* XXX: veryfy that */
1479 grow41_line (uint8_t * dst, const uint8_t * src, int width)
1487 for (w = width; w >= 4; w -= 4) {
1500 grow21 (uint8_t * dst, int dst_wrap,
1501 const uint8_t * src, int src_wrap, int width, int height)
1503 for (; height > 0; height--) {
1504 grow21_line (dst, src, width);
1512 grow22 (uint8_t * dst, int dst_wrap,
1513 const uint8_t * src, int src_wrap, int width, int height)
1515 for (; height > 0; height--) {
1516 grow21_line (dst, src, width);
1525 grow41 (uint8_t * dst, int dst_wrap,
1526 const uint8_t * src, int src_wrap, int width, int height)
1528 for (; height > 0; height--) {
1529 grow41_line (dst, src, width);
1537 grow44 (uint8_t * dst, int dst_wrap,
1538 const uint8_t * src, int src_wrap, int width, int height)
1540 for (; height > 0; height--) {
1541 grow41_line (dst, src, width);
1542 if ((height & 3) == 1)
1550 conv411 (uint8_t * dst, int dst_wrap,
1551 const uint8_t * src, int src_wrap, int width, int height)
1554 const uint8_t *s1, *s2;
1559 for (; height > 0; height--) {
1561 s2 = src + src_wrap;
1563 for (w = width; w > 0; w--) {
1564 c = (s1[0] + s2[0]) >> 1;
1571 src += src_wrap * 2;
1576 /* XXX: add jpeg quantize code */
1578 #define TRANSP_INDEX (6*6*6)
1580 /* this is maybe slow, but allows for extensions */
1581 static inline unsigned char
1582 gif_clut_index (uint8_t r, uint8_t g, uint8_t b)
1584 return ((((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6));
1588 build_rgb_palette (uint8_t * palette, int has_alpha)
1591 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1594 pal = (uint32_t *) palette;
1596 for (r = 0; r < 6; r++) {
1597 for (g = 0; g < 6; g++) {
1598 for (b = 0; b < 6; b++) {
1599 pal[i++] = (0xffU << 24) | (pal_value[r] << 16) |
1600 (pal_value[g] << 8) | pal_value[b];
1607 pal[i++] = 0xff000000;
1610 /* copy bit n to bits 0 ... n - 1 */
1611 static inline unsigned int
1612 bitcopy_n (unsigned int a, int n)
1616 mask = (1 << n) - 1;
1617 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1620 /* rgb555 handling */
1622 #define RGB_NAME rgb555
1624 #define RGB_IN(r, g, b, s)\
1626 unsigned int v = ((const uint16_t *)(s))[0];\
1627 r = bitcopy_n(v >> (10 - 3), 3);\
1628 g = bitcopy_n(v >> (5 - 3), 3);\
1629 b = bitcopy_n(v << 3, 3);\
1632 #define RGBA_IN(r, g, b, a, s)\
1634 unsigned int v = ((const uint16_t *)(s))[0];\
1635 r = bitcopy_n(v >> (10 - 3), 3);\
1636 g = bitcopy_n(v >> (5 - 3), 3);\
1637 b = bitcopy_n(v << 3, 3);\
1638 a = (-(v >> 15)) & 0xff;\
1641 #define RGBA_OUT(d, r, g, b, a)\
1643 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1644 ((a << 8) & 0x8000);\
1649 #include "imgconvert_template.h"
1651 /* rgb565 handling */
1653 #define RGB_NAME rgb565
1655 #define RGB_IN(r, g, b, s)\
1657 unsigned int v = ((const uint16_t *)(s))[0];\
1658 r = bitcopy_n(v >> (11 - 3), 3);\
1659 g = bitcopy_n(v >> (5 - 2), 2);\
1660 b = bitcopy_n(v << 3, 3);\
1663 #define RGB_OUT(d, r, g, b)\
1665 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1670 #include "imgconvert_template.h"
1672 /* bgr24 handling */
1674 #define RGB_NAME bgr24
1676 #define RGB_IN(r, g, b, s)\
1683 #define RGB_OUT(d, r, g, b)\
1692 #include "imgconvert_template.h"
1698 /* rgb24 handling */
1700 #define RGB_NAME rgb24
1703 #define RGB_IN(r, g, b, s)\
1710 #define RGB_OUT(d, r, g, b)\
1719 #include "imgconvert_template.h"
1721 /* rgb32 handling */
1723 #define RGB_NAME rgb32
1726 #define RGB_IN(r, g, b, s)\
1728 unsigned int v = ((const uint32_t *)(s))[0];\
1729 r = (v >> 16) & 0xff;\
1730 g = (v >> 8) & 0xff;\
1734 #define RGBA_IN(r, g, b, a, s)\
1736 unsigned int v = ((const uint32_t *)(s))[0];\
1738 r = (v >> 16) & 0xff;\
1739 g = (v >> 8) & 0xff;\
1743 #define RGBA_OUT(d, r, g, b, a)\
1745 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1750 #include "imgconvert_template.h"
1752 /* bgr32 handling */
1754 #define RGB_NAME bgr32
1756 #define RGB_IN(r, g, b, s)\
1758 unsigned int v = ((const uint32_t *)(s))[0];\
1759 r = (v >> 8) & 0xff;\
1760 g = (v >> 16) & 0xff;\
1761 b = (v >> 24) & 0xff;\
1764 #define RGBA_IN(r, g, b, a, s)\
1766 unsigned int v = ((const uint32_t *)(s))[0];\
1768 r = (v >> 8) & 0xff;\
1769 g = (v >> 16) & 0xff;\
1770 b = (v >> 24) & 0xff;\
1773 #define RGBA_OUT(d, r, g, b, a)\
1775 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24);\
1780 #include "imgconvert_template.h"
1782 /* xrgb32 handling */
1784 #define RGB_NAME xrgb32
1787 #define RGB_IN(r, g, b, s)\
1789 unsigned int v = ((const uint32_t *)(s))[0];\
1790 r = (v >> 24) & 0xff;\
1791 g = (v >> 16) & 0xff;\
1792 b = (v >> 8) & 0xff;\
1795 #define RGBA_IN(r, g, b, a, s)\
1797 unsigned int v = ((const uint32_t *)(s))[0];\
1799 r = (v >> 24) & 0xff;\
1800 g = (v >> 16) & 0xff;\
1801 b = (v >> 8) & 0xff;\
1804 #define RGBA_OUT(d, r, g, b, a)\
1806 ((uint32_t *)(d))[0] = (r << 24) | (g << 16) | (b << 8) | a;\
1811 #include "imgconvert_template.h"
1813 /* bgrx32 handling */
1815 #define RGB_NAME bgrx32
1817 #define RGB_IN(r, g, b, s)\
1819 unsigned int v = ((const uint32_t *)(s))[0];\
1821 g = (v >> 8) & 0xff;\
1822 b = (v >> 16) & 0xff;\
1825 #define RGBA_IN(r, g, b, a, s)\
1827 unsigned int v = ((const uint32_t *)(s))[0];\
1830 g = (v >> 8) & 0xff;\
1831 b = (v >> 16) & 0xff;\
1834 #define RGBA_OUT(d, r, g, b, a)\
1836 ((uint32_t *)(d))[0] = r | (g << 8) | (b << 16) | (a << 24);\
1841 #include "imgconvert_template.h"
1843 /* rgba32 handling */
1845 #define RGB_NAME rgba32
1848 #define RGB_IN(r, g, b, s)\
1850 unsigned int v = ((const uint32_t *)(s))[0];\
1851 r = (v >> 16) & 0xff;\
1852 g = (v >> 8) & 0xff;\
1856 #define RGBA_IN(r, g, b, a, s)\
1858 unsigned int v = ((const uint32_t *)(s))[0];\
1859 a = (v >> 24) & 0xff;\
1860 r = (v >> 16) & 0xff;\
1861 g = (v >> 8) & 0xff;\
1865 #define RGBA_OUT(d, r, g, b, a)\
1867 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1872 #include "imgconvert_template.h"
1874 /* bgra32 handling */
1876 #define RGB_NAME bgra32
1879 #define RGB_IN(r, g, b, s)\
1881 unsigned int v = ((const uint32_t *)(s))[0];\
1882 r = (v >> 8) & 0xff;\
1883 g = (v >> 16) & 0xff;\
1884 b = (v >> 24) & 0xff;\
1887 #define RGBA_IN(r, g, b, a, s)\
1889 unsigned int v = ((const uint32_t *)(s))[0];\
1891 r = (v >> 8) & 0xff;\
1892 g = (v >> 16) & 0xff;\
1893 b = (v >> 24) & 0xff;\
1896 #define RGBA_OUT(d, r, g, b, a)\
1898 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24 );\
1903 #include "imgconvert_template.h"
1905 /* argb32 handling */
1907 #define RGB_NAME argb32
1910 #define RGB_IN(r, g, b, s)\
1912 unsigned int v = ((const uint32_t *)(s))[0];\
1913 r = (v >> 24) & 0xff;\
1914 g = (v >> 16) & 0xff;\
1915 b = (v >> 8) & 0xff;\
1918 #define RGBA_IN(r, g, b, a, s)\
1920 unsigned int v = ((const uint32_t *)(s))[0];\
1921 r = (v >> 24) & 0xff;\
1922 g = (v >> 16) & 0xff;\
1923 b = (v >> 8) & 0xff;\
1927 #define RGBA_OUT(d, r, g, b, a)\
1929 ((uint32_t *)(d))[0] = (r << 24) | (g << 16) | (b << 8) | a;\
1934 #include "imgconvert_template.h"
1936 /* abgr32 handling */
1938 #define RGB_NAME abgr32
1941 #define RGB_IN(r, g, b, s)\
1943 unsigned int v = ((const uint32_t *)(s))[0];\
1945 g = (v >> 8) & 0xff;\
1946 b = (v >> 16) & 0xff;\
1949 #define RGBA_IN(r, g, b, a, s)\
1951 unsigned int v = ((const uint32_t *)(s))[0];\
1953 g = (v >> 8) & 0xff;\
1954 b = (v >> 16) & 0xff;\
1955 a = (v >> 24) & 0xff;\
1958 #define RGBA_OUT(d, r, g, b, a)\
1960 ((uint32_t *)(d))[0] = r | (g << 8) | (b << 16) | (a << 24 );\
1965 #include "imgconvert_template.h"
1968 mono_to_gray (AVPicture * dst, const AVPicture * src,
1969 int width, int height, int xor_mask)
1971 const unsigned char *p;
1973 int v, dst_wrap, src_wrap;
1977 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1980 dst_wrap = dst->linesize[0] - width;
1981 for (y = 0; y < height; y++) {
1984 v = *p++ ^ xor_mask;
1986 q[1] = -((v >> 6) & 1);
1987 q[2] = -((v >> 5) & 1);
1988 q[3] = -((v >> 4) & 1);
1989 q[4] = -((v >> 3) & 1);
1990 q[5] = -((v >> 2) & 1);
1991 q[6] = -((v >> 1) & 1);
1992 q[7] = -((v >> 0) & 1);
1997 v = *p++ ^ xor_mask;
1999 q[0] = -((v >> 7) & 1);
2010 monowhite_to_gray (AVPicture * dst, const AVPicture * src,
2011 int width, int height)
2013 mono_to_gray (dst, src, width, height, 0xff);
2017 monoblack_to_gray (AVPicture * dst, const AVPicture * src,
2018 int width, int height)
2020 mono_to_gray (dst, src, width, height, 0x00);
2024 gray_to_mono (AVPicture * dst, const AVPicture * src,
2025 int width, int height, int xor_mask)
2030 int j, b, v, n1, src_wrap, dst_wrap, y;
2033 src_wrap = src->linesize[0] - width;
2036 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
2038 for (y = 0; y < height; y++) {
2042 for (j = 0; j < 8; j++) {
2045 v = (v << 1) | (b >> 7);
2047 d[0] = v ^ xor_mask;
2057 v = (v << 1) | (b >> 7);
2060 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
2069 gray_to_monowhite (AVPicture * dst, const AVPicture * src,
2070 int width, int height)
2072 gray_to_mono (dst, src, width, height, 0xff);
2076 gray_to_monoblack (AVPicture * dst, const AVPicture * src,
2077 int width, int height)
2079 gray_to_mono (dst, src, width, height, 0x00);
2082 typedef struct ConvertEntry
2084 enum PixelFormat src;
2085 enum PixelFormat dest;
2086 void (*convert) (AVPicture * dst,
2087 const AVPicture * src, int width, int height);
2090 /* Add each new convertion function in this table. In order to be able
2091 to convert from any format to any format, the following constraints
2094 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
2096 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
2098 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
2100 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
2103 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
2105 The other conversion functions are just optimisations for common cases.
2107 static ConvertEntry convert_table[] = {
2108 {PIX_FMT_YUV420P, PIX_FMT_YUV422, yuv420p_to_yuv422},
2109 {PIX_FMT_YUV420P, PIX_FMT_RGB555, yuv420p_to_rgb555},
2110 {PIX_FMT_YUV420P, PIX_FMT_RGB565, yuv420p_to_rgb565},
2111 {PIX_FMT_YUV420P, PIX_FMT_BGR24, yuv420p_to_bgr24},
2112 {PIX_FMT_YUV420P, PIX_FMT_RGB24, yuv420p_to_rgb24},
2113 {PIX_FMT_YUV420P, PIX_FMT_RGB32, yuv420p_to_rgb32},
2114 {PIX_FMT_YUV420P, PIX_FMT_BGR32, yuv420p_to_bgr32},
2115 {PIX_FMT_YUV420P, PIX_FMT_xRGB32, yuv420p_to_xrgb32},
2116 {PIX_FMT_YUV420P, PIX_FMT_BGRx32, yuv420p_to_bgrx32},
2117 {PIX_FMT_YUV420P, PIX_FMT_RGBA32, yuv420p_to_rgba32},
2118 {PIX_FMT_YUV420P, PIX_FMT_BGRA32, yuv420p_to_bgra32},
2119 {PIX_FMT_YUV420P, PIX_FMT_ARGB32, yuv420p_to_argb32},
2120 {PIX_FMT_YUV420P, PIX_FMT_ABGR32, yuv420p_to_abgr32},
2122 {PIX_FMT_NV12, PIX_FMT_RGB555, nv12_to_rgb555},
2123 {PIX_FMT_NV12, PIX_FMT_RGB565, nv12_to_rgb565},
2124 {PIX_FMT_NV12, PIX_FMT_BGR24, nv12_to_bgr24},
2125 {PIX_FMT_NV12, PIX_FMT_RGB24, nv12_to_rgb24},
2126 {PIX_FMT_NV12, PIX_FMT_RGB32, nv12_to_rgb32},
2127 {PIX_FMT_NV12, PIX_FMT_BGR32, nv12_to_bgr32},
2128 {PIX_FMT_NV12, PIX_FMT_xRGB32, nv12_to_xrgb32},
2129 {PIX_FMT_NV12, PIX_FMT_BGRx32, nv12_to_bgrx32},
2130 {PIX_FMT_NV12, PIX_FMT_RGBA32, nv12_to_rgba32},
2131 {PIX_FMT_NV12, PIX_FMT_BGRA32, nv12_to_bgra32},
2132 {PIX_FMT_NV12, PIX_FMT_ARGB32, nv12_to_argb32},
2133 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv12_to_abgr32},
2134 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv12_to_nv21},
2136 {PIX_FMT_NV21, PIX_FMT_RGB555, nv21_to_rgb555},
2137 {PIX_FMT_NV21, PIX_FMT_RGB565, nv21_to_rgb565},
2138 {PIX_FMT_NV21, PIX_FMT_BGR24, nv21_to_bgr24},
2139 {PIX_FMT_NV21, PIX_FMT_RGB24, nv21_to_rgb24},
2140 {PIX_FMT_NV21, PIX_FMT_RGB32, nv21_to_rgb32},
2141 {PIX_FMT_NV21, PIX_FMT_BGR32, nv21_to_bgr32},
2142 {PIX_FMT_NV21, PIX_FMT_xRGB32, nv21_to_xrgb32},
2143 {PIX_FMT_NV21, PIX_FMT_BGRx32, nv21_to_bgrx32},
2144 {PIX_FMT_NV21, PIX_FMT_RGBA32, nv21_to_rgba32},
2145 {PIX_FMT_NV21, PIX_FMT_BGRA32, nv21_to_bgra32},
2146 {PIX_FMT_NV21, PIX_FMT_ARGB32, nv21_to_argb32},
2147 {PIX_FMT_NV21, PIX_FMT_ABGR32, nv21_to_abgr32},
2148 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv21_to_nv12},
2150 {PIX_FMT_YUV422P, PIX_FMT_YUV422, yuv422p_to_yuv422},
2151 {PIX_FMT_YUV422P, PIX_FMT_UYVY422, yuv422p_to_uyvy422},
2153 {PIX_FMT_YUV444P, PIX_FMT_RGB24, yuv444p_to_rgb24},
2155 {PIX_FMT_YUVJ420P, PIX_FMT_RGB555, yuvj420p_to_rgb555},
2156 {PIX_FMT_YUVJ420P, PIX_FMT_RGB565, yuvj420p_to_rgb565},
2157 {PIX_FMT_YUVJ420P, PIX_FMT_BGR24, yuvj420p_to_bgr24},
2158 {PIX_FMT_YUVJ420P, PIX_FMT_RGB24, yuvj420p_to_rgb24},
2159 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_rgb32},
2160 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgr32},
2161 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_xrgb32},
2162 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgrx32},
2163 {PIX_FMT_YUVJ420P, PIX_FMT_RGBA32, yuvj420p_to_rgba32},
2164 {PIX_FMT_YUVJ420P, PIX_FMT_BGRA32, yuvj420p_to_bgra32},
2165 {PIX_FMT_YUVJ420P, PIX_FMT_ARGB32, yuvj420p_to_argb32},
2166 {PIX_FMT_YUVJ420P, PIX_FMT_ABGR32, yuvj420p_to_abgr32},
2168 {PIX_FMT_YUVJ444P, PIX_FMT_RGB24, yuvj444p_to_rgb24},
2170 {PIX_FMT_YUV422, PIX_FMT_YUV420P, yuv422_to_yuv420p},
2171 {PIX_FMT_YUV422, PIX_FMT_YUV422P, yuv422_to_yuv422p},
2173 {PIX_FMT_UYVY422, PIX_FMT_YUV420P, uyvy422_to_yuv420p},
2174 {PIX_FMT_UYVY422, PIX_FMT_YUV422P, uyvy422_to_yuv422p},
2176 {PIX_FMT_RGB24, PIX_FMT_YUV420P, rgb24_to_yuv420p},
2177 {PIX_FMT_RGB24, PIX_FMT_NV12, rgb24_to_nv12},
2178 {PIX_FMT_RGB24, PIX_FMT_NV21, rgb24_to_nv21},
2179 {PIX_FMT_RGB24, PIX_FMT_RGB565, rgb24_to_rgb565},
2180 {PIX_FMT_RGB24, PIX_FMT_RGB555, rgb24_to_rgb555},
2181 {PIX_FMT_RGB24, PIX_FMT_RGB32, rgb24_to_rgb32},
2182 {PIX_FMT_RGB24, PIX_FMT_BGR32, rgb24_to_bgr32},
2183 {PIX_FMT_RGB24, PIX_FMT_xRGB32, rgb24_to_xrgb32},
2184 {PIX_FMT_RGB24, PIX_FMT_BGRx32, rgb24_to_bgrx32},
2185 {PIX_FMT_RGB24, PIX_FMT_RGBA32, rgb24_to_rgba32},
2186 {PIX_FMT_RGB24, PIX_FMT_BGR24, rgb24_to_bgr24},
2187 {PIX_FMT_RGB24, PIX_FMT_BGRA32, rgb24_to_bgra32},
2188 {PIX_FMT_RGB24, PIX_FMT_ARGB32, rgb24_to_argb32},
2189 {PIX_FMT_RGB24, PIX_FMT_ABGR32, rgb24_to_abgr32},
2190 {PIX_FMT_RGB24, PIX_FMT_GRAY8, rgb24_to_gray},
2191 {PIX_FMT_RGB24, PIX_FMT_PAL8, rgb24_to_pal8},
2192 {PIX_FMT_RGB24, PIX_FMT_YUV444P, rgb24_to_yuv444p},
2193 {PIX_FMT_RGB24, PIX_FMT_YUVJ420P, rgb24_to_yuvj420p},
2194 {PIX_FMT_RGB24, PIX_FMT_YUVJ444P, rgb24_to_yuvj444p},
2195 {PIX_FMT_RGB24, PIX_FMT_AYUV4444, rgb24_to_ayuv4444},
2196 {PIX_FMT_RGB24, PIX_FMT_V308, rgb24_to_v308},
2198 {PIX_FMT_RGB32, PIX_FMT_RGB24, rgb32_to_rgb24},
2199 {PIX_FMT_RGB32, PIX_FMT_RGB555, rgba32_to_rgb555},
2200 {PIX_FMT_RGB32, PIX_FMT_PAL8, rgb32_to_pal8},
2201 {PIX_FMT_RGB32, PIX_FMT_YUV420P, rgb32_to_yuv420p},
2202 {PIX_FMT_RGB32, PIX_FMT_NV12, rgb32_to_nv12},
2203 {PIX_FMT_RGB32, PIX_FMT_NV21, rgb32_to_nv21},
2204 {PIX_FMT_RGB32, PIX_FMT_GRAY8, rgb32_to_gray},
2206 {PIX_FMT_xRGB32, PIX_FMT_RGB24, xrgb32_to_rgb24},
2207 {PIX_FMT_xRGB32, PIX_FMT_PAL8, xrgb32_to_pal8},
2208 {PIX_FMT_xRGB32, PIX_FMT_YUV420P, xrgb32_to_yuv420p},
2209 {PIX_FMT_xRGB32, PIX_FMT_NV12, xrgb32_to_nv12},
2210 {PIX_FMT_xRGB32, PIX_FMT_NV21, xrgb32_to_nv21},
2211 {PIX_FMT_xRGB32, PIX_FMT_GRAY8, xrgb32_to_gray},
2213 {PIX_FMT_RGBA32, PIX_FMT_BGRA32, rgba32_to_bgra32},
2214 {PIX_FMT_RGBA32, PIX_FMT_ABGR32, rgba32_to_abgr32},
2215 {PIX_FMT_RGBA32, PIX_FMT_ARGB32, rgba32_to_argb32},
2216 {PIX_FMT_RGBA32, PIX_FMT_BGR32, rgba32_to_bgr32},
2217 {PIX_FMT_RGBA32, PIX_FMT_BGRx32, rgba32_to_bgrx32},
2218 {PIX_FMT_RGBA32, PIX_FMT_ABGR32, rgba32_to_abgr32},
2219 {PIX_FMT_RGBA32, PIX_FMT_RGB24, rgba32_to_rgb24},
2220 {PIX_FMT_RGBA32, PIX_FMT_RGB555, rgba32_to_rgb555},
2221 {PIX_FMT_RGBA32, PIX_FMT_PAL8, rgba32_to_pal8},
2222 {PIX_FMT_RGBA32, PIX_FMT_YUV420P, rgba32_to_yuv420p},
2223 {PIX_FMT_RGBA32, PIX_FMT_NV12, rgba32_to_nv12},
2224 {PIX_FMT_RGBA32, PIX_FMT_NV21, rgba32_to_nv21},
2225 {PIX_FMT_RGBA32, PIX_FMT_GRAY8, rgba32_to_gray},
2226 {PIX_FMT_RGBA32, PIX_FMT_AYUV4444, rgba32_to_ayuv4444},
2228 {PIX_FMT_BGR24, PIX_FMT_RGB24, bgr24_to_rgb24},
2229 {PIX_FMT_BGR24, PIX_FMT_YUV420P, bgr24_to_yuv420p},
2230 {PIX_FMT_BGR24, PIX_FMT_NV12, bgr24_to_nv12},
2231 {PIX_FMT_BGR24, PIX_FMT_NV21, bgr24_to_nv21},
2232 {PIX_FMT_BGR24, PIX_FMT_GRAY8, bgr24_to_gray},
2234 {PIX_FMT_BGR32, PIX_FMT_RGB24, bgr32_to_rgb24},
2235 {PIX_FMT_BGR32, PIX_FMT_RGBA32, bgr32_to_rgba32},
2236 {PIX_FMT_BGR32, PIX_FMT_YUV420P, bgr32_to_yuv420p},
2237 {PIX_FMT_BGR32, PIX_FMT_NV12, bgr32_to_nv12},
2238 {PIX_FMT_BGR32, PIX_FMT_NV21, bgr32_to_nv21},
2239 {PIX_FMT_BGR32, PIX_FMT_GRAY8, bgr32_to_gray},
2241 {PIX_FMT_BGRx32, PIX_FMT_RGB24, bgrx32_to_rgb24},
2242 {PIX_FMT_BGRx32, PIX_FMT_RGBA32, bgrx32_to_rgba32},
2243 {PIX_FMT_BGRx32, PIX_FMT_YUV420P, bgrx32_to_yuv420p},
2244 {PIX_FMT_BGRx32, PIX_FMT_NV12, bgrx32_to_nv12},
2245 {PIX_FMT_BGRx32, PIX_FMT_NV21, bgrx32_to_nv21},
2246 {PIX_FMT_BGRx32, PIX_FMT_GRAY8, bgrx32_to_gray},
2248 {PIX_FMT_BGRA32, PIX_FMT_RGB24, bgra32_to_rgb24},
2249 {PIX_FMT_BGRA32, PIX_FMT_RGBA32, bgra32_to_rgba32},
2250 {PIX_FMT_BGRA32, PIX_FMT_YUV420P, bgra32_to_yuv420p},
2251 {PIX_FMT_BGRA32, PIX_FMT_NV12, bgra32_to_nv12},
2252 {PIX_FMT_BGRA32, PIX_FMT_NV21, bgra32_to_nv21},
2253 {PIX_FMT_BGRA32, PIX_FMT_GRAY8, bgra32_to_gray},
2254 {PIX_FMT_BGRA32, PIX_FMT_AYUV4444, bgra32_to_ayuv4444},
2256 {PIX_FMT_ABGR32, PIX_FMT_RGB24, abgr32_to_rgb24},
2257 {PIX_FMT_ABGR32, PIX_FMT_RGBA32, abgr32_to_rgba32},
2258 {PIX_FMT_ABGR32, PIX_FMT_YUV420P, abgr32_to_yuv420p},
2259 {PIX_FMT_ABGR32, PIX_FMT_NV12, abgr32_to_nv12},
2260 {PIX_FMT_ABGR32, PIX_FMT_NV21, abgr32_to_nv21},
2261 {PIX_FMT_ABGR32, PIX_FMT_GRAY8, abgr32_to_gray},
2263 {PIX_FMT_ARGB32, PIX_FMT_RGB24, argb32_to_rgb24},
2264 {PIX_FMT_ARGB32, PIX_FMT_RGBA32, argb32_to_rgba32},
2265 {PIX_FMT_ARGB32, PIX_FMT_YUV420P, argb32_to_yuv420p},
2266 {PIX_FMT_ARGB32, PIX_FMT_NV12, argb32_to_nv12},
2267 {PIX_FMT_ARGB32, PIX_FMT_NV21, argb32_to_nv21},
2268 {PIX_FMT_ARGB32, PIX_FMT_GRAY8, argb32_to_gray},
2270 {PIX_FMT_RGB555, PIX_FMT_RGB24, rgb555_to_rgb24},
2271 {PIX_FMT_RGB555, PIX_FMT_RGB32, rgb555_to_rgba32},
2272 {PIX_FMT_RGB555, PIX_FMT_RGBA32, rgb555_to_rgba32},
2273 {PIX_FMT_RGB555, PIX_FMT_YUV420P, rgb555_to_yuv420p},
2274 {PIX_FMT_RGB555, PIX_FMT_NV12, rgb555_to_nv12},
2275 {PIX_FMT_RGB555, PIX_FMT_NV21, rgb555_to_nv21},
2276 {PIX_FMT_RGB555, PIX_FMT_GRAY8, rgb555_to_gray},
2278 {PIX_FMT_RGB565, PIX_FMT_RGB24, rgb565_to_rgb24},
2279 {PIX_FMT_RGB565, PIX_FMT_YUV420P, rgb565_to_yuv420p},
2280 {PIX_FMT_RGB565, PIX_FMT_NV12, rgb565_to_nv12},
2281 {PIX_FMT_RGB565, PIX_FMT_NV21, rgb565_to_nv21},
2282 {PIX_FMT_RGB565, PIX_FMT_GRAY8, rgb565_to_gray},
2284 {PIX_FMT_GRAY8, PIX_FMT_RGB555, gray_to_rgb555},
2285 {PIX_FMT_GRAY8, PIX_FMT_RGB565, gray_to_rgb565},
2286 {PIX_FMT_GRAY8, PIX_FMT_RGB24, gray_to_rgb24},
2287 {PIX_FMT_GRAY8, PIX_FMT_BGR24, gray_to_bgr24},
2288 {PIX_FMT_GRAY8, PIX_FMT_RGB32, gray_to_rgb32},
2289 {PIX_FMT_GRAY8, PIX_FMT_BGR32, gray_to_bgr32},
2290 {PIX_FMT_GRAY8, PIX_FMT_xRGB32, gray_to_xrgb32},
2291 {PIX_FMT_GRAY8, PIX_FMT_BGRx32, gray_to_bgrx32},
2292 {PIX_FMT_GRAY8, PIX_FMT_RGBA32, gray_to_rgba32},
2293 {PIX_FMT_GRAY8, PIX_FMT_BGRA32, gray_to_bgra32},
2294 {PIX_FMT_GRAY8, PIX_FMT_ARGB32, gray_to_argb32},
2295 {PIX_FMT_GRAY8, PIX_FMT_ABGR32, gray_to_abgr32},
2296 {PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, gray_to_monowhite},
2297 {PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, gray_to_monoblack},
2299 {PIX_FMT_MONOWHITE, PIX_FMT_GRAY8, monowhite_to_gray},
2301 {PIX_FMT_MONOBLACK, PIX_FMT_GRAY8, monoblack_to_gray},
2303 {PIX_FMT_PAL8, PIX_FMT_RGB555, pal8_to_rgb555},
2304 {PIX_FMT_PAL8, PIX_FMT_RGB565, pal8_to_rgb565},
2305 {PIX_FMT_PAL8, PIX_FMT_BGR24, pal8_to_bgr24},
2306 {PIX_FMT_PAL8, PIX_FMT_RGB24, pal8_to_rgb24},
2307 {PIX_FMT_PAL8, PIX_FMT_RGB32, pal8_to_rgb32},
2308 {PIX_FMT_PAL8, PIX_FMT_BGR32, pal8_to_bgr32},
2309 {PIX_FMT_PAL8, PIX_FMT_xRGB32, pal8_to_xrgb32},
2310 {PIX_FMT_PAL8, PIX_FMT_BGRx32, pal8_to_bgrx32},
2311 {PIX_FMT_PAL8, PIX_FMT_RGBA32, pal8_to_rgba32},
2312 {PIX_FMT_PAL8, PIX_FMT_BGRA32, pal8_to_bgra32},
2313 {PIX_FMT_PAL8, PIX_FMT_ARGB32, pal8_to_argb32},
2314 {PIX_FMT_PAL8, PIX_FMT_ABGR32, pal8_to_abgr32},
2316 {PIX_FMT_UYVY411, PIX_FMT_YUV411P, uyvy411_to_yuv411p},
2318 {PIX_FMT_V308, PIX_FMT_RGB24, v308_to_rgb24},
2320 {PIX_FMT_AYUV4444, PIX_FMT_RGBA32, ayuv4444_to_rgba32},
2321 {PIX_FMT_AYUV4444, PIX_FMT_RGB24, ayuv4444_to_rgb24},
2324 static ConvertEntry *
2325 get_convert_table_entry (int src_pix_fmt, int dst_pix_fmt)
2329 for (i = 0; i < sizeof (convert_table) / sizeof (convert_table[0]); i++) {
2330 if (convert_table[i].src == src_pix_fmt &&
2331 convert_table[i].dest == dst_pix_fmt) {
2332 return convert_table + i;
2340 avpicture_alloc (AVPicture * picture, int pix_fmt, int width, int height)
2345 size = avpicture_get_size (pix_fmt, width, height);
2346 ptr = av_malloc (size);
2349 gst_ffmpegcsp_avpicture_fill (picture, ptr, pix_fmt, width, height);
2352 memset (picture, 0, sizeof (AVPicture));
2357 avpicture_free (AVPicture * picture)
2359 av_free (picture->data[0]);
2362 /* return true if yuv planar */
2364 is_yuv_planar (PixFmtInfo * ps)
2366 return (ps->color_type == FF_COLOR_YUV ||
2367 ps->color_type == FF_COLOR_YUV_JPEG) && ps->pixel_type == FF_PIXEL_PLANAR;
2370 /* XXX: always use linesize. Return -1 if not supported */
2372 img_convert (AVPicture * dst, int dst_pix_fmt,
2373 const AVPicture * src, int src_pix_fmt, int src_width, int src_height)
2376 int i, ret, dst_width, dst_height, int_pix_fmt;
2377 PixFmtInfo *src_pix, *dst_pix;
2379 AVPicture tmp1, *tmp = &tmp1;
2381 if (G_UNLIKELY (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2382 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB))
2384 if (G_UNLIKELY (src_width <= 0 || src_height <= 0))
2387 if (G_UNLIKELY (!inited)) {
2389 img_convert_init ();
2392 dst_width = src_width;
2393 dst_height = src_height;
2395 dst_pix = get_pix_fmt_info (dst_pix_fmt);
2396 src_pix = get_pix_fmt_info (src_pix_fmt);
2397 if (G_UNLIKELY (src_pix_fmt == dst_pix_fmt)) {
2398 /* no conversion needed: just copy */
2399 img_copy (dst, src, dst_pix_fmt, dst_width, dst_height);
2403 ce = get_convert_table_entry (src_pix_fmt, dst_pix_fmt);
2404 if (ce && ce->convert) {
2405 /* specific conversion routine */
2406 ce->convert (dst, src, dst_width, dst_height);
2411 if (is_yuv_planar (dst_pix) && src_pix_fmt == PIX_FMT_GRAY8) {
2415 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2416 img_copy_plane (dst->data[0], dst->linesize[0],
2417 src->data[0], src->linesize[0], dst_width, dst_height);
2419 img_apply_table (dst->data[0], dst->linesize[0],
2420 src->data[0], src->linesize[0],
2421 dst_width, dst_height, y_jpeg_to_ccir);
2423 /* fill U and V with 128 */
2426 w >>= dst_pix->x_chroma_shift;
2427 h >>= dst_pix->y_chroma_shift;
2428 for (i = 1; i <= 2; i++) {
2430 for (y = 0; y < h; y++) {
2432 d += dst->linesize[i];
2439 if (is_yuv_planar (src_pix) && dst_pix_fmt == PIX_FMT_GRAY8) {
2440 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2441 img_copy_plane (dst->data[0], dst->linesize[0],
2442 src->data[0], src->linesize[0], dst_width, dst_height);
2444 img_apply_table (dst->data[0], dst->linesize[0],
2445 src->data[0], src->linesize[0],
2446 dst_width, dst_height, y_ccir_to_jpeg);
2451 /* YUV to YUV planar */
2452 if (is_yuv_planar (dst_pix) && is_yuv_planar (src_pix)) {
2453 int x_shift, y_shift, w, h, xy_shift;
2454 void (*resize_func) (uint8_t * dst, int dst_wrap,
2455 const uint8_t * src, int src_wrap, int width, int height);
2457 /* compute chroma size of the smallest dimensions */
2460 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2461 w >>= dst_pix->x_chroma_shift;
2463 w >>= src_pix->x_chroma_shift;
2464 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2465 h >>= dst_pix->y_chroma_shift;
2467 h >>= src_pix->y_chroma_shift;
2469 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2470 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2471 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2473 /* there must be filters for conversion at least from and to
2477 resize_func = img_copy_plane;
2480 resize_func = shrink21;
2483 resize_func = shrink41;
2486 resize_func = shrink12;
2489 resize_func = shrink22;
2492 resize_func = shrink44;
2495 resize_func = grow21;
2498 resize_func = grow41;
2501 resize_func = grow22;
2504 resize_func = grow44;
2507 resize_func = conv411;
2510 /* currently not handled */
2511 goto no_chroma_filter;
2514 img_copy_plane (dst->data[0], dst->linesize[0],
2515 src->data[0], src->linesize[0], dst_width, dst_height);
2517 #define GEN_MASK(x) ((1<<(x))-1)
2518 #define DIV_ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) >> (x))
2520 for (i = 1; i <= 2; i++) {
2523 w = DIV_ROUND_UP_X (dst_width, dst_pix->x_chroma_shift);
2524 h = DIV_ROUND_UP_X (dst_height, dst_pix->y_chroma_shift);
2526 resize_func (dst->data[i], dst->linesize[i],
2527 src->data[i], src->linesize[i], w, h);
2529 /* if yuv color space conversion is needed, we do it here on
2530 the destination image */
2531 if (dst_pix->color_type != src_pix->color_type) {
2532 const uint8_t *y_table, *c_table;
2534 if (dst_pix->color_type == FF_COLOR_YUV) {
2535 y_table = y_jpeg_to_ccir;
2536 c_table = c_jpeg_to_ccir;
2538 y_table = y_ccir_to_jpeg;
2539 c_table = c_ccir_to_jpeg;
2541 img_apply_table (dst->data[0], dst->linesize[0],
2542 dst->data[0], dst->linesize[0], dst_width, dst_height, y_table);
2544 for (i = 1; i <= 2; i++)
2545 img_apply_table (dst->data[i], dst->linesize[i],
2546 dst->data[i], dst->linesize[i],
2547 dst_width >> dst_pix->x_chroma_shift,
2548 dst_height >> dst_pix->y_chroma_shift, c_table);
2554 /* try to use an intermediate format */
2555 if (src_pix_fmt == PIX_FMT_YUV422 || dst_pix_fmt == PIX_FMT_YUV422) {
2556 /* specific case: convert to YUV422P first */
2557 int_pix_fmt = PIX_FMT_YUV422P;
2558 } else if (src_pix_fmt == PIX_FMT_UYVY422 || dst_pix_fmt == PIX_FMT_UYVY422) {
2559 /* specific case: convert to YUV422P first */
2560 int_pix_fmt = PIX_FMT_YUV422P;
2561 } else if (src_pix_fmt == PIX_FMT_UYVY411 || dst_pix_fmt == PIX_FMT_UYVY411) {
2562 /* specific case: convert to YUV411P first */
2563 int_pix_fmt = PIX_FMT_YUV411P;
2564 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2565 src_pix_fmt != PIX_FMT_GRAY8) ||
2566 (dst_pix->color_type == FF_COLOR_GRAY && dst_pix_fmt != PIX_FMT_GRAY8)) {
2567 /* gray8 is the normalized format */
2568 int_pix_fmt = PIX_FMT_GRAY8;
2569 } else if ((is_yuv_planar (src_pix) &&
2570 src_pix_fmt != PIX_FMT_YUV444P && src_pix_fmt != PIX_FMT_YUVJ444P)) {
2571 /* yuv444 is the normalized format */
2572 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2573 int_pix_fmt = PIX_FMT_YUVJ444P;
2575 int_pix_fmt = PIX_FMT_YUV444P;
2576 } else if ((is_yuv_planar (dst_pix) &&
2577 dst_pix_fmt != PIX_FMT_YUV444P && dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2578 /* yuv444 is the normalized format */
2579 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2580 int_pix_fmt = PIX_FMT_YUVJ444P;
2582 int_pix_fmt = PIX_FMT_YUV444P;
2584 /* the two formats are rgb or gray8 or yuv[j]444p */
2585 if (src_pix->is_alpha && dst_pix->is_alpha)
2586 int_pix_fmt = PIX_FMT_RGBA32;
2588 int_pix_fmt = PIX_FMT_RGB24;
2590 if (avpicture_alloc (tmp, int_pix_fmt, dst_width, dst_height) < 0)
2593 if (img_convert (tmp, int_pix_fmt,
2594 src, src_pix_fmt, src_width, src_height) < 0)
2597 if (img_convert (dst, dst_pix_fmt,
2598 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2602 avpicture_free (tmp);
2606 /* NOTE: we scan all the pixels to have an exact information */
2608 get_alpha_info_pal8 (const AVPicture * src, int width, int height)
2610 const unsigned char *p;
2611 int src_wrap, ret, x, y;
2613 uint32_t *palette = (uint32_t *) src->data[1];
2616 src_wrap = src->linesize[0] - width;
2618 for (y = 0; y < height; y++) {
2619 for (x = 0; x < width; x++) {
2620 a = palette[p[0]] >> 24;
2622 ret |= FF_ALPHA_TRANSP;
2623 } else if (a != 0xff) {
2624 ret |= FF_ALPHA_SEMI_TRANSP;
2634 * Tell if an image really has transparent alpha values.
2635 * @return ored mask of FF_ALPHA_xxx constants
2638 img_get_alpha_info (const AVPicture * src, int pix_fmt, int width, int height)
2640 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
2643 pf = get_pix_fmt_info (pix_fmt);
2644 /* no alpha can be represented in format */
2649 ret = get_alpha_info_rgb32 (src, width, height);
2652 ret = get_alpha_info_bgr32 (src, width, height);
2654 case PIX_FMT_xRGB32:
2655 ret = get_alpha_info_xrgb32 (src, width, height);
2657 case PIX_FMT_BGRx32:
2658 ret = get_alpha_info_bgrx32 (src, width, height);
2660 case PIX_FMT_RGBA32:
2661 ret = get_alpha_info_rgba32 (src, width, height);
2663 case PIX_FMT_BGRA32:
2664 ret = get_alpha_info_bgra32 (src, width, height);
2666 case PIX_FMT_ARGB32:
2667 ret = get_alpha_info_argb32 (src, width, height);
2669 case PIX_FMT_ABGR32:
2670 ret = get_alpha_info_abgr32 (src, width, height);
2672 case PIX_FMT_RGB555:
2673 ret = get_alpha_info_rgb555 (src, width, height);
2676 ret = get_alpha_info_pal8 (src, width, height);
2679 /* we do not know, so everything is indicated */
2680 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2687 #define DEINT_INPLACE_LINE_LUM \
2688 movd_m2r(lum_m4[0],mm0);\
2689 movd_m2r(lum_m3[0],mm1);\
2690 movd_m2r(lum_m2[0],mm2);\
2691 movd_m2r(lum_m1[0],mm3);\
2692 movd_m2r(lum[0],mm4);\
2693 punpcklbw_r2r(mm7,mm0);\
2694 movd_r2m(mm2,lum_m4[0]);\
2695 punpcklbw_r2r(mm7,mm1);\
2696 punpcklbw_r2r(mm7,mm2);\
2697 punpcklbw_r2r(mm7,mm3);\
2698 punpcklbw_r2r(mm7,mm4);\
2699 paddw_r2r(mm3,mm1);\
2701 paddw_r2r(mm4,mm0);\
2703 paddw_r2r(mm6,mm2);\
2704 paddw_r2r(mm2,mm1);\
2705 psubusw_r2r(mm0,mm1);\
2707 packuswb_r2r(mm7,mm1);\
2708 movd_r2m(mm1,lum_m2[0]);
2710 #define DEINT_LINE_LUM \
2711 movd_m2r(lum_m4[0],mm0);\
2712 movd_m2r(lum_m3[0],mm1);\
2713 movd_m2r(lum_m2[0],mm2);\
2714 movd_m2r(lum_m1[0],mm3);\
2715 movd_m2r(lum[0],mm4);\
2716 punpcklbw_r2r(mm7,mm0);\
2717 punpcklbw_r2r(mm7,mm1);\
2718 punpcklbw_r2r(mm7,mm2);\
2719 punpcklbw_r2r(mm7,mm3);\
2720 punpcklbw_r2r(mm7,mm4);\
2721 paddw_r2r(mm3,mm1);\
2723 paddw_r2r(mm4,mm0);\
2725 paddw_r2r(mm6,mm2);\
2726 paddw_r2r(mm2,mm1);\
2727 psubusw_r2r(mm0,mm1);\
2729 packuswb_r2r(mm7,mm1);\
2730 movd_r2m(mm1,dst[0]);
2733 /* filter parameters: [-1 4 2 4 -1] // 8 */
2736 deinterlace_line (uint8_t * dst,
2737 const uint8_t * lum_m4, const uint8_t * lum_m3,
2738 const uint8_t * lum_m2, const uint8_t * lum_m1,
2739 const uint8_t * lum, int size)
2742 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2745 for (; size > 0; size--) {
2747 sum += lum_m3[0] << 2;
2748 sum += lum_m2[0] << 1;
2749 sum += lum_m1[0] << 2;
2751 dst[0] = cm[(sum + 4) >> 3];
2768 pxor_r2r (mm7, mm7);
2769 movq_m2r (rounder, mm6);
2771 for (; size > 3; size -= 4) {
2772 DEINT_LINE_LUM lum_m4 += 4;
2784 deinterlace_line_inplace (uint8_t * lum_m4, uint8_t * lum_m3, uint8_t * lum_m2,
2785 uint8_t * lum_m1, uint8_t * lum, int size)
2788 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2791 for (; size > 0; size--) {
2793 sum += lum_m3[0] << 2;
2794 sum += lum_m2[0] << 1;
2795 lum_m4[0] = lum_m2[0];
2796 sum += lum_m1[0] << 2;
2798 lum_m2[0] = cm[(sum + 4) >> 3];
2814 pxor_r2r (mm7, mm7);
2815 movq_m2r (rounder, mm6);
2817 for (; size > 3; size -= 4) {
2818 DEINT_INPLACE_LINE_LUM lum_m4 += 4;
2829 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2830 top field is copied as is, but the bottom field is deinterlaced
2831 against the top field. */
2834 deinterlace_bottom_field (uint8_t * dst, int dst_wrap,
2835 const uint8_t * src1, int src_wrap, int width, int height)
2837 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2842 src_0 = &src_m1[src_wrap];
2843 src_p1 = &src_0[src_wrap];
2844 src_p2 = &src_p1[src_wrap];
2845 for (y = 0; y < (height - 2); y += 2) {
2846 memcpy (dst, src_m1, width);
2848 deinterlace_line (dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
2852 src_p1 += 2 * src_wrap;
2853 src_p2 += 2 * src_wrap;
2856 memcpy (dst, src_m1, width);
2859 deinterlace_line (dst, src_m2, src_m1, src_0, src_0, src_0, width);
2863 deinterlace_bottom_field_inplace (uint8_t * src1, int src_wrap,
2864 int width, int height)
2866 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2870 buf = (uint8_t *) av_malloc (width);
2873 memcpy (buf, src_m1, width);
2874 src_0 = &src_m1[src_wrap];
2875 src_p1 = &src_0[src_wrap];
2876 src_p2 = &src_p1[src_wrap];
2877 for (y = 0; y < (height - 2); y += 2) {
2878 deinterlace_line_inplace (buf, src_m1, src_0, src_p1, src_p2, width);
2881 src_p1 += 2 * src_wrap;
2882 src_p2 += 2 * src_wrap;
2885 deinterlace_line_inplace (buf, src_m1, src_0, src_0, src_0, width);
2890 /* deinterlace - if not supported return -1 */
2893 avpicture_deinterlace (AVPicture * dst, const AVPicture * src,
2894 int pix_fmt, int width, int height)
2898 if (pix_fmt != PIX_FMT_YUV420P &&
2899 pix_fmt != PIX_FMT_YUV422P &&
2900 pix_fmt != PIX_FMT_YUV444P && pix_fmt != PIX_FMT_YUV411P)
2902 if ((width & 3) != 0 || (height & 3) != 0)
2905 for (i = 0; i < 3; i++) {
2908 case PIX_FMT_YUV420P:
2912 case PIX_FMT_YUV422P:
2915 case PIX_FMT_YUV411P:
2923 deinterlace_bottom_field_inplace (dst->data[i], dst->linesize[i],
2926 deinterlace_bottom_field (dst->data[i], dst->linesize[i],
2927 src->data[i], src->linesize[i], width, height);