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_YUV422P] = */ {
68 /* .format = */ PIX_FMT_YUV422P,
69 /* .name = */ "yuv422p",
70 /* .nb_channels = */ 3,
71 /* .color_type = */ FF_COLOR_YUV,
72 /* .pixel_type = */ FF_PIXEL_PLANAR,
74 /* .x_chroma_shift = */ 1,
75 /* .y_chroma_shift = */ 0,
78 /* [PIX_FMT_YUV444P] = */ {
79 /* .format = */ PIX_FMT_YUV444P,
80 /* .name = */ "yuv444p",
81 /* .nb_channels = */ 3,
82 /* .color_type = */ FF_COLOR_YUV,
83 /* .pixel_type = */ FF_PIXEL_PLANAR,
85 /* .x_chroma_shift = */ 0,
86 /* .y_chroma_shift = */ 0,
89 /* [PIX_FMT_YUV422] = */ {
90 /* .format = */ PIX_FMT_YUV422,
91 /* .name = */ "yuv422",
92 /* .nb_channels = */ 1,
93 /* .color_type = */ FF_COLOR_YUV,
94 /* .pixel_type = */ FF_PIXEL_PACKED,
96 /* .x_chroma_shift = */ 1,
97 /* .y_chroma_shift = */ 0,
100 /* [PIX_FMT_UYVY422] = */ {
101 /* .format = */ PIX_FMT_UYVY422,
102 /* .name = */ "uyvy422",
103 /* .nb_channels = */ 1,
104 /* .color_type = */ FF_COLOR_YUV,
105 /* .pixel_type = */ FF_PIXEL_PACKED,
107 /* .x_chroma_shift = */ 1,
108 /* .y_chroma_shift = */ 0,
111 /* [PIX_FMT_YUV410P] = */ {
112 /* .format = */ PIX_FMT_YUV410P,
113 /* .name = */ "yuv410p",
114 /* .nb_channels = */ 3,
115 /* .color_type = */ FF_COLOR_YUV,
116 /* .pixel_type = */ FF_PIXEL_PLANAR,
118 /* .x_chroma_shift = */ 2,
119 /* .y_chroma_shift = */ 2,
122 /* [PIX_FMT_YUV411P] = */ {
123 /* .format = */ PIX_FMT_YUV411P,
124 /* .name = */ "yuv411p",
125 /* .nb_channels = */ 3,
126 /* .color_type = */ FF_COLOR_YUV,
127 /* .pixel_type = */ FF_PIXEL_PLANAR,
129 /* .x_chroma_shift = */ 2,
130 /* .y_chroma_shift = */ 0,
135 /* [PIX_FMT_YUVJ420P] = */ {
136 /* .format = */ PIX_FMT_YUVJ420P,
137 /* .name = */ "yuvj420p",
138 /* .nb_channels = */ 3,
139 /* .color_type = */ FF_COLOR_YUV_JPEG,
140 /* .pixel_type = */ FF_PIXEL_PLANAR,
142 /* .x_chroma_shift = */ 1,
143 /* .y_chroma_shift = */ 1,
146 /* [PIX_FMT_YUVJ422P] = */ {
147 /* .format = */ PIX_FMT_YUVJ422P,
148 /* .name = */ "yuvj422p",
149 /* .nb_channels = */ 3,
150 /* .color_type = */ FF_COLOR_YUV_JPEG,
151 /* .pixel_type = */ FF_PIXEL_PLANAR,
153 /* .x_chroma_shift = */ 1,
154 /* .y_chroma_shift = */ 0,
157 /* [PIX_FMT_YUVJ444P] = */ {
158 /* .format = */ PIX_FMT_YUVJ444P,
159 /* .name = */ "yuvj444p",
160 /* .nb_channels = */ 3,
161 /* .color_type = */ FF_COLOR_YUV_JPEG,
162 /* .pixel_type = */ FF_PIXEL_PLANAR,
164 /* .x_chroma_shift = */ 0,
165 /* .y_chroma_shift = */ 0,
170 /* [PIX_FMT_RGB24] = */ {
171 /* .format = */ PIX_FMT_RGB24,
172 /* .name = */ "rgb24",
173 /* .nb_channels = */ 3,
174 /* .color_type = */ FF_COLOR_RGB,
175 /* .pixel_type = */ FF_PIXEL_PACKED,
177 /* .x_chroma_shift = */ 0,
178 /* .y_chroma_shift = */ 0,
181 /* [PIX_FMT_BGR24] = */ {
182 /* .format = */ PIX_FMT_BGR24,
183 /* .name = */ "bgr24",
184 /* .nb_channels = */ 3,
185 /* .color_type = */ FF_COLOR_RGB,
186 /* .pixel_type = */ FF_PIXEL_PACKED,
188 /* .x_chroma_shift = */ 0,
189 /* .y_chroma_shift = */ 0,
192 /* [PIX_FMT_RGB32] = */ {
193 /* .format = */ PIX_FMT_RGB32,
194 /* .name = */ "rgb32",
195 /* .nb_channels = */ 4,
196 /* .color_type = */ FF_COLOR_RGB,
197 /* .pixel_type = */ FF_PIXEL_PACKED,
199 /* .x_chroma_shift = */ 0,
200 /* .y_chroma_shift = */ 0,
203 /* [PIX_FMT_BGR32] = */ {
204 /* .format = */ PIX_FMT_BGR32,
205 /* .name = */ "bgr32",
206 /* .nb_channels = */ 4,
207 /* .color_type = */ FF_COLOR_RGB,
208 /* .pixel_type = */ FF_PIXEL_PACKED,
210 /* .x_chroma_shift = */ 0,
211 /* .y_chroma_shift = */ 0,
214 /* [PIX_FMT_RGBA32] = */ {
215 /* .format = */ PIX_FMT_RGBA32,
216 /* .name = */ "rgba32",
217 /* .nb_channels = */ 4,
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_BGRA32] = */ {
226 /* .format = */ PIX_FMT_BGRA32,
227 /* .name = */ "bgra32",
228 /* .nb_channels = */ 4,
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_RGB565] = */ {
237 /* .format = */ PIX_FMT_RGB565,
238 /* .name = */ "rgb565",
239 /* .nb_channels = */ 3,
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_RGB555] = */ {
248 /* .format = */ PIX_FMT_RGB555,
249 /* .name = */ "rgb555",
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,
259 /* gray / mono formats */
260 /* [PIX_FMT_GRAY8] = */ {
261 /* .format = */ PIX_FMT_GRAY8,
262 /* .name = */ "gray",
263 /* .nb_channels = */ 1,
264 /* .color_type = */ FF_COLOR_GRAY,
265 /* .pixel_type = */ FF_PIXEL_PLANAR,
267 /* .x_chroma_shift = */ 0,
268 /* .y_chroma_shift = */ 0,
271 /* [PIX_FMT_MONOWHITE] = */ {
272 /* .format = */ PIX_FMT_MONOWHITE,
273 /* .name = */ "monow",
274 /* .nb_channels = */ 1,
275 /* .color_type = */ FF_COLOR_GRAY,
276 /* .pixel_type = */ FF_PIXEL_PLANAR,
278 /* .x_chroma_shift = */ 0,
279 /* .y_chroma_shift = */ 0,
282 /* [PIX_FMT_MONOBLACK] = */ {
283 /* .format = */ PIX_FMT_MONOBLACK,
284 /* .name = */ "monob",
285 /* .nb_channels = */ 1,
286 /* .color_type = */ FF_COLOR_GRAY,
287 /* .pixel_type = */ FF_PIXEL_PLANAR,
289 /* .x_chroma_shift = */ 0,
290 /* .y_chroma_shift = */ 0,
294 /* paletted formats */
295 /* [PIX_FMT_PAL8] = */ {
296 /* .format = */ PIX_FMT_PAL8,
297 /* .name = */ "pal8",
298 /* .nb_channels = */ 4,
299 /* .color_type = */ FF_COLOR_RGB,
300 /* .pixel_type = */ FF_PIXEL_PALETTE,
302 /* .x_chroma_shift = */ 0,
303 /* .y_chroma_shift = */ 0,
306 /* [PIX_FMT_XVMC_MPEG2_MC] = */ {
307 /* .format = */ PIX_FMT_XVMC_MPEG2_MC,
308 /* .name = */ "xvmcmc",
310 /* [PIX_FMT_XVMC_MPEG2_IDCT] = */ {
311 /* .format = */ PIX_FMT_XVMC_MPEG2_IDCT,
312 /* .name = */ "xvmcidct",
314 /* [PIX_FMT_UYVY411] = */ {
315 /* .format = */ PIX_FMT_UYVY411,
316 /* .name = */ "uyvy411",
317 /* .nb_channels = */ 1,
318 /* .color_type = */ FF_COLOR_YUV,
319 /* .pixel_type = */ FF_PIXEL_PACKED,
321 /* .x_chroma_shift = */ 2,
322 /* .y_chroma_shift = */ 0,
325 /* [PIX_FMT_AYUV4444] = */ {
326 /* .format = */ PIX_FMT_AYUV4444,
327 /* .name = */ "ayuv4444",
328 /* .nb_channels = */ 1,
329 /* .color_type = */ FF_COLOR_YUV,
330 /* .pixel_type = */ FF_PIXEL_PACKED,
332 /* .x_chroma_shift = */ 0,
333 /* .y_chroma_shift = */ 0,
339 /* returns NULL if not found */
340 /* undid static since this is also used in gstffmpegcodecmap.c */
342 get_pix_fmt_info (enum PixelFormat format)
346 for (i = 0; i < sizeof (pix_fmt_info) / sizeof (pix_fmt_info[0]); i++) {
347 if (pix_fmt_info[i].format == format) {
348 return pix_fmt_info + i;
352 /* since this doesn't get checked *anywhere*, we might as well warn
353 here if we return NULL so you have *some* idea what's going on */
355 ("Could not find info for pixel format %d out of %d known pixel formats. One segfault coming up",
361 avcodec_get_chroma_sub_sample (int pix_fmt, int *h_shift, int *v_shift)
363 *h_shift = get_pix_fmt_info (pix_fmt)->x_chroma_shift;
364 *v_shift = get_pix_fmt_info (pix_fmt)->y_chroma_shift;
368 avcodec_get_pix_fmt_name (int pix_fmt)
370 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
373 return get_pix_fmt_info (pix_fmt)->name;
377 avcodec_get_pix_fmt (const char *name)
381 for (i = 0; i < PIX_FMT_NB; i++)
382 if (!strcmp (pix_fmt_info[i].name, name))
384 return pix_fmt_info[i].format;
388 avpicture_layout (const AVPicture * src, int pix_fmt, int width, int height,
389 unsigned char *dest, int dest_size)
391 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
392 int i, j, w, h, data_planes;
393 const unsigned char *s;
394 int size = avpicture_get_size (pix_fmt, width, height);
396 if (size > dest_size)
399 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
400 if (pix_fmt == PIX_FMT_YUV422 ||
401 pix_fmt == PIX_FMT_UYVY422 ||
402 pix_fmt == PIX_FMT_RGB565 || pix_fmt == PIX_FMT_RGB555)
404 else if (pix_fmt == PIX_FMT_UYVY411)
405 w = width + width / 2;
406 else if (pix_fmt == PIX_FMT_PAL8)
409 w = width * (pf->depth * pf->nb_channels / 8);
414 data_planes = pf->nb_channels;
415 w = (width * pf->depth + 7) / 8;
419 for (i = 0; i < data_planes; i++) {
421 w = width >> pf->x_chroma_shift;
422 h = height >> pf->y_chroma_shift;
425 for (j = 0; j < h; j++) {
428 s += src->linesize[i];
432 if (pf->pixel_type == FF_PIXEL_PALETTE)
433 memcpy ((unsigned char *) (((size_t) dest + 3) & ~3), src->data[1],
440 avpicture_get_size (int pix_fmt, int width, int height)
442 AVPicture dummy_pict;
444 return gst_ffmpegcsp_avpicture_fill (&dummy_pict, NULL, pix_fmt, width,
449 * compute the loss when converting from a pixel format to another
452 avcodec_get_pix_fmt_loss (int dst_pix_fmt, int src_pix_fmt, int has_alpha)
454 const PixFmtInfo *pf, *ps;
457 ps = get_pix_fmt_info (src_pix_fmt);
458 pf = get_pix_fmt_info (dst_pix_fmt);
462 if (pf->depth < ps->depth ||
463 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
464 loss |= FF_LOSS_DEPTH;
465 if (pf->x_chroma_shift > ps->x_chroma_shift ||
466 pf->y_chroma_shift > ps->y_chroma_shift)
467 loss |= FF_LOSS_RESOLUTION;
468 switch (pf->color_type) {
470 if (ps->color_type != FF_COLOR_RGB && ps->color_type != FF_COLOR_GRAY)
471 loss |= FF_LOSS_COLORSPACE;
474 if (ps->color_type != FF_COLOR_GRAY)
475 loss |= FF_LOSS_COLORSPACE;
478 if (ps->color_type != FF_COLOR_YUV)
479 loss |= FF_LOSS_COLORSPACE;
481 case FF_COLOR_YUV_JPEG:
482 if (ps->color_type != FF_COLOR_YUV_JPEG &&
483 ps->color_type != FF_COLOR_YUV && ps->color_type != FF_COLOR_GRAY)
484 loss |= FF_LOSS_COLORSPACE;
488 if (ps->color_type != pf->color_type)
489 loss |= FF_LOSS_COLORSPACE;
492 if (pf->color_type == FF_COLOR_GRAY && ps->color_type != FF_COLOR_GRAY)
493 loss |= FF_LOSS_CHROMA;
494 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
495 loss |= FF_LOSS_ALPHA;
496 if (pf->pixel_type == FF_PIXEL_PALETTE &&
497 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
498 loss |= FF_LOSS_COLORQUANT;
503 avg_bits_per_pixel (int pix_fmt)
506 const PixFmtInfo *pf;
508 pf = get_pix_fmt_info (pix_fmt);
509 switch (pf->pixel_type) {
510 case FF_PIXEL_PACKED:
513 case PIX_FMT_UYVY422:
518 case PIX_FMT_UYVY411:
522 bits = pf->depth * pf->nb_channels;
526 case FF_PIXEL_PLANAR:
527 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
528 bits = pf->depth * pf->nb_channels;
530 bits = pf->depth + ((2 * pf->depth) >>
531 (pf->x_chroma_shift + pf->y_chroma_shift));
534 case FF_PIXEL_PALETTE:
545 avcodec_find_best_pix_fmt1 (int pix_fmt_mask,
546 int src_pix_fmt, int has_alpha, int loss_mask)
548 int dist, i, loss, min_dist, dst_pix_fmt;
550 /* find exact color match with smallest size */
552 min_dist = 0x7fffffff;
553 for (i = 0; i < PIX_FMT_NB; i++) {
554 if (pix_fmt_mask & (1 << i)) {
555 loss = avcodec_get_pix_fmt_loss (i, src_pix_fmt, has_alpha) & loss_mask;
557 dist = avg_bits_per_pixel (i);
558 if (dist < min_dist) {
569 * find best pixel format to convert to. Return -1 if none found
572 avcodec_find_best_pix_fmt (int pix_fmt_mask, int src_pix_fmt,
573 int has_alpha, int *loss_ptr)
575 int dst_pix_fmt, loss_mask, i;
576 static const int loss_mask_order[] = {
577 ~0, /* no loss first */
580 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
586 /* try with successive loss */
589 loss_mask = loss_mask_order[i++];
590 dst_pix_fmt = avcodec_find_best_pix_fmt1 (pix_fmt_mask, src_pix_fmt,
591 has_alpha, loss_mask);
592 if (dst_pix_fmt >= 0)
600 *loss_ptr = avcodec_get_pix_fmt_loss (dst_pix_fmt, src_pix_fmt, has_alpha);
605 img_copy_plane (uint8_t * dst, int dst_wrap,
606 const uint8_t * src, int src_wrap, int width, int height)
608 for (; height > 0; height--) {
609 memcpy (dst, src, width);
616 * Copy image 'src' to 'dst'.
619 img_copy (AVPicture * dst, const AVPicture * src,
620 int pix_fmt, int width, int height)
623 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
625 pf = get_pix_fmt_info (pix_fmt);
626 switch (pf->pixel_type) {
627 case FF_PIXEL_PACKED:
630 case PIX_FMT_UYVY422:
635 case PIX_FMT_UYVY411:
639 bits = pf->depth * pf->nb_channels;
642 bwidth = (width * bits + 7) >> 3;
643 img_copy_plane (dst->data[0], dst->linesize[0],
644 src->data[0], src->linesize[0], bwidth, height);
646 case FF_PIXEL_PLANAR:
647 for (i = 0; i < pf->nb_channels; i++) {
652 if (i == 1 || i == 2) {
653 w >>= pf->x_chroma_shift;
654 h >>= pf->y_chroma_shift;
656 bwidth = (w * pf->depth + 7) >> 3;
657 img_copy_plane (dst->data[i], dst->linesize[i],
658 src->data[i], src->linesize[i], bwidth, h);
661 case FF_PIXEL_PALETTE:
662 img_copy_plane (dst->data[0], dst->linesize[0],
663 src->data[0], src->linesize[0], width, height);
664 /* copy the palette */
665 img_copy_plane (dst->data[1], dst->linesize[1],
666 src->data[1], src->linesize[1], 4, 256);
671 /* XXX: totally non optimized */
674 yuv422_to_yuv420p (AVPicture * dst, const AVPicture * src,
675 int width, int height)
677 const uint8_t *p, *p1;
678 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
686 for (; height >= 1; height -= 2) {
691 for (w = width; w >= 2; w -= 2) {
708 p1 += src->linesize[0];
709 lum1 += dst->linesize[0];
713 for (w = width; w >= 2; w -= 2) {
722 p1 += src->linesize[0];
723 lum1 += dst->linesize[0];
725 cb1 += dst->linesize[1];
726 cr1 += dst->linesize[2];
731 uyvy422_to_yuv420p (AVPicture * dst, const AVPicture * src,
732 int width, int height)
734 const uint8_t *p, *p1;
735 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
744 for (; height >= 1; height -= 2) {
749 for (w = width; w >= 2; w -= 2) {
766 p1 += src->linesize[0];
767 lum1 += dst->linesize[0];
771 for (w = width; w >= 2; w -= 2) {
780 p1 += src->linesize[0];
781 lum1 += dst->linesize[0];
783 cb1 += dst->linesize[1];
784 cr1 += dst->linesize[2];
790 uyvy422_to_yuv422p (AVPicture * dst, const AVPicture * src,
791 int width, int height)
793 const uint8_t *p, *p1;
794 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
801 for (; height > 0; height--) {
806 for (w = width; w >= 2; w -= 2) {
816 p1 += src->linesize[0];
817 lum1 += dst->linesize[0];
818 cb1 += dst->linesize[1];
819 cr1 += dst->linesize[2];
825 yuv422_to_yuv422p (AVPicture * dst, const AVPicture * src,
826 int width, int height)
828 const uint8_t *p, *p1;
829 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
836 for (; height > 0; height--) {
841 for (w = width; w >= 2; w -= 2) {
851 p1 += src->linesize[0];
852 lum1 += dst->linesize[0];
853 cb1 += dst->linesize[1];
854 cr1 += dst->linesize[2];
859 yuv422p_to_yuv422 (AVPicture * dst, const AVPicture * src,
860 int width, int height)
863 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
870 for (; height > 0; height--) {
875 for (w = width; w >= 2; w -= 2) {
889 p1 += dst->linesize[0];
890 lum1 += src->linesize[0];
891 cb1 += src->linesize[1];
892 cr1 += src->linesize[2];
897 yuv422p_to_uyvy422 (AVPicture * dst, const AVPicture * src,
898 int width, int height)
901 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
908 for (; height > 0; height--) {
913 for (w = width; w >= 2; w -= 2) {
923 p1 += dst->linesize[0];
924 lum1 += src->linesize[0];
925 cb1 += src->linesize[1];
926 cr1 += src->linesize[2];
931 uyvy411_to_yuv411p (AVPicture * dst, const AVPicture * src,
932 int width, int height)
934 const uint8_t *p, *p1;
935 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
942 for (; height > 0; height--) {
947 for (w = width; w >= 4; w -= 4) {
959 p1 += src->linesize[0];
960 lum1 += dst->linesize[0];
961 cb1 += dst->linesize[1];
962 cr1 += dst->linesize[2];
968 yuv420p_to_yuv422 (AVPicture * dst, const AVPicture * src,
969 int width, int height)
972 uint8_t *line1, *line2, *linesrc = dst->data[0];
973 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
974 uint8_t *cb1, *cb2 = src->data[1];
975 uint8_t *cr1, *cr2 = src->data[2];
977 for (h = height / 2; h--;) {
979 line2 = linesrc + dst->linesize[0];
982 lum2 = lumsrc + src->linesize[0];
987 for (w = width / 2; w--;) {
990 *line1++ = *line2++ = *cb1++;
993 *line1++ = *line2++ = *cr1++;
996 if (width % 2 != 0) {
999 *line1++ = *line2++ = *cb1++;
1002 linesrc += dst->linesize[0] * 2;
1003 lumsrc += src->linesize[0] * 2;
1004 cb2 += src->linesize[1];
1005 cr2 += src->linesize[2];
1008 if (height % 2 != 0) {
1014 for (w = width / 2; w--;) {
1021 if (width % 2 != 0) {
1028 #define SCALEBITS 10
1029 #define ONE_HALF (1 << (SCALEBITS - 1))
1030 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1032 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1036 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1037 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1039 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1042 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1044 y = ((y1) - 16) * FIX(255.0/219.0);\
1045 r = cm[(y + r_add) >> SCALEBITS];\
1046 g = cm[(y + g_add) >> SCALEBITS];\
1047 b = cm[(y + b_add) >> SCALEBITS];\
1050 #define YUV_TO_RGB1(cb1, cr1)\
1054 r_add = FIX(1.40200) * cr + ONE_HALF;\
1055 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1056 b_add = FIX(1.77200) * cb + ONE_HALF;\
1059 #define YUV_TO_RGB2(r, g, b, y1)\
1061 y = (y1) << SCALEBITS;\
1062 r = cm[(y + r_add) >> SCALEBITS];\
1063 g = cm[(y + g_add) >> SCALEBITS];\
1064 b = cm[(y + b_add) >> SCALEBITS];\
1067 #define Y_CCIR_TO_JPEG(y)\
1068 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1070 #define Y_JPEG_TO_CCIR(y)\
1071 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1073 #define C_CCIR_TO_JPEG(y)\
1074 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1076 /* NOTE: the clamp is really necessary! */
1078 C_JPEG_TO_CCIR (int y)
1080 y = (((y - 128) * FIX (112.0 / 127.0) + (ONE_HALF +
1081 (128 << SCALEBITS))) >> SCALEBITS);
1088 #define RGB_TO_Y(r, g, b) \
1089 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1090 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1092 #define RGB_TO_U(r1, g1, b1, shift)\
1093 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1094 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1096 #define RGB_TO_V(r1, g1, b1, shift)\
1097 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1098 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1100 #define RGB_TO_Y_CCIR(r, g, b) \
1101 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1102 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1104 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1105 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1106 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1108 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1109 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1110 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1112 static uint8_t y_ccir_to_jpeg[256];
1113 static uint8_t y_jpeg_to_ccir[256];
1114 static uint8_t c_ccir_to_jpeg[256];
1115 static uint8_t c_jpeg_to_ccir[256];
1117 /* init various conversion tables */
1119 img_convert_init (void)
1122 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1124 for (i = 0; i < 256; i++) {
1125 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG (i);
1126 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR (i);
1127 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG (i);
1128 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR (i);
1132 /* apply to each pixel the given table */
1134 img_apply_table (uint8_t * dst, int dst_wrap,
1135 const uint8_t * src, int src_wrap,
1136 int width, int height, const uint8_t * table1)
1141 const uint8_t *table;
1144 for (; height > 0; height--) {
1168 /* XXX: use generic filter ? */
1169 /* XXX: in most cases, the sampling position is incorrect */
1173 shrink41 (uint8_t * dst, int dst_wrap,
1174 const uint8_t * src, int src_wrap, int width, int height)
1180 for (; height > 0; height--) {
1183 for (w = width; w > 0; w--) {
1184 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1195 shrink21 (uint8_t * dst, int dst_wrap,
1196 const uint8_t * src, int src_wrap, int width, int height)
1202 for (; height > 0; height--) {
1205 for (w = width; w > 0; w--) {
1206 d[0] = (s[0] + s[1]) >> 1;
1217 shrink12 (uint8_t * dst, int dst_wrap,
1218 const uint8_t * src, int src_wrap, int width, int height)
1222 const uint8_t *s1, *s2;
1224 for (; height > 0; height--) {
1228 for (w = width; w >= 4; w -= 4) {
1229 d[0] = (s1[0] + s2[0]) >> 1;
1230 d[1] = (s1[1] + s2[1]) >> 1;
1231 d[2] = (s1[2] + s2[2]) >> 1;
1232 d[3] = (s1[3] + s2[3]) >> 1;
1237 for (; w > 0; w--) {
1238 d[0] = (s1[0] + s2[0]) >> 1;
1243 src += 2 * src_wrap;
1250 shrink22 (uint8_t * dst, int dst_wrap,
1251 const uint8_t * src, int src_wrap, int width, int height)
1254 const uint8_t *s1, *s2;
1257 for (; height > 0; height--) {
1261 for (w = width; w >= 4; w -= 4) {
1262 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1263 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1264 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1265 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1270 for (; w > 0; w--) {
1271 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1276 src += 2 * src_wrap;
1283 shrink44 (uint8_t * dst, int dst_wrap,
1284 const uint8_t * src, int src_wrap, int width, int height)
1287 const uint8_t *s1, *s2, *s3, *s4;
1290 for (; height > 0; height--) {
1296 for (w = width; w > 0; w--) {
1297 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1298 s2[0] + s2[1] + s2[2] + s2[3] +
1299 s3[0] + s3[1] + s3[2] + s3[3] +
1300 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1307 src += 4 * src_wrap;
1313 grow21_line (uint8_t * dst, const uint8_t * src, int width)
1321 for (w = width; w >= 4; w -= 4) {
1322 d[1] = d[0] = s1[0];
1323 d[3] = d[2] = s1[1];
1327 for (; w >= 2; w -= 2) {
1328 d[1] = d[0] = s1[0];
1332 /* only needed if width is not a multiple of two */
1333 /* XXX: veryfy that */
1340 grow41_line (uint8_t * dst, const uint8_t * src, int width)
1348 for (w = width; w >= 4; w -= 4) {
1361 grow21 (uint8_t * dst, int dst_wrap,
1362 const uint8_t * src, int src_wrap, int width, int height)
1364 for (; height > 0; height--) {
1365 grow21_line (dst, src, width);
1373 grow22 (uint8_t * dst, int dst_wrap,
1374 const uint8_t * src, int src_wrap, int width, int height)
1376 for (; height > 0; height--) {
1377 grow21_line (dst, src, width);
1386 grow41 (uint8_t * dst, int dst_wrap,
1387 const uint8_t * src, int src_wrap, int width, int height)
1389 for (; height > 0; height--) {
1390 grow41_line (dst, src, width);
1398 grow44 (uint8_t * dst, int dst_wrap,
1399 const uint8_t * src, int src_wrap, int width, int height)
1401 for (; height > 0; height--) {
1402 grow41_line (dst, src, width);
1403 if ((height & 3) == 1)
1411 conv411 (uint8_t * dst, int dst_wrap,
1412 const uint8_t * src, int src_wrap, int width, int height)
1415 const uint8_t *s1, *s2;
1420 for (; height > 0; height--) {
1422 s2 = src + src_wrap;
1424 for (w = width; w > 0; w--) {
1425 c = (s1[0] + s2[0]) >> 1;
1432 src += src_wrap * 2;
1437 /* XXX: add jpeg quantize code */
1439 #define TRANSP_INDEX (6*6*6)
1441 /* this is maybe slow, but allows for extensions */
1442 static inline unsigned char
1443 gif_clut_index (uint8_t r, uint8_t g, uint8_t b)
1445 return ((((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6));
1449 build_rgb_palette (uint8_t * palette, int has_alpha)
1452 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1455 pal = (uint32_t *) palette;
1457 for (r = 0; r < 6; r++) {
1458 for (g = 0; g < 6; g++) {
1459 for (b = 0; b < 6; b++) {
1460 pal[i++] = (0xff << 24) | (pal_value[r] << 16) |
1461 (pal_value[g] << 8) | pal_value[b];
1468 pal[i++] = 0xff000000;
1471 /* copy bit n to bits 0 ... n - 1 */
1472 static inline unsigned int
1473 bitcopy_n (unsigned int a, int n)
1477 mask = (1 << n) - 1;
1478 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1481 /* rgb555 handling */
1483 #define RGB_NAME rgb555
1485 #define RGB_IN(r, g, b, s)\
1487 unsigned int v = ((const uint16_t *)(s))[0];\
1488 r = bitcopy_n(v >> (10 - 3), 3);\
1489 g = bitcopy_n(v >> (5 - 3), 3);\
1490 b = bitcopy_n(v << 3, 3);\
1493 #define RGBA_IN(r, g, b, a, s)\
1495 unsigned int v = ((const uint16_t *)(s))[0];\
1496 r = bitcopy_n(v >> (10 - 3), 3);\
1497 g = bitcopy_n(v >> (5 - 3), 3);\
1498 b = bitcopy_n(v << 3, 3);\
1499 a = (-(v >> 15)) & 0xff;\
1502 #define RGBA_OUT(d, r, g, b, a)\
1504 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1505 ((a << 8) & 0x8000);\
1510 #include "imgconvert_template.h"
1512 /* rgb565 handling */
1514 #define RGB_NAME rgb565
1516 #define RGB_IN(r, g, b, s)\
1518 unsigned int v = ((const uint16_t *)(s))[0];\
1519 r = bitcopy_n(v >> (11 - 3), 3);\
1520 g = bitcopy_n(v >> (5 - 2), 2);\
1521 b = bitcopy_n(v << 3, 3);\
1524 #define RGB_OUT(d, r, g, b)\
1526 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1531 #include "imgconvert_template.h"
1533 /* bgr24 handling */
1535 #define RGB_NAME bgr24
1537 #define RGB_IN(r, g, b, s)\
1544 #define RGB_OUT(d, r, g, b)\
1553 #include "imgconvert_template.h"
1559 /* rgb24 handling */
1561 #define RGB_NAME rgb24
1564 #define RGB_IN(r, g, b, s)\
1571 #define RGB_OUT(d, r, g, b)\
1580 #include "imgconvert_template.h"
1582 /* rgb32 handling */
1584 #define RGB_NAME rgb32
1587 #define RGB_IN(r, g, b, s)\
1589 unsigned int v = ((const uint32_t *)(s))[0];\
1590 r = (v >> 16) & 0xff;\
1591 g = (v >> 8) & 0xff;\
1595 #define RGBA_IN(r, g, b, a, s)\
1597 unsigned int v = ((const uint32_t *)(s))[0];\
1599 r = (v >> 16) & 0xff;\
1600 g = (v >> 8) & 0xff;\
1604 #define RGBA_OUT(d, r, g, b, a)\
1606 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1611 #include "imgconvert_template.h"
1613 /* bgr32 handling */
1615 #define RGB_NAME bgr32
1617 #define RGB_IN(r, g, b, s)\
1619 unsigned int v = ((const uint32_t *)(s))[0];\
1620 r = (v >> 8) & 0xff;\
1621 g = (v >> 16) & 0xff;\
1622 b = (v >> 24) & 0xff;\
1625 #define RGBA_IN(r, g, b, a, s)\
1627 unsigned int v = ((const uint32_t *)(s))[0];\
1629 r = (v >> 8) & 0xff;\
1630 g = (v >> 16) & 0xff;\
1631 b = (v >> 24) & 0xff;\
1634 #define RGBA_OUT(d, r, g, b, a)\
1636 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24);\
1641 #include "imgconvert_template.h"
1643 /* rgba32 handling */
1645 #define RGB_NAME rgba32
1648 #define RGB_IN(r, g, b, s)\
1650 unsigned int v = ((const uint32_t *)(s))[0];\
1651 r = (v >> 16) & 0xff;\
1652 g = (v >> 8) & 0xff;\
1656 #define RGBA_IN(r, g, b, a, s)\
1658 unsigned int v = ((const uint32_t *)(s))[0];\
1659 a = (v >> 24) & 0xff;\
1660 r = (v >> 16) & 0xff;\
1661 g = (v >> 8) & 0xff;\
1665 #define RGBA_OUT(d, r, g, b, a)\
1667 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1672 #include "imgconvert_template.h"
1674 /* bgra32 handling */
1676 #define RGB_NAME bgra32
1679 #define RGB_IN(r, g, b, s)\
1681 unsigned int v = ((const uint32_t *)(s))[0];\
1682 r = (v >> 8) & 0xff;\
1683 g = (v >> 16) & 0xff;\
1684 b = (v >> 24) & 0xff;\
1687 #define RGBA_IN(r, g, b, a, s)\
1689 unsigned int v = ((const uint32_t *)(s))[0];\
1691 r = (v >> 8) & 0xff;\
1692 g = (v >> 16) & 0xff;\
1693 b = (v >> 24) & 0xff;\
1696 #define RGBA_OUT(d, r, g, b, a)\
1698 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24 );\
1703 #include "imgconvert_template.h"
1706 mono_to_gray (AVPicture * dst, const AVPicture * src,
1707 int width, int height, int xor_mask)
1709 const unsigned char *p;
1711 int v, dst_wrap, src_wrap;
1715 src_wrap = src->linesize[0] - ((width + 7) >> 3);
1718 dst_wrap = dst->linesize[0] - width;
1719 for (y = 0; y < height; y++) {
1722 v = *p++ ^ xor_mask;
1724 q[1] = -((v >> 6) & 1);
1725 q[2] = -((v >> 5) & 1);
1726 q[3] = -((v >> 4) & 1);
1727 q[4] = -((v >> 3) & 1);
1728 q[5] = -((v >> 2) & 1);
1729 q[6] = -((v >> 1) & 1);
1730 q[7] = -((v >> 0) & 1);
1735 v = *p++ ^ xor_mask;
1737 q[0] = -((v >> 7) & 1);
1748 monowhite_to_gray (AVPicture * dst, const AVPicture * src,
1749 int width, int height)
1751 mono_to_gray (dst, src, width, height, 0xff);
1755 monoblack_to_gray (AVPicture * dst, const AVPicture * src,
1756 int width, int height)
1758 mono_to_gray (dst, src, width, height, 0x00);
1762 gray_to_mono (AVPicture * dst, const AVPicture * src,
1763 int width, int height, int xor_mask)
1768 int j, b, v, n1, src_wrap, dst_wrap, y;
1771 src_wrap = src->linesize[0] - width;
1774 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
1776 for (y = 0; y < height; y++) {
1780 for (j = 0; j < 8; j++) {
1783 v = (v << 1) | (b >> 7);
1785 d[0] = v ^ xor_mask;
1795 v = (v << 1) | (b >> 7);
1798 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
1807 gray_to_monowhite (AVPicture * dst, const AVPicture * src,
1808 int width, int height)
1810 gray_to_mono (dst, src, width, height, 0xff);
1814 gray_to_monoblack (AVPicture * dst, const AVPicture * src,
1815 int width, int height)
1817 gray_to_mono (dst, src, width, height, 0x00);
1820 typedef struct ConvertEntry
1822 enum PixelFormat src;
1823 enum PixelFormat dest;
1824 void (*convert) (AVPicture * dst,
1825 const AVPicture * src, int width, int height);
1828 /* Add each new convertion function in this table. In order to be able
1829 to convert from any format to any format, the following constraints
1832 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1834 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1836 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
1838 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1841 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1843 The other conversion functions are just optimisations for common cases.
1845 static ConvertEntry convert_table[] = {
1846 {PIX_FMT_YUV420P, PIX_FMT_YUV422, yuv420p_to_yuv422},
1847 {PIX_FMT_YUV420P, PIX_FMT_RGB555, yuv420p_to_rgb555},
1848 {PIX_FMT_YUV420P, PIX_FMT_RGB565, yuv420p_to_rgb565},
1849 {PIX_FMT_YUV420P, PIX_FMT_BGR24, yuv420p_to_bgr24},
1850 {PIX_FMT_YUV420P, PIX_FMT_RGB24, yuv420p_to_rgb24},
1851 {PIX_FMT_YUV420P, PIX_FMT_RGB32, yuv420p_to_rgb32},
1852 {PIX_FMT_YUV420P, PIX_FMT_BGR32, yuv420p_to_bgr32},
1853 {PIX_FMT_YUV420P, PIX_FMT_RGBA32, yuv420p_to_rgba32},
1854 {PIX_FMT_YUV420P, PIX_FMT_BGRA32, yuv420p_to_bgra32},
1856 {PIX_FMT_YUV422P, PIX_FMT_YUV422, yuv422p_to_yuv422},
1857 {PIX_FMT_YUV422P, PIX_FMT_UYVY422, yuv422p_to_uyvy422},
1859 {PIX_FMT_YUV444P, PIX_FMT_RGB24, yuv444p_to_rgb24},
1861 {PIX_FMT_YUVJ420P, PIX_FMT_RGB555, yuvj420p_to_rgb555},
1862 {PIX_FMT_YUVJ420P, PIX_FMT_RGB565, yuvj420p_to_rgb565},
1863 {PIX_FMT_YUVJ420P, PIX_FMT_BGR24, yuvj420p_to_bgr24},
1864 {PIX_FMT_YUVJ420P, PIX_FMT_RGB24, yuvj420p_to_rgb24},
1865 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_rgb32},
1866 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgr32},
1867 {PIX_FMT_YUVJ420P, PIX_FMT_RGBA32, yuvj420p_to_rgba32},
1868 {PIX_FMT_YUVJ420P, PIX_FMT_BGRA32, yuvj420p_to_bgra32},
1870 {PIX_FMT_YUVJ444P, PIX_FMT_RGB24, yuvj444p_to_rgb24},
1872 {PIX_FMT_YUV422, PIX_FMT_YUV420P, yuv422_to_yuv420p},
1873 {PIX_FMT_YUV422, PIX_FMT_YUV422P, yuv422_to_yuv422p},
1875 {PIX_FMT_UYVY422, PIX_FMT_YUV420P, uyvy422_to_yuv420p},
1876 {PIX_FMT_UYVY422, PIX_FMT_YUV422P, uyvy422_to_yuv422p},
1878 {PIX_FMT_RGB24, PIX_FMT_YUV420P, rgb24_to_yuv420p},
1879 {PIX_FMT_RGB24, PIX_FMT_RGB565, rgb24_to_rgb565},
1880 {PIX_FMT_RGB24, PIX_FMT_RGB555, rgb24_to_rgb555},
1881 {PIX_FMT_RGB24, PIX_FMT_RGB32, rgb24_to_rgb32},
1882 {PIX_FMT_RGB24, PIX_FMT_BGR32, rgb24_to_bgr32},
1883 {PIX_FMT_RGB24, PIX_FMT_RGBA32, rgb24_to_rgba32},
1884 {PIX_FMT_RGB24, PIX_FMT_BGR24, rgb24_to_bgr24},
1885 {PIX_FMT_RGB24, PIX_FMT_BGRA32, rgb24_to_bgra32},
1886 {PIX_FMT_RGB24, PIX_FMT_GRAY8, rgb24_to_gray},
1887 {PIX_FMT_RGB24, PIX_FMT_PAL8, rgb24_to_pal8},
1888 {PIX_FMT_RGB24, PIX_FMT_YUV444P, rgb24_to_yuv444p},
1889 {PIX_FMT_RGB24, PIX_FMT_YUVJ420P, rgb24_to_yuvj420p},
1890 {PIX_FMT_RGB24, PIX_FMT_YUVJ444P, rgb24_to_yuvj444p},
1891 {PIX_FMT_RGB24, PIX_FMT_AYUV4444, rgb24_to_ayuv4444},
1893 {PIX_FMT_RGB32, PIX_FMT_RGB24, rgb32_to_rgb24},
1894 {PIX_FMT_RGB32, PIX_FMT_RGB555, rgba32_to_rgb555},
1895 {PIX_FMT_RGB32, PIX_FMT_PAL8, rgb32_to_pal8},
1896 {PIX_FMT_RGB32, PIX_FMT_YUV420P, rgb32_to_yuv420p},
1897 {PIX_FMT_RGB32, PIX_FMT_GRAY8, rgb32_to_gray},
1899 {PIX_FMT_RGBA32, PIX_FMT_BGRA32, rgba32_to_bgra32},
1900 {PIX_FMT_RGBA32, PIX_FMT_BGR32, rgba32_to_bgr32},
1901 {PIX_FMT_RGBA32, PIX_FMT_RGB24, rgba32_to_rgb24},
1902 {PIX_FMT_RGBA32, PIX_FMT_RGB555, rgba32_to_rgb555},
1903 {PIX_FMT_RGBA32, PIX_FMT_PAL8, rgba32_to_pal8},
1904 {PIX_FMT_RGBA32, PIX_FMT_YUV420P, rgba32_to_yuv420p},
1905 {PIX_FMT_RGBA32, PIX_FMT_GRAY8, rgba32_to_gray},
1906 {PIX_FMT_RGBA32, PIX_FMT_AYUV4444, rgba32_to_ayuv4444},
1907 {PIX_FMT_BGRA32, PIX_FMT_AYUV4444, bgra32_to_ayuv4444},
1909 {PIX_FMT_BGR24, PIX_FMT_RGB24, bgr24_to_rgb24},
1910 {PIX_FMT_BGR24, PIX_FMT_YUV420P, bgr24_to_yuv420p},
1911 {PIX_FMT_BGR24, PIX_FMT_GRAY8, bgr24_to_gray},
1913 {PIX_FMT_BGR32, PIX_FMT_RGB24, bgr32_to_rgb24},
1914 {PIX_FMT_BGR32, PIX_FMT_RGBA32, bgr32_to_rgba32},
1915 {PIX_FMT_BGR32, PIX_FMT_YUV420P, bgr32_to_yuv420p},
1916 {PIX_FMT_BGR32, PIX_FMT_GRAY8, bgr32_to_gray},
1918 {PIX_FMT_BGRA32, PIX_FMT_RGB24, bgra32_to_rgb24},
1919 {PIX_FMT_BGRA32, PIX_FMT_RGBA32, bgra32_to_rgba32},
1920 {PIX_FMT_BGRA32, PIX_FMT_YUV420P, bgra32_to_yuv420p},
1921 {PIX_FMT_BGRA32, PIX_FMT_GRAY8, bgra32_to_gray},
1923 {PIX_FMT_RGB555, PIX_FMT_RGB24, rgb555_to_rgb24},
1924 {PIX_FMT_RGB555, PIX_FMT_RGB32, rgb555_to_rgba32},
1925 {PIX_FMT_RGB555, PIX_FMT_RGBA32, rgb555_to_rgba32},
1926 {PIX_FMT_RGB555, PIX_FMT_YUV420P, rgb555_to_yuv420p},
1927 {PIX_FMT_RGB555, PIX_FMT_GRAY8, rgb555_to_gray},
1929 {PIX_FMT_RGB565, PIX_FMT_RGB24, rgb565_to_rgb24},
1930 {PIX_FMT_RGB565, PIX_FMT_YUV420P, rgb565_to_yuv420p},
1931 {PIX_FMT_RGB565, PIX_FMT_GRAY8, rgb565_to_gray},
1933 {PIX_FMT_GRAY8, PIX_FMT_RGB555, gray_to_rgb555},
1934 {PIX_FMT_GRAY8, PIX_FMT_RGB565, gray_to_rgb565},
1935 {PIX_FMT_GRAY8, PIX_FMT_RGB24, gray_to_rgb24},
1936 {PIX_FMT_GRAY8, PIX_FMT_BGR24, gray_to_bgr24},
1937 {PIX_FMT_GRAY8, PIX_FMT_RGB32, gray_to_rgb32},
1938 {PIX_FMT_GRAY8, PIX_FMT_BGR32, gray_to_bgr32},
1939 {PIX_FMT_GRAY8, PIX_FMT_RGBA32, gray_to_rgba32},
1940 {PIX_FMT_GRAY8, PIX_FMT_BGRA32, gray_to_bgra32},
1941 {PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, gray_to_monowhite},
1942 {PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, gray_to_monoblack},
1944 {PIX_FMT_MONOWHITE, PIX_FMT_GRAY8, monowhite_to_gray},
1946 {PIX_FMT_MONOBLACK, PIX_FMT_GRAY8, monoblack_to_gray},
1948 {PIX_FMT_PAL8, PIX_FMT_RGB555, pal8_to_rgb555},
1949 {PIX_FMT_PAL8, PIX_FMT_RGB565, pal8_to_rgb565},
1950 {PIX_FMT_PAL8, PIX_FMT_BGR24, pal8_to_bgr24},
1951 {PIX_FMT_PAL8, PIX_FMT_RGB24, pal8_to_rgb24},
1952 {PIX_FMT_PAL8, PIX_FMT_RGB32, pal8_to_rgb32},
1953 {PIX_FMT_PAL8, PIX_FMT_BGR32, pal8_to_bgr32},
1954 {PIX_FMT_PAL8, PIX_FMT_RGBA32, pal8_to_rgba32},
1955 {PIX_FMT_PAL8, PIX_FMT_BGRA32, pal8_to_bgra32},
1957 {PIX_FMT_UYVY411, PIX_FMT_YUV411P, uyvy411_to_yuv411p},
1959 {PIX_FMT_AYUV4444, PIX_FMT_RGBA32, ayuv4444_to_rgba32},
1961 {PIX_FMT_AYUV4444, PIX_FMT_RGB24, ayuv4444_to_rgb24}
1964 static ConvertEntry *
1965 get_convert_table_entry (int src_pix_fmt, int dst_pix_fmt)
1969 for (i = 0; i < sizeof (convert_table) / sizeof (convert_table[0]); i++) {
1970 if (convert_table[i].src == src_pix_fmt &&
1971 convert_table[i].dest == dst_pix_fmt) {
1972 return convert_table + i;
1980 avpicture_alloc (AVPicture * picture, int pix_fmt, int width, int height)
1985 size = avpicture_get_size (pix_fmt, width, height);
1986 ptr = av_malloc (size);
1989 gst_ffmpegcsp_avpicture_fill (picture, ptr, pix_fmt, width, height);
1992 memset (picture, 0, sizeof (AVPicture));
1997 avpicture_free (AVPicture * picture)
1999 av_free (picture->data[0]);
2002 /* return true if yuv planar */
2004 is_yuv_planar (PixFmtInfo * ps)
2006 return (ps->color_type == FF_COLOR_YUV ||
2007 ps->color_type == FF_COLOR_YUV_JPEG) && ps->pixel_type == FF_PIXEL_PLANAR;
2010 /* XXX: always use linesize. Return -1 if not supported */
2012 img_convert (AVPicture * dst, int dst_pix_fmt,
2013 const AVPicture * src, int src_pix_fmt, int src_width, int src_height)
2016 int i, ret, dst_width, dst_height, int_pix_fmt;
2017 PixFmtInfo *src_pix, *dst_pix;
2019 AVPicture tmp1, *tmp = &tmp1;
2021 if (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2022 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB)
2024 if (src_width <= 0 || src_height <= 0)
2029 img_convert_init ();
2032 dst_width = src_width;
2033 dst_height = src_height;
2035 dst_pix = get_pix_fmt_info (dst_pix_fmt);
2036 src_pix = get_pix_fmt_info (src_pix_fmt);
2037 if (src_pix_fmt == dst_pix_fmt) {
2038 /* no conversion needed: just copy */
2039 img_copy (dst, src, dst_pix_fmt, dst_width, dst_height);
2043 ce = get_convert_table_entry (src_pix_fmt, dst_pix_fmt);
2044 if (ce && ce->convert) {
2045 /* specific conversion routine */
2046 ce->convert (dst, src, dst_width, dst_height);
2051 if (is_yuv_planar (dst_pix) && src_pix_fmt == PIX_FMT_GRAY8) {
2055 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2056 img_copy_plane (dst->data[0], dst->linesize[0],
2057 src->data[0], src->linesize[0], dst_width, dst_height);
2059 img_apply_table (dst->data[0], dst->linesize[0],
2060 src->data[0], src->linesize[0],
2061 dst_width, dst_height, y_jpeg_to_ccir);
2063 /* fill U and V with 128 */
2066 w >>= dst_pix->x_chroma_shift;
2067 h >>= dst_pix->y_chroma_shift;
2068 for (i = 1; i <= 2; i++) {
2070 for (y = 0; y < h; y++) {
2072 d += dst->linesize[i];
2079 if (is_yuv_planar (src_pix) && dst_pix_fmt == PIX_FMT_GRAY8) {
2080 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2081 img_copy_plane (dst->data[0], dst->linesize[0],
2082 src->data[0], src->linesize[0], dst_width, dst_height);
2084 img_apply_table (dst->data[0], dst->linesize[0],
2085 src->data[0], src->linesize[0],
2086 dst_width, dst_height, y_ccir_to_jpeg);
2091 /* YUV to YUV planar */
2092 if (is_yuv_planar (dst_pix) && is_yuv_planar (src_pix)) {
2093 int x_shift, y_shift, w, h, xy_shift;
2094 void (*resize_func) (uint8_t * dst, int dst_wrap,
2095 const uint8_t * src, int src_wrap, int width, int height);
2097 /* compute chroma size of the smallest dimensions */
2100 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2101 w >>= dst_pix->x_chroma_shift;
2103 w >>= src_pix->x_chroma_shift;
2104 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2105 h >>= dst_pix->y_chroma_shift;
2107 h >>= src_pix->y_chroma_shift;
2109 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2110 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2111 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2112 /* there must be filters for conversion at least from and to
2116 resize_func = img_copy_plane;
2119 resize_func = shrink21;
2122 resize_func = shrink41;
2125 resize_func = shrink12;
2128 resize_func = shrink22;
2131 resize_func = shrink44;
2134 resize_func = grow21;
2137 resize_func = grow41;
2140 resize_func = grow22;
2143 resize_func = grow44;
2146 resize_func = conv411;
2149 /* currently not handled */
2150 goto no_chroma_filter;
2153 img_copy_plane (dst->data[0], dst->linesize[0],
2154 src->data[0], src->linesize[0], dst_width, dst_height);
2156 #define GEN_MASK(x) ((1<<(x))-1)
2157 #define DIV_ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) >> (x))
2159 for (i = 1; i <= 2; i++)
2160 resize_func (dst->data[i], dst->linesize[i],
2161 src->data[i], src->linesize[i],
2162 DIV_ROUND_UP_X (dst_width, dst_pix->x_chroma_shift),
2163 DIV_ROUND_UP_X (dst_height, dst_pix->y_chroma_shift));
2164 /* if yuv color space conversion is needed, we do it here on
2165 the destination image */
2166 if (dst_pix->color_type != src_pix->color_type) {
2167 const uint8_t *y_table, *c_table;
2169 if (dst_pix->color_type == FF_COLOR_YUV) {
2170 y_table = y_jpeg_to_ccir;
2171 c_table = c_jpeg_to_ccir;
2173 y_table = y_ccir_to_jpeg;
2174 c_table = c_ccir_to_jpeg;
2176 img_apply_table (dst->data[0], dst->linesize[0],
2177 dst->data[0], dst->linesize[0], dst_width, dst_height, y_table);
2179 for (i = 1; i <= 2; i++)
2180 img_apply_table (dst->data[i], dst->linesize[i],
2181 dst->data[i], dst->linesize[i],
2182 dst_width >> dst_pix->x_chroma_shift,
2183 dst_height >> dst_pix->y_chroma_shift, c_table);
2189 /* try to use an intermediate format */
2190 if (src_pix_fmt == PIX_FMT_YUV422 || dst_pix_fmt == PIX_FMT_YUV422) {
2191 /* specific case: convert to YUV422P first */
2192 int_pix_fmt = PIX_FMT_YUV422P;
2193 } else if (src_pix_fmt == PIX_FMT_UYVY422 || dst_pix_fmt == PIX_FMT_UYVY422) {
2194 /* specific case: convert to YUV422P first */
2195 int_pix_fmt = PIX_FMT_YUV422P;
2196 } else if (src_pix_fmt == PIX_FMT_UYVY411 || dst_pix_fmt == PIX_FMT_UYVY411) {
2197 /* specific case: convert to YUV411P first */
2198 int_pix_fmt = PIX_FMT_YUV411P;
2199 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2200 src_pix_fmt != PIX_FMT_GRAY8) ||
2201 (dst_pix->color_type == FF_COLOR_GRAY && dst_pix_fmt != PIX_FMT_GRAY8)) {
2202 /* gray8 is the normalized format */
2203 int_pix_fmt = PIX_FMT_GRAY8;
2204 } else if ((is_yuv_planar (src_pix) &&
2205 src_pix_fmt != PIX_FMT_YUV444P && src_pix_fmt != PIX_FMT_YUVJ444P)) {
2206 /* yuv444 is the normalized format */
2207 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2208 int_pix_fmt = PIX_FMT_YUVJ444P;
2210 int_pix_fmt = PIX_FMT_YUV444P;
2211 } else if ((is_yuv_planar (dst_pix) &&
2212 dst_pix_fmt != PIX_FMT_YUV444P && dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2213 /* yuv444 is the normalized format */
2214 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2215 int_pix_fmt = PIX_FMT_YUVJ444P;
2217 int_pix_fmt = PIX_FMT_YUV444P;
2219 /* the two formats are rgb or gray8 or yuv[j]444p */
2220 if (src_pix->is_alpha && dst_pix->is_alpha)
2221 int_pix_fmt = PIX_FMT_RGBA32;
2223 int_pix_fmt = PIX_FMT_RGB24;
2225 if (avpicture_alloc (tmp, int_pix_fmt, dst_width, dst_height) < 0)
2228 if (img_convert (tmp, int_pix_fmt,
2229 src, src_pix_fmt, src_width, src_height) < 0)
2231 if (img_convert (dst, dst_pix_fmt,
2232 tmp, int_pix_fmt, dst_width, dst_height) < 0)
2236 avpicture_free (tmp);
2240 /* NOTE: we scan all the pixels to have an exact information */
2242 get_alpha_info_pal8 (const AVPicture * src, int width, int height)
2244 const unsigned char *p;
2245 int src_wrap, ret, x, y;
2247 uint32_t *palette = (uint32_t *) src->data[1];
2250 src_wrap = src->linesize[0] - width;
2252 for (y = 0; y < height; y++) {
2253 for (x = 0; x < width; x++) {
2254 a = palette[p[0]] >> 24;
2256 ret |= FF_ALPHA_TRANSP;
2257 } else if (a != 0xff) {
2258 ret |= FF_ALPHA_SEMI_TRANSP;
2268 * Tell if an image really has transparent alpha values.
2269 * @return ored mask of FF_ALPHA_xxx constants
2272 img_get_alpha_info (const AVPicture * src, int pix_fmt, int width, int height)
2274 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
2277 pf = get_pix_fmt_info (pix_fmt);
2278 /* no alpha can be represented in format */
2283 ret = get_alpha_info_rgb32 (src, width, height);
2286 ret = get_alpha_info_bgr32 (src, width, height);
2288 case PIX_FMT_RGBA32:
2289 ret = get_alpha_info_rgba32 (src, width, height);
2291 case PIX_FMT_BGRA32:
2292 ret = get_alpha_info_bgra32 (src, width, height);
2294 case PIX_FMT_RGB555:
2295 ret = get_alpha_info_rgb555 (src, width, height);
2298 ret = get_alpha_info_pal8 (src, width, height);
2301 /* we do not know, so everything is indicated */
2302 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
2309 #define DEINT_INPLACE_LINE_LUM \
2310 movd_m2r(lum_m4[0],mm0);\
2311 movd_m2r(lum_m3[0],mm1);\
2312 movd_m2r(lum_m2[0],mm2);\
2313 movd_m2r(lum_m1[0],mm3);\
2314 movd_m2r(lum[0],mm4);\
2315 punpcklbw_r2r(mm7,mm0);\
2316 movd_r2m(mm2,lum_m4[0]);\
2317 punpcklbw_r2r(mm7,mm1);\
2318 punpcklbw_r2r(mm7,mm2);\
2319 punpcklbw_r2r(mm7,mm3);\
2320 punpcklbw_r2r(mm7,mm4);\
2321 paddw_r2r(mm3,mm1);\
2323 paddw_r2r(mm4,mm0);\
2325 paddw_r2r(mm6,mm2);\
2326 paddw_r2r(mm2,mm1);\
2327 psubusw_r2r(mm0,mm1);\
2329 packuswb_r2r(mm7,mm1);\
2330 movd_r2m(mm1,lum_m2[0]);
2332 #define DEINT_LINE_LUM \
2333 movd_m2r(lum_m4[0],mm0);\
2334 movd_m2r(lum_m3[0],mm1);\
2335 movd_m2r(lum_m2[0],mm2);\
2336 movd_m2r(lum_m1[0],mm3);\
2337 movd_m2r(lum[0],mm4);\
2338 punpcklbw_r2r(mm7,mm0);\
2339 punpcklbw_r2r(mm7,mm1);\
2340 punpcklbw_r2r(mm7,mm2);\
2341 punpcklbw_r2r(mm7,mm3);\
2342 punpcklbw_r2r(mm7,mm4);\
2343 paddw_r2r(mm3,mm1);\
2345 paddw_r2r(mm4,mm0);\
2347 paddw_r2r(mm6,mm2);\
2348 paddw_r2r(mm2,mm1);\
2349 psubusw_r2r(mm0,mm1);\
2351 packuswb_r2r(mm7,mm1);\
2352 movd_r2m(mm1,dst[0]);
2355 /* filter parameters: [-1 4 2 4 -1] // 8 */
2357 deinterlace_line (uint8_t * dst,
2358 const uint8_t * lum_m4, const uint8_t * lum_m3,
2359 const uint8_t * lum_m2, const uint8_t * lum_m1,
2360 const uint8_t * lum, int size)
2363 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2366 for (; size > 0; size--) {
2368 sum += lum_m3[0] << 2;
2369 sum += lum_m2[0] << 1;
2370 sum += lum_m1[0] << 2;
2372 dst[0] = cm[(sum + 4) >> 3];
2389 pxor_r2r (mm7, mm7);
2390 movq_m2r (rounder, mm6);
2392 for (; size > 3; size -= 4) {
2393 DEINT_LINE_LUM lum_m4 += 4;
2404 deinterlace_line_inplace (uint8_t * lum_m4, uint8_t * lum_m3, uint8_t * lum_m2,
2405 uint8_t * lum_m1, uint8_t * lum, int size)
2408 uint8_t *cm = cropTbl + MAX_NEG_CROP;
2411 for (; size > 0; size--) {
2413 sum += lum_m3[0] << 2;
2414 sum += lum_m2[0] << 1;
2415 lum_m4[0] = lum_m2[0];
2416 sum += lum_m1[0] << 2;
2418 lum_m2[0] = cm[(sum + 4) >> 3];
2434 pxor_r2r (mm7, mm7);
2435 movq_m2r (rounder, mm6);
2437 for (; size > 3; size -= 4) {
2438 DEINT_INPLACE_LINE_LUM lum_m4 += 4;
2448 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2449 top field is copied as is, but the bottom field is deinterlaced
2450 against the top field. */
2452 deinterlace_bottom_field (uint8_t * dst, int dst_wrap,
2453 const uint8_t * src1, int src_wrap, int width, int height)
2455 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
2460 src_0 = &src_m1[src_wrap];
2461 src_p1 = &src_0[src_wrap];
2462 src_p2 = &src_p1[src_wrap];
2463 for (y = 0; y < (height - 2); y += 2) {
2464 memcpy (dst, src_m1, width);
2466 deinterlace_line (dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
2470 src_p1 += 2 * src_wrap;
2471 src_p2 += 2 * src_wrap;
2474 memcpy (dst, src_m1, width);
2477 deinterlace_line (dst, src_m2, src_m1, src_0, src_0, src_0, width);
2481 deinterlace_bottom_field_inplace (uint8_t * src1, int src_wrap,
2482 int width, int height)
2484 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
2488 buf = (uint8_t *) av_malloc (width);
2491 memcpy (buf, src_m1, width);
2492 src_0 = &src_m1[src_wrap];
2493 src_p1 = &src_0[src_wrap];
2494 src_p2 = &src_p1[src_wrap];
2495 for (y = 0; y < (height - 2); y += 2) {
2496 deinterlace_line_inplace (buf, src_m1, src_0, src_p1, src_p2, width);
2499 src_p1 += 2 * src_wrap;
2500 src_p2 += 2 * src_wrap;
2503 deinterlace_line_inplace (buf, src_m1, src_0, src_0, src_0, width);
2508 /* deinterlace - if not supported return -1 */
2510 avpicture_deinterlace (AVPicture * dst, const AVPicture * src,
2511 int pix_fmt, int width, int height)
2515 if (pix_fmt != PIX_FMT_YUV420P &&
2516 pix_fmt != PIX_FMT_YUV422P &&
2517 pix_fmt != PIX_FMT_YUV444P && pix_fmt != PIX_FMT_YUV411P)
2519 if ((width & 3) != 0 || (height & 3) != 0)
2522 for (i = 0; i < 3; i++) {
2525 case PIX_FMT_YUV420P:
2529 case PIX_FMT_YUV422P:
2532 case PIX_FMT_YUV411P:
2540 deinterlace_bottom_field_inplace (dst->data[i], dst->linesize[i],
2543 deinterlace_bottom_field (dst->data[i], dst->linesize[i],
2544 src->data[i], src->linesize[i], width, height);