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_YVYU422] = */ {
134 /* .format = */ PIX_FMT_YVYU422,
135 /* .name = */ "yvyu422",
136 /* .nb_channels = */ 1,
137 /* .color_type = */ FF_COLOR_YUV,
138 /* .pixel_type = */ FF_PIXEL_PACKED,
140 /* .x_chroma_shift = */ 1,
141 /* .y_chroma_shift = */ 0,
144 /* [PIX_FMT_V308] = */ {
145 /* .format = */ PIX_FMT_V308,
146 /* .name = */ "v308",
147 /* .nb_channels = */ 1,
148 /* .color_type = */ FF_COLOR_YUV,
149 /* .pixel_type = */ FF_PIXEL_PACKED,
151 /* .x_chroma_shift = */ 0,
152 /* .y_chroma_shift = */ 0,
155 /* [PIX_FMT_YUV410P] = */ {
156 /* .format = */ PIX_FMT_YUV410P,
157 /* .name = */ "yuv410p",
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_YVU410P] = */ {
167 /* .format = */ PIX_FMT_YVU410P,
168 /* .name = */ "yvu410p",
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 = */ 2,
177 /* [PIX_FMT_YUV411P] = */ {
178 /* .format = */ PIX_FMT_YUV411P,
179 /* .name = */ "yuv411p",
180 /* .nb_channels = */ 3,
181 /* .color_type = */ FF_COLOR_YUV,
182 /* .pixel_type = */ FF_PIXEL_PLANAR,
184 /* .x_chroma_shift = */ 2,
185 /* .y_chroma_shift = */ 0,
190 /* [PIX_FMT_YUVJ420P] = */ {
191 /* .format = */ PIX_FMT_YUVJ420P,
192 /* .name = */ "yuvj420p",
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 = */ 1,
201 /* [PIX_FMT_YUVJ422P] = */ {
202 /* .format = */ PIX_FMT_YUVJ422P,
203 /* .name = */ "yuvj422p",
204 /* .nb_channels = */ 3,
205 /* .color_type = */ FF_COLOR_YUV_JPEG,
206 /* .pixel_type = */ FF_PIXEL_PLANAR,
208 /* .x_chroma_shift = */ 1,
209 /* .y_chroma_shift = */ 0,
212 /* [PIX_FMT_YUVJ444P] = */ {
213 /* .format = */ PIX_FMT_YUVJ444P,
214 /* .name = */ "yuvj444p",
215 /* .nb_channels = */ 3,
216 /* .color_type = */ FF_COLOR_YUV_JPEG,
217 /* .pixel_type = */ FF_PIXEL_PLANAR,
219 /* .x_chroma_shift = */ 0,
220 /* .y_chroma_shift = */ 0,
225 /* [PIX_FMT_RGB24] = */ {
226 /* .format = */ PIX_FMT_RGB24,
227 /* .name = */ "rgb24",
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_BGR24] = */ {
237 /* .format = */ PIX_FMT_BGR24,
238 /* .name = */ "bgr24",
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_RGB32] = */ {
248 /* .format = */ PIX_FMT_RGB32,
249 /* .name = */ "rgb32",
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_BGR32] = */ {
259 /* .format = */ PIX_FMT_BGR32,
260 /* .name = */ "bgr32",
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_RGB32] = */ {
270 /* .format = */ PIX_FMT_xRGB32,
271 /* .name = */ "xrgb32",
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_BGR32] = */ {
281 /* .format = */ PIX_FMT_BGRx32,
282 /* .name = */ "bgrx32",
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_RGBA32] = */ {
292 /* .format = */ PIX_FMT_RGBA32,
293 /* .name = */ "rgba32",
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_BGRA32] = */ {
303 /* .format = */ PIX_FMT_BGRA32,
304 /* .name = */ "bgra32",
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_ARGB32] = */ {
314 /* .format = */ PIX_FMT_ARGB32,
315 /* .name = */ "argb32",
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_ABGR32] = */ {
325 /* .format = */ PIX_FMT_ABGR32,
326 /* .name = */ "abgr32",
327 /* .nb_channels = */ 4,
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_RGB565] = */ {
336 /* .format = */ PIX_FMT_RGB565,
337 /* .name = */ "rgb565",
338 /* .nb_channels = */ 3,
339 /* .color_type = */ FF_COLOR_RGB,
340 /* .pixel_type = */ FF_PIXEL_PACKED,
342 /* .x_chroma_shift = */ 0,
343 /* .y_chroma_shift = */ 0,
346 /* [PIX_FMT_RGB555] = */ {
347 /* .format = */ PIX_FMT_RGB555,
348 /* .name = */ "rgb555",
349 /* .nb_channels = */ 4,
350 /* .color_type = */ FF_COLOR_RGB,
351 /* .pixel_type = */ FF_PIXEL_PACKED,
353 /* .x_chroma_shift = */ 0,
354 /* .y_chroma_shift = */ 0,
358 /* gray / mono formats */
359 /* [PIX_FMT_GRAY8] = */ {
360 /* .format = */ PIX_FMT_GRAY8,
361 /* .name = */ "gray",
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_GRAY16_L] = */ {
371 /* .format = */ PIX_FMT_GRAY16_L,
372 /* .name = */ "gray",
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,
381 /* [PIX_FMT_GRAY16_B] = */ {
382 /* .format = */ PIX_FMT_GRAY16_B,
383 /* .name = */ "gray",
384 /* .nb_channels = */ 1,
385 /* .color_type = */ FF_COLOR_GRAY,
386 /* .pixel_type = */ FF_PIXEL_PLANAR,
388 /* .x_chroma_shift = */ 0,
389 /* .y_chroma_shift = */ 0,
392 /* [PIX_FMT_MONOWHITE] = */ {
393 /* .format = */ PIX_FMT_MONOWHITE,
394 /* .name = */ "monow",
395 /* .nb_channels = */ 1,
396 /* .color_type = */ FF_COLOR_GRAY,
397 /* .pixel_type = */ FF_PIXEL_PLANAR,
399 /* .x_chroma_shift = */ 0,
400 /* .y_chroma_shift = */ 0,
403 /* [PIX_FMT_MONOBLACK] = */ {
404 /* .format = */ PIX_FMT_MONOBLACK,
405 /* .name = */ "monob",
406 /* .nb_channels = */ 1,
407 /* .color_type = */ FF_COLOR_GRAY,
408 /* .pixel_type = */ FF_PIXEL_PLANAR,
410 /* .x_chroma_shift = */ 0,
411 /* .y_chroma_shift = */ 0,
415 /* paletted formats */
416 /* [PIX_FMT_PAL8] = */ {
417 /* .format = */ PIX_FMT_PAL8,
418 /* .name = */ "pal8",
419 /* .nb_channels = */ 4,
420 /* .color_type = */ FF_COLOR_RGB,
421 /* .pixel_type = */ FF_PIXEL_PALETTE,
423 /* .x_chroma_shift = */ 0,
424 /* .y_chroma_shift = */ 0,
427 /* [PIX_FMT_XVMC_MPEG2_MC] = */ {
428 /* .format = */ PIX_FMT_XVMC_MPEG2_MC,
429 /* .name = */ "xvmcmc",
431 /* [PIX_FMT_XVMC_MPEG2_IDCT] = */ {
432 /* .format = */ PIX_FMT_XVMC_MPEG2_IDCT,
433 /* .name = */ "xvmcidct",
435 /* [PIX_FMT_UYVY411] = */ {
436 /* .format = */ PIX_FMT_UYVY411,
437 /* .name = */ "uyvy411",
438 /* .nb_channels = */ 1,
439 /* .color_type = */ FF_COLOR_YUV,
440 /* .pixel_type = */ FF_PIXEL_PACKED,
442 /* .x_chroma_shift = */ 2,
443 /* .y_chroma_shift = */ 0,
446 /* [PIX_FMT_AYUV4444] = */ {
447 /* .format = */ PIX_FMT_AYUV4444,
448 /* .name = */ "ayuv4444",
449 /* .nb_channels = */ 1,
450 /* .color_type = */ FF_COLOR_YUV,
451 /* .pixel_type = */ FF_PIXEL_PACKED,
453 /* .x_chroma_shift = */ 0,
454 /* .y_chroma_shift = */ 0,
460 /* returns NULL if not found */
461 /* undid static since this is also used in gstffmpegcodecmap.c */
463 get_pix_fmt_info (enum PixelFormat format)
467 for (i = 0; i < sizeof (pix_fmt_info) / sizeof (pix_fmt_info[0]); i++) {
468 if (pix_fmt_info[i].format == format) {
469 return pix_fmt_info + i;
473 /* since this doesn't get checked *anywhere*, we might as well warn
474 here if we return NULL so you have *some* idea what's going on */
476 ("Could not find info for pixel format %d out of %d known pixel formats. One segfault coming up",
482 avcodec_get_chroma_sub_sample (int pix_fmt, int *h_shift, int *v_shift)
484 *h_shift = get_pix_fmt_info (pix_fmt)->x_chroma_shift;
485 *v_shift = get_pix_fmt_info (pix_fmt)->y_chroma_shift;
489 avcodec_get_pix_fmt_name (int pix_fmt)
491 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
494 return get_pix_fmt_info (pix_fmt)->name;
498 avcodec_get_pix_fmt (const char *name)
502 for (i = 0; i < PIX_FMT_NB; i++)
503 if (!strcmp (pix_fmt_info[i].name, name))
505 return pix_fmt_info[i].format;
510 avpicture_layout (const AVPicture * src, int pix_fmt, int width, int height,
511 unsigned char *dest, int dest_size)
513 PixFmtInfo *pf = get_pix_fmt_info (pix_fmt);
514 int i, j, w, h, data_planes;
515 const unsigned char *s;
516 int size = avpicture_get_size (pix_fmt, width, height);
518 if (size > dest_size)
521 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
522 if (pix_fmt == PIX_FMT_YUV422 ||
523 pix_fmt == PIX_FMT_UYVY422 ||
524 pix_fmt == PIX_FMT_RGB565 || pix_fmt == PIX_FMT_RGB555)
526 else if (pix_fmt == PIX_FMT_UYVY411)
527 w = width + width / 2;
528 else if (pix_fmt == PIX_FMT_PAL8)
531 w = width * (pf->depth * pf->nb_channels / 8);
536 data_planes = pf->nb_channels;
537 w = (width * pf->depth + 7) / 8;
541 for (i = 0; i < data_planes; i++) {
543 w = width >> pf->x_chroma_shift;
544 h = height >> pf->y_chroma_shift;
547 for (j = 0; j < h; j++) {
550 s += src->linesize[i];
554 if (pf->pixel_type == FF_PIXEL_PALETTE)
555 memcpy ((unsigned char *) (((size_t) dest + 3) & ~3), src->data[1],
563 avpicture_get_size (int pix_fmt, int width, int height)
565 AVPicture dummy_pict;
567 return gst_ffmpegcsp_avpicture_fill (&dummy_pict, NULL, pix_fmt, width,
572 * compute the loss when converting from a pixel format to another
575 avcodec_get_pix_fmt_loss (int dst_pix_fmt, int src_pix_fmt, int has_alpha)
577 const PixFmtInfo *pf, *ps;
580 ps = get_pix_fmt_info (src_pix_fmt);
581 pf = get_pix_fmt_info (dst_pix_fmt);
585 if (pf->depth < ps->depth ||
586 (dst_pix_fmt == PIX_FMT_RGB555 && src_pix_fmt == PIX_FMT_RGB565))
587 loss |= FF_LOSS_DEPTH;
588 if (pf->x_chroma_shift > ps->x_chroma_shift ||
589 pf->y_chroma_shift > ps->y_chroma_shift)
590 loss |= FF_LOSS_RESOLUTION;
591 switch (pf->color_type) {
593 if (ps->color_type != FF_COLOR_RGB && ps->color_type != FF_COLOR_GRAY)
594 loss |= FF_LOSS_COLORSPACE;
597 if (ps->color_type != FF_COLOR_GRAY)
598 loss |= FF_LOSS_COLORSPACE;
601 if (ps->color_type != FF_COLOR_YUV)
602 loss |= FF_LOSS_COLORSPACE;
604 case FF_COLOR_YUV_JPEG:
605 if (ps->color_type != FF_COLOR_YUV_JPEG &&
606 ps->color_type != FF_COLOR_YUV && ps->color_type != FF_COLOR_GRAY)
607 loss |= FF_LOSS_COLORSPACE;
611 if (ps->color_type != pf->color_type)
612 loss |= FF_LOSS_COLORSPACE;
615 if (pf->color_type == FF_COLOR_GRAY && ps->color_type != FF_COLOR_GRAY)
616 loss |= FF_LOSS_CHROMA;
617 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
618 loss |= FF_LOSS_ALPHA;
619 if (pf->pixel_type == FF_PIXEL_PALETTE &&
620 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
621 loss |= FF_LOSS_COLORQUANT;
626 avg_bits_per_pixel (int pix_fmt)
629 const PixFmtInfo *pf;
631 pf = get_pix_fmt_info (pix_fmt);
632 switch (pf->pixel_type) {
633 case FF_PIXEL_PACKED:
636 case PIX_FMT_UYVY422:
637 case PIX_FMT_YVYU422:
642 case PIX_FMT_UYVY411:
646 bits = pf->depth * pf->nb_channels;
650 case FF_PIXEL_PLANAR:
651 if (pf->x_chroma_shift == 0 && pf->y_chroma_shift == 0) {
652 bits = pf->depth * pf->nb_channels;
654 bits = pf->depth + ((2 * pf->depth) >>
655 (pf->x_chroma_shift + pf->y_chroma_shift));
658 case FF_PIXEL_PALETTE:
669 avcodec_find_best_pix_fmt1 (int pix_fmt_mask,
670 int src_pix_fmt, int has_alpha, int loss_mask)
672 int dist, i, loss, min_dist, dst_pix_fmt;
674 /* find exact color match with smallest size */
676 min_dist = 0x7fffffff;
677 for (i = 0; i < PIX_FMT_NB; i++) {
678 if (pix_fmt_mask & (1 << i)) {
679 loss = avcodec_get_pix_fmt_loss (i, src_pix_fmt, has_alpha) & loss_mask;
681 dist = avg_bits_per_pixel (i);
682 if (dist < min_dist) {
693 * find best pixel format to convert to. Return -1 if none found
696 avcodec_find_best_pix_fmt (int pix_fmt_mask, int src_pix_fmt,
697 int has_alpha, int *loss_ptr)
699 int dst_pix_fmt, loss_mask, i;
700 static const int loss_mask_order[] = {
701 ~0, /* no loss first */
704 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
710 /* try with successive loss */
713 loss_mask = loss_mask_order[i++];
714 dst_pix_fmt = avcodec_find_best_pix_fmt1 (pix_fmt_mask, src_pix_fmt,
715 has_alpha, loss_mask);
716 if (dst_pix_fmt >= 0)
724 *loss_ptr = avcodec_get_pix_fmt_loss (dst_pix_fmt, src_pix_fmt, has_alpha);
729 img_copy_plane (uint8_t * dst, int dst_wrap,
730 const uint8_t * src, int src_wrap, int width, int height)
732 for (; height > 0; height--) {
733 memcpy (dst, src, width);
740 * Copy image 'src' to 'dst'.
743 img_copy (AVPicture * dst, const AVPicture * src,
744 int pix_fmt, int width, int height)
747 const PixFmtInfo *pf;
749 pf = get_pix_fmt_info (pix_fmt);
750 switch (pf->pixel_type) {
751 case FF_PIXEL_PACKED:
754 case PIX_FMT_UYVY422:
755 case PIX_FMT_YVYU422:
760 case PIX_FMT_UYVY411:
764 bits = pf->depth * pf->nb_channels;
767 bwidth = (width * bits + 7) >> 3;
768 img_copy_plane (dst->data[0], dst->linesize[0],
769 src->data[0], src->linesize[0], bwidth, height);
771 case FF_PIXEL_PLANAR:
772 for (i = 0; i < pf->nb_channels; i++) {
777 if (i == 1 || i == 2) {
778 w >>= pf->x_chroma_shift;
779 h >>= pf->y_chroma_shift;
781 bwidth = (w * pf->depth + 7) >> 3;
782 img_copy_plane (dst->data[i], dst->linesize[i],
783 src->data[i], src->linesize[i], bwidth, h);
786 case FF_PIXEL_PALETTE:
787 img_copy_plane (dst->data[0], dst->linesize[0],
788 src->data[0], src->linesize[0], width, height);
789 /* copy the palette */
790 img_copy_plane (dst->data[1], dst->linesize[1],
791 src->data[1], src->linesize[1], 4, 256);
796 /* XXX: totally non optimized */
799 yuv422_to_yuv420p (AVPicture * dst, const AVPicture * src,
800 int width, int height)
802 const uint8_t *p, *p1;
803 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
811 for (; height >= 1; height -= 2) {
816 for (w = width; w >= 2; w -= 2) {
833 p1 += src->linesize[0];
834 lum1 += dst->linesize[0];
838 for (w = width; w >= 2; w -= 2) {
847 p1 += src->linesize[0];
848 lum1 += dst->linesize[0];
850 cb1 += dst->linesize[1];
851 cr1 += dst->linesize[2];
856 uyvy422_to_gray (AVPicture * dst, const AVPicture * src, int width, int height)
858 const uint8_t *p, *p1;
864 for (; height > 0; height--) {
868 for (w = width; w >= 2; w -= 2) {
874 p1 += src->linesize[0];
875 lum1 += dst->linesize[0];
881 uyvy422_to_yuv420p (AVPicture * dst, const AVPicture * src,
882 int width, int height)
884 const uint8_t *p, *p1;
885 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
894 for (; height >= 1; height -= 2) {
899 for (w = width; w >= 2; w -= 2) {
916 p1 += src->linesize[0];
917 lum1 += dst->linesize[0];
921 for (w = width; w >= 2; w -= 2) {
930 p1 += src->linesize[0];
931 lum1 += dst->linesize[0];
933 cb1 += dst->linesize[1];
934 cr1 += dst->linesize[2];
940 uyvy422_to_yuv422p (AVPicture * dst, const AVPicture * src,
941 int width, int height)
943 const uint8_t *p, *p1;
944 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
951 for (; height > 0; height--) {
956 for (w = width; w >= 2; w -= 2) {
966 p1 += src->linesize[0];
967 lum1 += dst->linesize[0];
968 cb1 += dst->linesize[1];
969 cr1 += dst->linesize[2];
974 yvyu422_to_gray (AVPicture * dst, const AVPicture * src, int width, int height)
976 const uint8_t *p, *p1;
982 for (; height > 0; height--) {
986 for (w = width; w >= 2; w -= 2) {
992 p1 += src->linesize[0];
993 lum1 += dst->linesize[0];
999 yvyu422_to_yuv420p (AVPicture * dst, const AVPicture * src,
1000 int width, int height)
1002 const uint8_t *p, *p1;
1003 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1008 lum1 = dst->data[0];
1012 for (; height >= 1; height -= 2) {
1017 for (w = width; w >= 2; w -= 2) {
1034 p1 += src->linesize[0];
1035 lum1 += dst->linesize[0];
1039 for (w = width; w >= 2; w -= 2) {
1048 p1 += src->linesize[0];
1049 lum1 += dst->linesize[0];
1051 cb1 += dst->linesize[1];
1052 cr1 += dst->linesize[2];
1057 yvyu422_to_yuv422p (AVPicture * dst, const AVPicture * src,
1058 int width, int height)
1060 const uint8_t *p, *p1;
1061 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1065 lum1 = dst->data[0];
1068 for (; height > 0; height--) {
1073 for (w = width; w >= 2; w -= 2) {
1083 p1 += src->linesize[0];
1084 lum1 += dst->linesize[0];
1085 cb1 += dst->linesize[1];
1086 cr1 += dst->linesize[2];
1091 yuv422_to_yuv422p (AVPicture * dst, const AVPicture * src,
1092 int width, int height)
1094 const uint8_t *p, *p1;
1095 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1099 lum1 = dst->data[0];
1102 for (; height > 0; height--) {
1107 for (w = width; w >= 2; w -= 2) {
1117 p1 += src->linesize[0];
1118 lum1 += dst->linesize[0];
1119 cb1 += dst->linesize[1];
1120 cr1 += dst->linesize[2];
1125 yuv422p_to_yuv422 (AVPicture * dst, const AVPicture * src,
1126 int width, int height)
1129 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1133 lum1 = src->data[0];
1136 for (; height > 0; height--) {
1141 for (w = width; w >= 2; w -= 2) {
1155 p1 += dst->linesize[0];
1156 lum1 += src->linesize[0];
1157 cb1 += src->linesize[1];
1158 cr1 += src->linesize[2];
1163 yuv422p_to_uyvy422 (AVPicture * dst, const AVPicture * src,
1164 int width, int height)
1167 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1171 lum1 = src->data[0];
1174 for (; height > 0; height--) {
1179 for (w = width; w >= 2; w -= 2) {
1189 p1 += dst->linesize[0];
1190 lum1 += src->linesize[0];
1191 cb1 += src->linesize[1];
1192 cr1 += src->linesize[2];
1197 yuv422p_to_yvyu422 (AVPicture * dst, const AVPicture * src,
1198 int width, int height)
1201 const uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1205 lum1 = src->data[0];
1208 for (; height > 0; height--) {
1213 for (w = width; w >= 2; w -= 2) {
1223 p1 += dst->linesize[0];
1224 lum1 += src->linesize[0];
1225 cb1 += src->linesize[1];
1226 cr1 += src->linesize[2];
1231 uyvy411_to_yuv411p (AVPicture * dst, const AVPicture * src,
1232 int width, int height)
1234 const uint8_t *p, *p1;
1235 uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
1239 lum1 = dst->data[0];
1242 for (; height > 0; height--) {
1247 for (w = width; w >= 4; w -= 4) {
1259 p1 += src->linesize[0];
1260 lum1 += dst->linesize[0];
1261 cb1 += dst->linesize[1];
1262 cr1 += dst->linesize[2];
1267 yuv420p_to_yuv422 (AVPicture * dst, const AVPicture * src,
1268 int width, int height)
1271 uint8_t *line1, *line2, *linesrc = dst->data[0];
1272 uint8_t *lum1, *lum2, *lumsrc = src->data[0];
1273 uint8_t *cb1, *cb2 = src->data[1];
1274 uint8_t *cr1, *cr2 = src->data[2];
1276 for (h = height / 2; h--;) {
1278 line2 = linesrc + dst->linesize[0];
1281 lum2 = lumsrc + src->linesize[0];
1286 for (w = width / 2; w--;) {
1289 *line1++ = *line2++ = *cb1++;
1292 *line1++ = *line2++ = *cr1++;
1295 if (width % 2 != 0) {
1298 *line1++ = *line2++ = *cb1++;
1301 linesrc += dst->linesize[0] * 2;
1302 lumsrc += src->linesize[0] * 2;
1303 cb2 += src->linesize[1];
1304 cr2 += src->linesize[2];
1307 if (height % 2 != 0) {
1313 for (w = width / 2; w--;) {
1320 if (width % 2 != 0) {
1328 nv12_to_nv21 (AVPicture * dst, const AVPicture * src, int width, int height)
1330 const uint8_t *s_c_ptr;
1334 s_c_ptr = src->data[1];
1335 d_c_ptr = dst->data[1];
1337 for (; height >= 2; height -= 2) {
1338 for (w = width; w >= 2; w -= 2) {
1339 d_c_ptr[0] = s_c_ptr[1];
1340 d_c_ptr[1] = s_c_ptr[0];
1345 /* handle odd width */
1347 d_c_ptr[0] = s_c_ptr[1];
1348 d_c_ptr[1] = s_c_ptr[0];
1352 s_c_ptr += src->linesize[1] - width;
1353 d_c_ptr += src->linesize[1] - width;
1355 /* handle odd height */
1357 for (w = width; w >= 2; w -= 2) {
1358 d_c_ptr[0] = s_c_ptr[1];
1359 d_c_ptr[1] = s_c_ptr[0];
1364 /* handle odd width */
1366 d_c_ptr[0] = s_c_ptr[1];
1367 d_c_ptr[1] = s_c_ptr[0];
1374 #define nv21_to_nv12 nv12_to_nv21
1376 #define SCALEBITS 10
1377 #define ONE_HALF (1 << (SCALEBITS - 1))
1378 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1380 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1384 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1385 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1387 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1390 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1392 y = ((y1) - 16) * FIX(255.0/219.0);\
1393 r = cm[(y + r_add) >> SCALEBITS];\
1394 g = cm[(y + g_add) >> SCALEBITS];\
1395 b = cm[(y + b_add) >> SCALEBITS];\
1398 #define YUV_TO_RGB1(cb1, cr1)\
1402 r_add = FIX(1.40200) * cr + ONE_HALF;\
1403 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1404 b_add = FIX(1.77200) * cb + ONE_HALF;\
1407 #define YUV_TO_RGB2(r, g, b, y1)\
1409 y = (y1) << SCALEBITS;\
1410 r = cm[(y + r_add) >> SCALEBITS];\
1411 g = cm[(y + g_add) >> SCALEBITS];\
1412 b = cm[(y + b_add) >> SCALEBITS];\
1415 #define Y_CCIR_TO_JPEG(y)\
1416 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1418 #define Y_JPEG_TO_CCIR(y)\
1419 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1421 #define C_CCIR_TO_JPEG(y)\
1422 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1424 /* NOTE: the clamp is really necessary! */
1426 C_JPEG_TO_CCIR (int y)
1428 y = (((y - 128) * FIX (112.0 / 127.0) + (ONE_HALF +
1429 (128 << SCALEBITS))) >> SCALEBITS);
1436 #define RGB_TO_Y(r, g, b) \
1437 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1438 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1440 #define RGB_TO_U(r1, g1, b1, shift)\
1441 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1442 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1444 #define RGB_TO_V(r1, g1, b1, shift)\
1445 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1446 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1448 #define RGB_TO_Y_CCIR(r, g, b) \
1449 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1450 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1452 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1453 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1454 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1456 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1457 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1458 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1460 static uint8_t y_ccir_to_jpeg[256];
1461 static uint8_t y_jpeg_to_ccir[256];
1462 static uint8_t c_ccir_to_jpeg[256];
1463 static uint8_t c_jpeg_to_ccir[256];
1465 /* init various conversion tables */
1467 img_convert_init (void)
1470 uint8_t *cm = cropTbl + MAX_NEG_CROP;
1472 for (i = 0; i < 256; i++) {
1473 y_ccir_to_jpeg[i] = Y_CCIR_TO_JPEG (i);
1474 y_jpeg_to_ccir[i] = Y_JPEG_TO_CCIR (i);
1475 c_ccir_to_jpeg[i] = C_CCIR_TO_JPEG (i);
1476 c_jpeg_to_ccir[i] = C_JPEG_TO_CCIR (i);
1480 /* apply to each pixel the given table */
1482 img_apply_table (uint8_t * dst, int dst_wrap,
1483 const uint8_t * src, int src_wrap,
1484 int width, int height, const uint8_t * table1)
1489 const uint8_t *table;
1492 for (; height > 0; height--) {
1516 /* XXX: use generic filter ? */
1517 /* XXX: in most cases, the sampling position is incorrect */
1521 shrink41 (uint8_t * dst, int dst_wrap,
1522 const uint8_t * src, int src_wrap, int width, int height)
1528 for (; height > 0; height--) {
1531 for (w = width; w > 0; w--) {
1532 d[0] = (s[0] + s[1] + s[2] + s[3] + 2) >> 2;
1543 shrink21 (uint8_t * dst, int dst_wrap,
1544 const uint8_t * src, int src_wrap, int width, int height)
1550 for (; height > 0; height--) {
1553 for (w = width; w > 0; w--) {
1554 d[0] = (s[0] + s[1]) >> 1;
1565 shrink12 (uint8_t * dst, int dst_wrap,
1566 const uint8_t * src, int src_wrap, int width, int height)
1570 const uint8_t *s1, *s2;
1572 for (; height > 0; height--) {
1576 for (w = width; w >= 4; w -= 4) {
1577 d[0] = (s1[0] + s2[0]) >> 1;
1578 d[1] = (s1[1] + s2[1]) >> 1;
1579 d[2] = (s1[2] + s2[2]) >> 1;
1580 d[3] = (s1[3] + s2[3]) >> 1;
1585 for (; w > 0; w--) {
1586 d[0] = (s1[0] + s2[0]) >> 1;
1591 src += 2 * src_wrap;
1598 shrink22 (uint8_t * dst, int dst_wrap,
1599 const uint8_t * src, int src_wrap, int width, int height)
1602 const uint8_t *s1, *s2;
1605 for (; height > 0; height--) {
1609 for (w = width; w >= 4; w -= 4) {
1610 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1611 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
1612 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
1613 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
1618 for (; w > 0; w--) {
1619 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
1624 src += 2 * src_wrap;
1631 shrink44 (uint8_t * dst, int dst_wrap,
1632 const uint8_t * src, int src_wrap, int width, int height)
1635 const uint8_t *s1, *s2, *s3, *s4;
1638 for (; height > 0; height--) {
1644 for (w = width; w > 0; w--) {
1645 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
1646 s2[0] + s2[1] + s2[2] + s2[3] +
1647 s3[0] + s3[1] + s3[2] + s3[3] +
1648 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
1655 src += 4 * src_wrap;
1661 grow21_line (uint8_t * dst, const uint8_t * src, int width)
1669 for (w = width; w >= 4; w -= 4) {
1670 d[1] = d[0] = s1[0];
1671 d[3] = d[2] = s1[1];
1675 for (; w >= 2; w -= 2) {
1676 d[1] = d[0] = s1[0];
1680 /* only needed if width is not a multiple of two */
1681 /* XXX: veryfy that */
1688 grow41_line (uint8_t * dst, const uint8_t * src, int width)
1696 for (w = width; w >= 4; w -= 4) {
1709 grow21 (uint8_t * dst, int dst_wrap,
1710 const uint8_t * src, int src_wrap, int width, int height)
1712 for (; height > 0; height--) {
1713 grow21_line (dst, src, width);
1721 grow22 (uint8_t * dst, int dst_wrap,
1722 const uint8_t * src, int src_wrap, int width, int height)
1724 for (; height > 0; height--) {
1725 grow21_line (dst, src, width);
1734 grow41 (uint8_t * dst, int dst_wrap,
1735 const uint8_t * src, int src_wrap, int width, int height)
1737 for (; height > 0; height--) {
1738 grow41_line (dst, src, width);
1746 grow44 (uint8_t * dst, int dst_wrap,
1747 const uint8_t * src, int src_wrap, int width, int height)
1749 for (; height > 0; height--) {
1750 grow41_line (dst, src, width);
1751 if ((height & 3) == 1)
1759 conv411 (uint8_t * dst, int dst_wrap,
1760 const uint8_t * src, int src_wrap, int width, int height)
1763 const uint8_t *s1, *s2;
1768 for (; height > 0; height--) {
1770 s2 = src + src_wrap;
1772 for (w = width; w > 0; w--) {
1773 c = (s1[0] + s2[0]) >> 1;
1780 src += src_wrap * 2;
1785 /* XXX: add jpeg quantize code */
1787 #define TRANSP_INDEX (6*6*6)
1789 /* this is maybe slow, but allows for extensions */
1790 static inline unsigned char
1791 gif_clut_index (uint8_t r, uint8_t g, uint8_t b)
1793 return ((((r) / 47) % 6) * 6 * 6 + (((g) / 47) % 6) * 6 + (((b) / 47) % 6));
1797 build_rgb_palette (uint8_t * palette, int has_alpha)
1800 static const uint8_t pal_value[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1803 pal = (uint32_t *) palette;
1805 for (r = 0; r < 6; r++) {
1806 for (g = 0; g < 6; g++) {
1807 for (b = 0; b < 6; b++) {
1808 pal[i++] = (0xffU << 24) | (pal_value[r] << 16) |
1809 (pal_value[g] << 8) | pal_value[b];
1816 pal[i++] = 0xff000000;
1819 /* copy bit n to bits 0 ... n - 1 */
1820 static inline unsigned int
1821 bitcopy_n (unsigned int a, int n)
1825 mask = (1 << n) - 1;
1826 return (a & (0xff & ~mask)) | ((-((a >> n) & 1)) & mask);
1829 /* rgb555 handling */
1831 #define RGB_NAME rgb555
1833 #define RGB_IN(r, g, b, s)\
1835 unsigned int v = ((const uint16_t *)(s))[0];\
1836 r = bitcopy_n(v >> (10 - 3), 3);\
1837 g = bitcopy_n(v >> (5 - 3), 3);\
1838 b = bitcopy_n(v << 3, 3);\
1841 #define RGBA_IN(r, g, b, a, s)\
1843 unsigned int v = ((const uint16_t *)(s))[0];\
1844 r = bitcopy_n(v >> (10 - 3), 3);\
1845 g = bitcopy_n(v >> (5 - 3), 3);\
1846 b = bitcopy_n(v << 3, 3);\
1847 a = (-(v >> 15)) & 0xff;\
1850 #define RGBA_OUT(d, r, g, b, a)\
1852 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3) | \
1853 ((a << 8) & 0x8000);\
1858 #include "imgconvert_template.h"
1860 /* rgb565 handling */
1862 #define RGB_NAME rgb565
1864 #define RGB_IN(r, g, b, s)\
1866 unsigned int v = ((const uint16_t *)(s))[0];\
1867 r = bitcopy_n(v >> (11 - 3), 3);\
1868 g = bitcopy_n(v >> (5 - 2), 2);\
1869 b = bitcopy_n(v << 3, 3);\
1872 #define RGB_OUT(d, r, g, b)\
1874 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1879 #include "imgconvert_template.h"
1881 /* bgr24 handling */
1883 #define RGB_NAME bgr24
1885 #define RGB_IN(r, g, b, s)\
1892 #define RGB_OUT(d, r, g, b)\
1901 #include "imgconvert_template.h"
1907 /* rgb24 handling */
1909 #define RGB_NAME rgb24
1912 #define RGB_IN(r, g, b, s)\
1919 #define RGB_OUT(d, r, g, b)\
1928 #include "imgconvert_template.h"
1930 /* rgb32 handling */
1932 #define RGB_NAME rgb32
1935 #define RGB_IN(r, g, b, s)\
1937 unsigned int v = ((const uint32_t *)(s))[0];\
1938 r = (v >> 16) & 0xff;\
1939 g = (v >> 8) & 0xff;\
1943 #define RGBA_IN(r, g, b, a, s)\
1945 unsigned int v = ((const uint32_t *)(s))[0];\
1947 r = (v >> 16) & 0xff;\
1948 g = (v >> 8) & 0xff;\
1952 #define RGBA_OUT(d, r, g, b, a)\
1954 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1959 #include "imgconvert_template.h"
1961 /* bgr32 handling */
1963 #define RGB_NAME bgr32
1965 #define RGB_IN(r, g, b, s)\
1967 unsigned int v = ((const uint32_t *)(s))[0];\
1968 r = (v >> 8) & 0xff;\
1969 g = (v >> 16) & 0xff;\
1970 b = (v >> 24) & 0xff;\
1973 #define RGBA_IN(r, g, b, a, s)\
1975 unsigned int v = ((const uint32_t *)(s))[0];\
1977 r = (v >> 8) & 0xff;\
1978 g = (v >> 16) & 0xff;\
1979 b = (v >> 24) & 0xff;\
1982 #define RGBA_OUT(d, r, g, b, a)\
1984 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24);\
1989 #include "imgconvert_template.h"
1991 /* xrgb32 handling */
1993 #define RGB_NAME xrgb32
1996 #define RGB_IN(r, g, b, s)\
1998 unsigned int v = ((const uint32_t *)(s))[0];\
1999 r = (v >> 24) & 0xff;\
2000 g = (v >> 16) & 0xff;\
2001 b = (v >> 8) & 0xff;\
2004 #define RGBA_IN(r, g, b, a, s)\
2006 unsigned int v = ((const uint32_t *)(s))[0];\
2008 r = (v >> 24) & 0xff;\
2009 g = (v >> 16) & 0xff;\
2010 b = (v >> 8) & 0xff;\
2013 #define RGBA_OUT(d, r, g, b, a)\
2015 ((uint32_t *)(d))[0] = (r << 24) | (g << 16) | (b << 8) | a;\
2020 #include "imgconvert_template.h"
2022 /* bgrx32 handling */
2024 #define RGB_NAME bgrx32
2026 #define RGB_IN(r, g, b, s)\
2028 unsigned int v = ((const uint32_t *)(s))[0];\
2030 g = (v >> 8) & 0xff;\
2031 b = (v >> 16) & 0xff;\
2034 #define RGBA_IN(r, g, b, a, s)\
2036 unsigned int v = ((const uint32_t *)(s))[0];\
2039 g = (v >> 8) & 0xff;\
2040 b = (v >> 16) & 0xff;\
2043 #define RGBA_OUT(d, r, g, b, a)\
2045 ((uint32_t *)(d))[0] = r | (g << 8) | (b << 16) | (a << 24);\
2050 #include "imgconvert_template.h"
2052 /* rgba32 handling */
2054 #define RGB_NAME rgba32
2057 #define RGB_IN(r, g, b, s)\
2059 unsigned int v = ((const uint32_t *)(s))[0];\
2060 r = (v >> 16) & 0xff;\
2061 g = (v >> 8) & 0xff;\
2065 #define RGBA_IN(r, g, b, a, s)\
2067 unsigned int v = ((const uint32_t *)(s))[0];\
2068 a = (v >> 24) & 0xff;\
2069 r = (v >> 16) & 0xff;\
2070 g = (v >> 8) & 0xff;\
2074 #define RGBA_OUT(d, r, g, b, a)\
2076 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
2081 #include "imgconvert_template.h"
2083 /* bgra32 handling */
2085 #define RGB_NAME bgra32
2088 #define RGB_IN(r, g, b, s)\
2090 unsigned int v = ((const uint32_t *)(s))[0];\
2091 r = (v >> 8) & 0xff;\
2092 g = (v >> 16) & 0xff;\
2093 b = (v >> 24) & 0xff;\
2096 #define RGBA_IN(r, g, b, a, s)\
2098 unsigned int v = ((const uint32_t *)(s))[0];\
2100 r = (v >> 8) & 0xff;\
2101 g = (v >> 16) & 0xff;\
2102 b = (v >> 24) & 0xff;\
2105 #define RGBA_OUT(d, r, g, b, a)\
2107 ((uint32_t *)(d))[0] = a | (r << 8) | (g << 16) | (b << 24 );\
2112 #include "imgconvert_template.h"
2114 /* argb32 handling */
2116 #define RGB_NAME argb32
2119 #define RGB_IN(r, g, b, s)\
2121 unsigned int v = ((const uint32_t *)(s))[0];\
2122 r = (v >> 24) & 0xff;\
2123 g = (v >> 16) & 0xff;\
2124 b = (v >> 8) & 0xff;\
2127 #define RGBA_IN(r, g, b, a, s)\
2129 unsigned int v = ((const uint32_t *)(s))[0];\
2130 r = (v >> 24) & 0xff;\
2131 g = (v >> 16) & 0xff;\
2132 b = (v >> 8) & 0xff;\
2136 #define RGBA_OUT(d, r, g, b, a)\
2138 ((uint32_t *)(d))[0] = (r << 24) | (g << 16) | (b << 8) | a;\
2143 #include "imgconvert_template.h"
2145 /* abgr32 handling */
2147 #define RGB_NAME abgr32
2150 #define RGB_IN(r, g, b, s)\
2152 unsigned int v = ((const uint32_t *)(s))[0];\
2154 g = (v >> 8) & 0xff;\
2155 b = (v >> 16) & 0xff;\
2158 #define RGBA_IN(r, g, b, a, s)\
2160 unsigned int v = ((const uint32_t *)(s))[0];\
2162 g = (v >> 8) & 0xff;\
2163 b = (v >> 16) & 0xff;\
2164 a = (v >> 24) & 0xff;\
2167 #define RGBA_OUT(d, r, g, b, a)\
2169 ((uint32_t *)(d))[0] = r | (g << 8) | (b << 16) | (a << 24 );\
2174 #include "imgconvert_template.h"
2177 gray_to_gray16_l (AVPicture * dst, const AVPicture * src, int width, int height)
2179 const unsigned char *p;
2181 int dst_wrap, src_wrap;
2185 src_wrap = src->linesize[0] - width;
2188 dst_wrap = dst->linesize[0] - 2 * width;
2190 for (y = 0; y < height; y++) {
2191 for (x = 0; x < width; x++) {
2192 GST_WRITE_UINT16_LE (q, (*p << 8));
2202 gray_to_gray16_b (AVPicture * dst, const AVPicture * src, int width, int height)
2204 const unsigned char *p;
2206 int dst_wrap, src_wrap;
2210 src_wrap = src->linesize[0] - width;
2213 dst_wrap = dst->linesize[0] - 2 * width;
2215 for (y = 0; y < height; y++) {
2216 for (x = 0; x < width; x++) {
2217 GST_WRITE_UINT16_BE (q, (*p << 8));
2227 gray16_l_to_gray (AVPicture * dst, const AVPicture * src, int width, int height)
2229 const unsigned char *p;
2231 int dst_wrap, src_wrap;
2235 src_wrap = src->linesize[0] - 2 * width;
2238 dst_wrap = dst->linesize[0] - width;
2240 for (y = 0; y < height; y++) {
2241 for (x = 0; x < width; x++) {
2242 q[0] = GST_READ_UINT16_LE (p) >> 8;
2252 gray16_b_to_gray (AVPicture * dst, const AVPicture * src, int width, int height)
2254 const unsigned char *p;
2256 int dst_wrap, src_wrap;
2260 src_wrap = src->linesize[0] - 2 * width;
2263 dst_wrap = dst->linesize[0] - width;
2265 for (y = 0; y < height; y++) {
2266 for (x = 0; x < width; x++) {
2267 q[0] = GST_READ_UINT16_BE (p) >> 8;
2277 gray16_b_to_gray16_l (AVPicture * dst, const AVPicture * src,
2278 int width, int height)
2280 const unsigned char *p;
2282 int dst_wrap, src_wrap;
2286 src_wrap = src->linesize[0] - 2 * width;
2289 dst_wrap = dst->linesize[0] - 2 * width;
2291 for (y = 0; y < height; y++) {
2292 for (x = 0; x < width; x++) {
2304 mono_to_gray (AVPicture * dst, const AVPicture * src,
2305 int width, int height, int xor_mask)
2307 const unsigned char *p;
2309 int v, dst_wrap, src_wrap;
2313 src_wrap = src->linesize[0] - ((width + 7) >> 3);
2316 dst_wrap = dst->linesize[0] - width;
2317 for (y = 0; y < height; y++) {
2320 v = *p++ ^ xor_mask;
2322 q[1] = -((v >> 6) & 1);
2323 q[2] = -((v >> 5) & 1);
2324 q[3] = -((v >> 4) & 1);
2325 q[4] = -((v >> 3) & 1);
2326 q[5] = -((v >> 2) & 1);
2327 q[6] = -((v >> 1) & 1);
2328 q[7] = -((v >> 0) & 1);
2333 v = *p++ ^ xor_mask;
2335 q[0] = -((v >> 7) & 1);
2346 monowhite_to_gray (AVPicture * dst, const AVPicture * src,
2347 int width, int height)
2349 mono_to_gray (dst, src, width, height, 0xff);
2353 monoblack_to_gray (AVPicture * dst, const AVPicture * src,
2354 int width, int height)
2356 mono_to_gray (dst, src, width, height, 0x00);
2360 gray_to_mono (AVPicture * dst, const AVPicture * src,
2361 int width, int height, int xor_mask)
2366 int j, b, v, n1, src_wrap, dst_wrap, y;
2369 src_wrap = src->linesize[0] - width;
2372 dst_wrap = dst->linesize[0] - ((width + 7) >> 3);
2374 for (y = 0; y < height; y++) {
2378 for (j = 0; j < 8; j++) {
2381 v = (v << 1) | (b >> 7);
2383 d[0] = v ^ xor_mask;
2393 v = (v << 1) | (b >> 7);
2396 d[0] = (v << (8 - (n1 & 7))) ^ xor_mask;
2405 gray_to_monowhite (AVPicture * dst, const AVPicture * src,
2406 int width, int height)
2408 gray_to_mono (dst, src, width, height, 0xff);
2412 gray_to_monoblack (AVPicture * dst, const AVPicture * src,
2413 int width, int height)
2415 gray_to_mono (dst, src, width, height, 0x00);
2418 typedef struct ConvertEntry
2420 enum PixelFormat src;
2421 enum PixelFormat dest;
2422 void (*convert) (AVPicture * dst,
2423 const AVPicture * src, int width, int height);
2426 /* Add each new convertion function in this table. In order to be able
2427 to convert from any format to any format, the following constraints
2430 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
2432 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
2434 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGBA32
2436 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
2439 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
2441 The other conversion functions are just optimisations for common cases.
2443 static ConvertEntry convert_table[] = {
2444 {PIX_FMT_YUV420P, PIX_FMT_YUV422, yuv420p_to_yuv422},
2445 {PIX_FMT_YUV420P, PIX_FMT_RGB555, yuv420p_to_rgb555},
2446 {PIX_FMT_YUV420P, PIX_FMT_RGB565, yuv420p_to_rgb565},
2447 {PIX_FMT_YUV420P, PIX_FMT_BGR24, yuv420p_to_bgr24},
2448 {PIX_FMT_YUV420P, PIX_FMT_RGB24, yuv420p_to_rgb24},
2449 {PIX_FMT_YUV420P, PIX_FMT_RGB32, yuv420p_to_rgb32},
2450 {PIX_FMT_YUV420P, PIX_FMT_BGR32, yuv420p_to_bgr32},
2451 {PIX_FMT_YUV420P, PIX_FMT_xRGB32, yuv420p_to_xrgb32},
2452 {PIX_FMT_YUV420P, PIX_FMT_BGRx32, yuv420p_to_bgrx32},
2453 {PIX_FMT_YUV420P, PIX_FMT_RGBA32, yuv420p_to_rgba32},
2454 {PIX_FMT_YUV420P, PIX_FMT_BGRA32, yuv420p_to_bgra32},
2455 {PIX_FMT_YUV420P, PIX_FMT_ARGB32, yuv420p_to_argb32},
2456 {PIX_FMT_YUV420P, PIX_FMT_ABGR32, yuv420p_to_abgr32},
2458 {PIX_FMT_NV12, PIX_FMT_RGB555, nv12_to_rgb555},
2459 {PIX_FMT_NV12, PIX_FMT_RGB565, nv12_to_rgb565},
2460 {PIX_FMT_NV12, PIX_FMT_BGR24, nv12_to_bgr24},
2461 {PIX_FMT_NV12, PIX_FMT_RGB24, nv12_to_rgb24},
2462 {PIX_FMT_NV12, PIX_FMT_RGB32, nv12_to_rgb32},
2463 {PIX_FMT_NV12, PIX_FMT_BGR32, nv12_to_bgr32},
2464 {PIX_FMT_NV12, PIX_FMT_xRGB32, nv12_to_xrgb32},
2465 {PIX_FMT_NV12, PIX_FMT_BGRx32, nv12_to_bgrx32},
2466 {PIX_FMT_NV12, PIX_FMT_RGBA32, nv12_to_rgba32},
2467 {PIX_FMT_NV12, PIX_FMT_BGRA32, nv12_to_bgra32},
2468 {PIX_FMT_NV12, PIX_FMT_ARGB32, nv12_to_argb32},
2469 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv12_to_abgr32},
2470 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv12_to_nv21},
2472 {PIX_FMT_NV21, PIX_FMT_RGB555, nv21_to_rgb555},
2473 {PIX_FMT_NV21, PIX_FMT_RGB565, nv21_to_rgb565},
2474 {PIX_FMT_NV21, PIX_FMT_BGR24, nv21_to_bgr24},
2475 {PIX_FMT_NV21, PIX_FMT_RGB24, nv21_to_rgb24},
2476 {PIX_FMT_NV21, PIX_FMT_RGB32, nv21_to_rgb32},
2477 {PIX_FMT_NV21, PIX_FMT_BGR32, nv21_to_bgr32},
2478 {PIX_FMT_NV21, PIX_FMT_xRGB32, nv21_to_xrgb32},
2479 {PIX_FMT_NV21, PIX_FMT_BGRx32, nv21_to_bgrx32},
2480 {PIX_FMT_NV21, PIX_FMT_RGBA32, nv21_to_rgba32},
2481 {PIX_FMT_NV21, PIX_FMT_BGRA32, nv21_to_bgra32},
2482 {PIX_FMT_NV21, PIX_FMT_ARGB32, nv21_to_argb32},
2483 {PIX_FMT_NV21, PIX_FMT_ABGR32, nv21_to_abgr32},
2484 {PIX_FMT_NV12, PIX_FMT_ABGR32, nv21_to_nv12},
2486 {PIX_FMT_YUV422P, PIX_FMT_YUV422, yuv422p_to_yuv422},
2487 {PIX_FMT_YUV422P, PIX_FMT_UYVY422, yuv422p_to_uyvy422},
2488 {PIX_FMT_YUV422P, PIX_FMT_YVYU422, yuv422p_to_yvyu422},
2490 {PIX_FMT_YUV444P, PIX_FMT_RGB24, yuv444p_to_rgb24},
2492 {PIX_FMT_YUVJ420P, PIX_FMT_RGB555, yuvj420p_to_rgb555},
2493 {PIX_FMT_YUVJ420P, PIX_FMT_RGB565, yuvj420p_to_rgb565},
2494 {PIX_FMT_YUVJ420P, PIX_FMT_BGR24, yuvj420p_to_bgr24},
2495 {PIX_FMT_YUVJ420P, PIX_FMT_RGB24, yuvj420p_to_rgb24},
2496 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_rgb32},
2497 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgr32},
2498 {PIX_FMT_YUVJ420P, PIX_FMT_RGB32, yuvj420p_to_xrgb32},
2499 {PIX_FMT_YUVJ420P, PIX_FMT_BGR32, yuvj420p_to_bgrx32},
2500 {PIX_FMT_YUVJ420P, PIX_FMT_RGBA32, yuvj420p_to_rgba32},
2501 {PIX_FMT_YUVJ420P, PIX_FMT_BGRA32, yuvj420p_to_bgra32},
2502 {PIX_FMT_YUVJ420P, PIX_FMT_ARGB32, yuvj420p_to_argb32},
2503 {PIX_FMT_YUVJ420P, PIX_FMT_ABGR32, yuvj420p_to_abgr32},
2505 {PIX_FMT_YUVJ444P, PIX_FMT_RGB24, yuvj444p_to_rgb24},
2507 {PIX_FMT_YUV422, PIX_FMT_YUV420P, yuv422_to_yuv420p},
2508 {PIX_FMT_YUV422, PIX_FMT_YUV422P, yuv422_to_yuv422p},
2510 {PIX_FMT_UYVY422, PIX_FMT_YUV420P, uyvy422_to_yuv420p},
2511 {PIX_FMT_UYVY422, PIX_FMT_YUV422P, uyvy422_to_yuv422p},
2512 {PIX_FMT_UYVY422, PIX_FMT_GRAY8, uyvy422_to_gray},
2514 {PIX_FMT_YVYU422, PIX_FMT_YUV420P, yvyu422_to_yuv420p},
2515 {PIX_FMT_YVYU422, PIX_FMT_YUV422P, yvyu422_to_yuv422p},
2516 {PIX_FMT_YVYU422, PIX_FMT_GRAY8, yvyu422_to_gray},
2518 {PIX_FMT_RGB24, PIX_FMT_YUV420P, rgb24_to_yuv420p},
2519 {PIX_FMT_RGB24, PIX_FMT_NV12, rgb24_to_nv12},
2520 {PIX_FMT_RGB24, PIX_FMT_NV21, rgb24_to_nv21},
2521 {PIX_FMT_RGB24, PIX_FMT_RGB565, rgb24_to_rgb565},
2522 {PIX_FMT_RGB24, PIX_FMT_RGB555, rgb24_to_rgb555},
2523 {PIX_FMT_RGB24, PIX_FMT_RGB32, rgb24_to_rgb32},
2524 {PIX_FMT_RGB24, PIX_FMT_BGR32, rgb24_to_bgr32},
2525 {PIX_FMT_RGB24, PIX_FMT_xRGB32, rgb24_to_xrgb32},
2526 {PIX_FMT_RGB24, PIX_FMT_BGRx32, rgb24_to_bgrx32},
2527 {PIX_FMT_RGB24, PIX_FMT_RGBA32, rgb24_to_rgba32},
2528 {PIX_FMT_RGB24, PIX_FMT_BGR24, rgb24_to_bgr24},
2529 {PIX_FMT_RGB24, PIX_FMT_BGRA32, rgb24_to_bgra32},
2530 {PIX_FMT_RGB24, PIX_FMT_ARGB32, rgb24_to_argb32},
2531 {PIX_FMT_RGB24, PIX_FMT_ABGR32, rgb24_to_abgr32},
2532 {PIX_FMT_RGB24, PIX_FMT_GRAY8, rgb24_to_gray},
2533 {PIX_FMT_RGB24, PIX_FMT_GRAY16_L, rgb24_to_gray16_l},
2534 {PIX_FMT_RGB24, PIX_FMT_GRAY16_B, rgb24_to_gray16_b},
2535 {PIX_FMT_RGB24, PIX_FMT_PAL8, rgb24_to_pal8},
2536 {PIX_FMT_RGB24, PIX_FMT_YUV444P, rgb24_to_yuv444p},
2537 {PIX_FMT_RGB24, PIX_FMT_YUVJ420P, rgb24_to_yuvj420p},
2538 {PIX_FMT_RGB24, PIX_FMT_YUVJ444P, rgb24_to_yuvj444p},
2539 {PIX_FMT_RGB24, PIX_FMT_AYUV4444, rgb24_to_ayuv4444},
2540 {PIX_FMT_RGB24, PIX_FMT_V308, rgb24_to_v308},
2542 {PIX_FMT_RGB32, PIX_FMT_RGB24, rgb32_to_rgb24},
2543 {PIX_FMT_RGB32, PIX_FMT_RGB555, rgba32_to_rgb555},
2544 {PIX_FMT_RGB32, PIX_FMT_PAL8, rgb32_to_pal8},
2545 {PIX_FMT_RGB32, PIX_FMT_YUV420P, rgb32_to_yuv420p},
2546 {PIX_FMT_RGB32, PIX_FMT_NV12, rgb32_to_nv12},
2547 {PIX_FMT_RGB32, PIX_FMT_NV21, rgb32_to_nv21},
2548 {PIX_FMT_RGB32, PIX_FMT_GRAY8, rgb32_to_gray},
2549 {PIX_FMT_RGB32, PIX_FMT_GRAY16_L, rgb32_to_gray16_l},
2550 {PIX_FMT_RGB32, PIX_FMT_GRAY16_B, rgb32_to_gray16_b},
2552 {PIX_FMT_xRGB32, PIX_FMT_RGB24, xrgb32_to_rgb24},
2553 {PIX_FMT_xRGB32, PIX_FMT_PAL8, xrgb32_to_pal8},
2554 {PIX_FMT_xRGB32, PIX_FMT_YUV420P, xrgb32_to_yuv420p},
2555 {PIX_FMT_xRGB32, PIX_FMT_NV12, xrgb32_to_nv12},
2556 {PIX_FMT_xRGB32, PIX_FMT_NV21, xrgb32_to_nv21},
2557 {PIX_FMT_xRGB32, PIX_FMT_GRAY8, xrgb32_to_gray},
2558 {PIX_FMT_xRGB32, PIX_FMT_GRAY16_L, xrgb32_to_gray16_l},
2559 {PIX_FMT_xRGB32, PIX_FMT_GRAY16_B, xrgb32_to_gray16_b},
2561 {PIX_FMT_RGBA32, PIX_FMT_BGRA32, rgba32_to_bgra32},
2562 {PIX_FMT_RGBA32, PIX_FMT_ABGR32, rgba32_to_abgr32},
2563 {PIX_FMT_RGBA32, PIX_FMT_ARGB32, rgba32_to_argb32},
2564 {PIX_FMT_RGBA32, PIX_FMT_BGR32, rgba32_to_bgr32},
2565 {PIX_FMT_RGBA32, PIX_FMT_BGRx32, rgba32_to_bgrx32},
2566 {PIX_FMT_RGBA32, PIX_FMT_ABGR32, rgba32_to_abgr32},
2567 {PIX_FMT_RGBA32, PIX_FMT_RGB24, rgba32_to_rgb24},
2568 {PIX_FMT_RGBA32, PIX_FMT_RGB555, rgba32_to_rgb555},
2569 {PIX_FMT_RGBA32, PIX_FMT_PAL8, rgba32_to_pal8},
2570 {PIX_FMT_RGBA32, PIX_FMT_YUV420P, rgba32_to_yuv420p},
2571 {PIX_FMT_RGBA32, PIX_FMT_NV12, rgba32_to_nv12},
2572 {PIX_FMT_RGBA32, PIX_FMT_NV21, rgba32_to_nv21},
2573 {PIX_FMT_RGBA32, PIX_FMT_GRAY8, rgba32_to_gray},
2574 {PIX_FMT_RGBA32, PIX_FMT_GRAY16_L, rgba32_to_gray16_l},
2575 {PIX_FMT_RGBA32, PIX_FMT_GRAY16_B, rgba32_to_gray16_b},
2576 {PIX_FMT_RGBA32, PIX_FMT_AYUV4444, rgba32_to_ayuv4444},
2578 {PIX_FMT_BGR24, PIX_FMT_RGB24, bgr24_to_rgb24},
2579 {PIX_FMT_BGR24, PIX_FMT_YUV420P, bgr24_to_yuv420p},
2580 {PIX_FMT_BGR24, PIX_FMT_NV12, bgr24_to_nv12},
2581 {PIX_FMT_BGR24, PIX_FMT_NV21, bgr24_to_nv21},
2582 {PIX_FMT_BGR24, PIX_FMT_GRAY8, bgr24_to_gray},
2583 {PIX_FMT_BGR24, PIX_FMT_GRAY16_L, bgr24_to_gray16_l},
2584 {PIX_FMT_BGR24, PIX_FMT_GRAY16_B, bgr24_to_gray16_b},
2586 {PIX_FMT_BGR32, PIX_FMT_RGB24, bgr32_to_rgb24},
2587 {PIX_FMT_BGR32, PIX_FMT_RGBA32, bgr32_to_rgba32},
2588 {PIX_FMT_BGR32, PIX_FMT_YUV420P, bgr32_to_yuv420p},
2589 {PIX_FMT_BGR32, PIX_FMT_NV12, bgr32_to_nv12},
2590 {PIX_FMT_BGR32, PIX_FMT_NV21, bgr32_to_nv21},
2591 {PIX_FMT_BGR32, PIX_FMT_GRAY8, bgr32_to_gray},
2592 {PIX_FMT_BGR32, PIX_FMT_GRAY16_L, bgr32_to_gray16_l},
2593 {PIX_FMT_BGR32, PIX_FMT_GRAY16_B, bgr32_to_gray16_b},
2595 {PIX_FMT_BGRx32, PIX_FMT_RGB24, bgrx32_to_rgb24},
2596 {PIX_FMT_BGRx32, PIX_FMT_RGBA32, bgrx32_to_rgba32},
2597 {PIX_FMT_BGRx32, PIX_FMT_YUV420P, bgrx32_to_yuv420p},
2598 {PIX_FMT_BGRx32, PIX_FMT_NV12, bgrx32_to_nv12},
2599 {PIX_FMT_BGRx32, PIX_FMT_NV21, bgrx32_to_nv21},
2600 {PIX_FMT_BGRx32, PIX_FMT_GRAY8, bgrx32_to_gray},
2601 {PIX_FMT_BGRx32, PIX_FMT_GRAY16_L, bgrx32_to_gray16_l},
2602 {PIX_FMT_BGRx32, PIX_FMT_GRAY16_B, bgrx32_to_gray16_b},
2604 {PIX_FMT_BGRA32, PIX_FMT_RGB24, bgra32_to_rgb24},
2605 {PIX_FMT_BGRA32, PIX_FMT_RGBA32, bgra32_to_rgba32},
2606 {PIX_FMT_BGRA32, PIX_FMT_YUV420P, bgra32_to_yuv420p},
2607 {PIX_FMT_BGRA32, PIX_FMT_NV12, bgra32_to_nv12},
2608 {PIX_FMT_BGRA32, PIX_FMT_NV21, bgra32_to_nv21},
2609 {PIX_FMT_BGRA32, PIX_FMT_GRAY8, bgra32_to_gray},
2610 {PIX_FMT_BGRA32, PIX_FMT_GRAY16_L, bgra32_to_gray16_l},
2611 {PIX_FMT_BGRA32, PIX_FMT_GRAY16_B, bgra32_to_gray16_b},
2612 {PIX_FMT_BGRA32, PIX_FMT_AYUV4444, bgra32_to_ayuv4444},
2614 {PIX_FMT_ABGR32, PIX_FMT_RGB24, abgr32_to_rgb24},
2615 {PIX_FMT_ABGR32, PIX_FMT_RGBA32, abgr32_to_rgba32},
2616 {PIX_FMT_ABGR32, PIX_FMT_YUV420P, abgr32_to_yuv420p},
2617 {PIX_FMT_ABGR32, PIX_FMT_NV12, abgr32_to_nv12},
2618 {PIX_FMT_ABGR32, PIX_FMT_NV21, abgr32_to_nv21},
2619 {PIX_FMT_ABGR32, PIX_FMT_GRAY8, abgr32_to_gray},
2620 {PIX_FMT_ABGR32, PIX_FMT_GRAY16_L, abgr32_to_gray16_l},
2621 {PIX_FMT_ABGR32, PIX_FMT_GRAY16_B, abgr32_to_gray16_b},
2623 {PIX_FMT_ARGB32, PIX_FMT_RGB24, argb32_to_rgb24},
2624 {PIX_FMT_ARGB32, PIX_FMT_RGBA32, argb32_to_rgba32},
2625 {PIX_FMT_ARGB32, PIX_FMT_YUV420P, argb32_to_yuv420p},
2626 {PIX_FMT_ARGB32, PIX_FMT_NV12, argb32_to_nv12},
2627 {PIX_FMT_ARGB32, PIX_FMT_NV21, argb32_to_nv21},
2628 {PIX_FMT_ARGB32, PIX_FMT_GRAY8, argb32_to_gray},
2629 {PIX_FMT_ARGB32, PIX_FMT_GRAY16_L, argb32_to_gray16_l},
2630 {PIX_FMT_ARGB32, PIX_FMT_GRAY16_B, argb32_to_gray16_b},
2632 {PIX_FMT_RGB555, PIX_FMT_RGB24, rgb555_to_rgb24},
2633 {PIX_FMT_RGB555, PIX_FMT_RGB32, rgb555_to_rgba32},
2634 {PIX_FMT_RGB555, PIX_FMT_RGBA32, rgb555_to_rgba32},
2635 {PIX_FMT_RGB555, PIX_FMT_YUV420P, rgb555_to_yuv420p},
2636 {PIX_FMT_RGB555, PIX_FMT_NV12, rgb555_to_nv12},
2637 {PIX_FMT_RGB555, PIX_FMT_NV21, rgb555_to_nv21},
2638 {PIX_FMT_RGB555, PIX_FMT_GRAY8, rgb555_to_gray},
2639 {PIX_FMT_RGB555, PIX_FMT_GRAY16_L, rgb555_to_gray16_l},
2640 {PIX_FMT_RGB555, PIX_FMT_GRAY16_B, rgb555_to_gray16_b},
2642 {PIX_FMT_RGB565, PIX_FMT_RGB24, rgb565_to_rgb24},
2643 {PIX_FMT_RGB565, PIX_FMT_YUV420P, rgb565_to_yuv420p},
2644 {PIX_FMT_RGB565, PIX_FMT_NV12, rgb565_to_nv12},
2645 {PIX_FMT_RGB565, PIX_FMT_NV21, rgb565_to_nv21},
2646 {PIX_FMT_RGB565, PIX_FMT_GRAY8, rgb565_to_gray},
2647 {PIX_FMT_RGB565, PIX_FMT_GRAY16_L, rgb565_to_gray16_l},
2648 {PIX_FMT_RGB565, PIX_FMT_GRAY16_B, rgb565_to_gray16_b},
2650 {PIX_FMT_GRAY8, PIX_FMT_RGB555, gray_to_rgb555},
2651 {PIX_FMT_GRAY8, PIX_FMT_RGB565, gray_to_rgb565},
2652 {PIX_FMT_GRAY8, PIX_FMT_RGB24, gray_to_rgb24},
2653 {PIX_FMT_GRAY8, PIX_FMT_BGR24, gray_to_bgr24},
2654 {PIX_FMT_GRAY8, PIX_FMT_RGB32, gray_to_rgb32},
2655 {PIX_FMT_GRAY8, PIX_FMT_BGR32, gray_to_bgr32},
2656 {PIX_FMT_GRAY8, PIX_FMT_xRGB32, gray_to_xrgb32},
2657 {PIX_FMT_GRAY8, PIX_FMT_BGRx32, gray_to_bgrx32},
2658 {PIX_FMT_GRAY8, PIX_FMT_RGBA32, gray_to_rgba32},
2659 {PIX_FMT_GRAY8, PIX_FMT_BGRA32, gray_to_bgra32},
2660 {PIX_FMT_GRAY8, PIX_FMT_ARGB32, gray_to_argb32},
2661 {PIX_FMT_GRAY8, PIX_FMT_ABGR32, gray_to_abgr32},
2662 {PIX_FMT_GRAY8, PIX_FMT_MONOWHITE, gray_to_monowhite},
2663 {PIX_FMT_GRAY8, PIX_FMT_MONOBLACK, gray_to_monoblack},
2664 {PIX_FMT_GRAY8, PIX_FMT_GRAY16_L, gray_to_gray16_l},
2665 {PIX_FMT_GRAY8, PIX_FMT_GRAY16_B, gray_to_gray16_b},
2667 {PIX_FMT_MONOWHITE, PIX_FMT_GRAY8, monowhite_to_gray},
2669 {PIX_FMT_MONOBLACK, PIX_FMT_GRAY8, monoblack_to_gray},
2671 {PIX_FMT_GRAY16_L, PIX_FMT_GRAY8, gray16_l_to_gray},
2672 {PIX_FMT_GRAY16_L, PIX_FMT_RGB555, gray16_l_to_rgb555},
2673 {PIX_FMT_GRAY16_L, PIX_FMT_RGB565, gray16_l_to_rgb565},
2674 {PIX_FMT_GRAY16_L, PIX_FMT_BGR24, gray16_l_to_bgr24},
2675 {PIX_FMT_GRAY16_L, PIX_FMT_RGB24, gray16_l_to_rgb24},
2676 {PIX_FMT_GRAY16_L, PIX_FMT_BGR32, gray16_l_to_bgr32},
2677 {PIX_FMT_GRAY16_L, PIX_FMT_RGB32, gray16_l_to_rgb32},
2678 {PIX_FMT_GRAY16_L, PIX_FMT_xRGB32, gray16_l_to_xrgb32},
2679 {PIX_FMT_GRAY16_L, PIX_FMT_BGRx32, gray16_l_to_bgrx32},
2680 {PIX_FMT_GRAY16_L, PIX_FMT_ABGR32, gray16_l_to_abgr32},
2681 {PIX_FMT_GRAY16_L, PIX_FMT_ARGB32, gray16_l_to_argb32},
2682 {PIX_FMT_GRAY16_L, PIX_FMT_BGRA32, gray16_l_to_bgra32},
2683 {PIX_FMT_GRAY16_L, PIX_FMT_RGBA32, gray16_l_to_rgba32},
2684 {PIX_FMT_GRAY16_L, PIX_FMT_GRAY16_B, gray16_b_to_gray16_l},
2686 {PIX_FMT_GRAY16_B, PIX_FMT_GRAY8, gray16_b_to_gray},
2687 {PIX_FMT_GRAY16_B, PIX_FMT_RGB555, gray16_b_to_rgb555},
2688 {PIX_FMT_GRAY16_B, PIX_FMT_RGB565, gray16_b_to_rgb565},
2689 {PIX_FMT_GRAY16_B, PIX_FMT_BGR24, gray16_b_to_bgr24},
2690 {PIX_FMT_GRAY16_B, PIX_FMT_RGB24, gray16_b_to_rgb24},
2691 {PIX_FMT_GRAY16_B, PIX_FMT_BGR32, gray16_b_to_bgr32},
2692 {PIX_FMT_GRAY16_B, PIX_FMT_RGB32, gray16_b_to_rgb32},
2693 {PIX_FMT_GRAY16_B, PIX_FMT_xRGB32, gray16_b_to_xrgb32},
2694 {PIX_FMT_GRAY16_B, PIX_FMT_BGRx32, gray16_b_to_bgrx32},
2695 {PIX_FMT_GRAY16_B, PIX_FMT_ABGR32, gray16_b_to_abgr32},
2696 {PIX_FMT_GRAY16_B, PIX_FMT_ARGB32, gray16_b_to_argb32},
2697 {PIX_FMT_GRAY16_B, PIX_FMT_BGRA32, gray16_b_to_bgra32},
2698 {PIX_FMT_GRAY16_B, PIX_FMT_RGBA32, gray16_b_to_rgba32},
2699 {PIX_FMT_GRAY16_B, PIX_FMT_GRAY16_L, gray16_b_to_gray16_l},
2701 {PIX_FMT_PAL8, PIX_FMT_RGB555, pal8_to_rgb555},
2702 {PIX_FMT_PAL8, PIX_FMT_RGB565, pal8_to_rgb565},
2703 {PIX_FMT_PAL8, PIX_FMT_BGR24, pal8_to_bgr24},
2704 {PIX_FMT_PAL8, PIX_FMT_RGB24, pal8_to_rgb24},
2705 {PIX_FMT_PAL8, PIX_FMT_RGB32, pal8_to_rgb32},
2706 {PIX_FMT_PAL8, PIX_FMT_BGR32, pal8_to_bgr32},
2707 {PIX_FMT_PAL8, PIX_FMT_xRGB32, pal8_to_xrgb32},
2708 {PIX_FMT_PAL8, PIX_FMT_BGRx32, pal8_to_bgrx32},
2709 {PIX_FMT_PAL8, PIX_FMT_RGBA32, pal8_to_rgba32},
2710 {PIX_FMT_PAL8, PIX_FMT_BGRA32, pal8_to_bgra32},
2711 {PIX_FMT_PAL8, PIX_FMT_ARGB32, pal8_to_argb32},
2712 {PIX_FMT_PAL8, PIX_FMT_ABGR32, pal8_to_abgr32},
2714 {PIX_FMT_UYVY411, PIX_FMT_YUV411P, uyvy411_to_yuv411p},
2716 {PIX_FMT_V308, PIX_FMT_RGB24, v308_to_rgb24},
2718 {PIX_FMT_AYUV4444, PIX_FMT_RGBA32, ayuv4444_to_rgba32},
2719 {PIX_FMT_AYUV4444, PIX_FMT_RGB24, ayuv4444_to_rgb24},
2722 static ConvertEntry *
2723 get_convert_table_entry (int src_pix_fmt, int dst_pix_fmt)
2727 for (i = 0; i < sizeof (convert_table) / sizeof (convert_table[0]); i++) {
2728 if (convert_table[i].src == src_pix_fmt &&
2729 convert_table[i].dest == dst_pix_fmt) {
2730 return convert_table + i;
2738 avpicture_alloc (AVPicture * picture, int pix_fmt, int width, int height)
2743 size = avpicture_get_size (pix_fmt, width, height);
2744 ptr = av_malloc (size);
2747 gst_ffmpegcsp_avpicture_fill (picture, ptr, pix_fmt, width, height);
2750 memset (picture, 0, sizeof (AVPicture));
2755 avpicture_free (AVPicture * picture)
2757 av_free (picture->data[0]);
2760 /* return true if yuv planar */
2762 is_yuv_planar (PixFmtInfo * ps)
2764 return (ps->color_type == FF_COLOR_YUV ||
2765 ps->color_type == FF_COLOR_YUV_JPEG) && ps->pixel_type == FF_PIXEL_PLANAR;
2768 /* XXX: always use linesize. Return -1 if not supported */
2770 img_convert (AVPicture * dst, int dst_pix_fmt,
2771 const AVPicture * src, int src_pix_fmt, int src_width, int src_height)
2774 int i, ret, dst_width, dst_height, int_pix_fmt;
2775 PixFmtInfo *src_pix, *dst_pix;
2777 AVPicture tmp1, *tmp = &tmp1;
2779 if (G_UNLIKELY (src_pix_fmt < 0 || src_pix_fmt >= PIX_FMT_NB ||
2780 dst_pix_fmt < 0 || dst_pix_fmt >= PIX_FMT_NB))
2782 if (G_UNLIKELY (src_width <= 0 || src_height <= 0))
2785 if (G_UNLIKELY (!inited)) {
2787 img_convert_init ();
2790 dst_width = src_width;
2791 dst_height = src_height;
2793 dst_pix = get_pix_fmt_info (dst_pix_fmt);
2794 src_pix = get_pix_fmt_info (src_pix_fmt);
2795 if (G_UNLIKELY (src_pix_fmt == dst_pix_fmt)) {
2796 /* no conversion needed: just copy */
2797 img_copy (dst, src, dst_pix_fmt, dst_width, dst_height);
2801 ce = get_convert_table_entry (src_pix_fmt, dst_pix_fmt);
2802 if (ce && ce->convert) {
2803 /* specific conversion routine */
2804 ce->convert (dst, src, dst_width, dst_height);
2809 if (is_yuv_planar (dst_pix) && src_pix_fmt == PIX_FMT_GRAY8) {
2813 if (dst_pix->color_type == FF_COLOR_YUV_JPEG) {
2814 img_copy_plane (dst->data[0], dst->linesize[0],
2815 src->data[0], src->linesize[0], dst_width, dst_height);
2817 img_apply_table (dst->data[0], dst->linesize[0],
2818 src->data[0], src->linesize[0],
2819 dst_width, dst_height, y_jpeg_to_ccir);
2821 /* fill U and V with 128 */
2824 w >>= dst_pix->x_chroma_shift;
2825 h >>= dst_pix->y_chroma_shift;
2826 for (i = 1; i <= 2; i++) {
2828 for (y = 0; y < h; y++) {
2830 d += dst->linesize[i];
2837 if (is_yuv_planar (src_pix) && dst_pix_fmt == PIX_FMT_GRAY8) {
2838 if (src_pix->color_type == FF_COLOR_YUV_JPEG) {
2839 img_copy_plane (dst->data[0], dst->linesize[0],
2840 src->data[0], src->linesize[0], dst_width, dst_height);
2842 img_apply_table (dst->data[0], dst->linesize[0],
2843 src->data[0], src->linesize[0],
2844 dst_width, dst_height, y_ccir_to_jpeg);
2849 /* YUV to YUV planar */
2850 if (is_yuv_planar (dst_pix) && is_yuv_planar (src_pix)) {
2851 int x_shift, y_shift, w, h, xy_shift;
2852 void (*resize_func) (uint8_t * dst, int dst_wrap,
2853 const uint8_t * src, int src_wrap, int width, int height);
2855 /* compute chroma size of the smallest dimensions */
2858 if (dst_pix->x_chroma_shift >= src_pix->x_chroma_shift)
2859 w >>= dst_pix->x_chroma_shift;
2861 w >>= src_pix->x_chroma_shift;
2862 if (dst_pix->y_chroma_shift >= src_pix->y_chroma_shift)
2863 h >>= dst_pix->y_chroma_shift;
2865 h >>= src_pix->y_chroma_shift;
2867 x_shift = (dst_pix->x_chroma_shift - src_pix->x_chroma_shift);
2868 y_shift = (dst_pix->y_chroma_shift - src_pix->y_chroma_shift);
2869 xy_shift = ((x_shift & 0xf) << 4) | (y_shift & 0xf);
2871 /* there must be filters for conversion at least from and to
2875 resize_func = img_copy_plane;
2878 resize_func = shrink21;
2881 resize_func = shrink41;
2884 resize_func = shrink12;
2887 resize_func = shrink22;
2890 resize_func = shrink44;
2893 resize_func = grow21;
2896 resize_func = grow41;
2899 resize_func = grow22;
2902 resize_func = grow44;
2905 resize_func = conv411;
2908 /* currently not handled */
2909 goto no_chroma_filter;
2912 img_copy_plane (dst->data[0], dst->linesize[0],
2913 src->data[0], src->linesize[0], dst_width, dst_height);
2915 #define GEN_MASK(x) ((1<<(x))-1)
2916 #define DIV_ROUND_UP_X(v,x) (((v) + GEN_MASK(x)) >> (x))
2918 for (i = 1; i <= 2; i++) {
2921 w = DIV_ROUND_UP_X (dst_width, dst_pix->x_chroma_shift);
2922 h = DIV_ROUND_UP_X (dst_height, dst_pix->y_chroma_shift);
2924 resize_func (dst->data[i], dst->linesize[i],
2925 src->data[i], src->linesize[i], w, h);
2927 /* if yuv color space conversion is needed, we do it here on
2928 the destination image */
2929 if (dst_pix->color_type != src_pix->color_type) {
2930 const uint8_t *y_table, *c_table;
2932 if (dst_pix->color_type == FF_COLOR_YUV) {
2933 y_table = y_jpeg_to_ccir;
2934 c_table = c_jpeg_to_ccir;
2936 y_table = y_ccir_to_jpeg;
2937 c_table = c_ccir_to_jpeg;
2939 img_apply_table (dst->data[0], dst->linesize[0],
2940 dst->data[0], dst->linesize[0], dst_width, dst_height, y_table);
2942 for (i = 1; i <= 2; i++)
2943 img_apply_table (dst->data[i], dst->linesize[i],
2944 dst->data[i], dst->linesize[i],
2945 dst_width >> dst_pix->x_chroma_shift,
2946 dst_height >> dst_pix->y_chroma_shift, c_table);
2952 /* try to use an intermediate format */
2953 if (src_pix_fmt == PIX_FMT_YUV422 || dst_pix_fmt == PIX_FMT_YUV422) {
2954 /* specific case: convert to YUV422P first */
2955 int_pix_fmt = PIX_FMT_YUV422P;
2956 } else if (src_pix_fmt == PIX_FMT_UYVY422 || dst_pix_fmt == PIX_FMT_UYVY422 ||
2957 src_pix_fmt == PIX_FMT_YVYU422 || dst_pix_fmt == PIX_FMT_YVYU422) {
2958 /* specific case: convert to YUV422P first */
2959 int_pix_fmt = PIX_FMT_YUV422P;
2960 } else if (src_pix_fmt == PIX_FMT_UYVY411 || dst_pix_fmt == PIX_FMT_UYVY411) {
2961 /* specific case: convert to YUV411P first */
2962 int_pix_fmt = PIX_FMT_YUV411P;
2963 } else if ((src_pix->color_type == FF_COLOR_GRAY &&
2964 src_pix_fmt != PIX_FMT_GRAY8) ||
2965 (dst_pix->color_type == FF_COLOR_GRAY && dst_pix_fmt != PIX_FMT_GRAY8)) {
2966 /* gray8 is the normalized format */
2967 int_pix_fmt = PIX_FMT_GRAY8;
2968 } else if ((is_yuv_planar (src_pix) &&
2969 src_pix_fmt != PIX_FMT_YUV444P && src_pix_fmt != PIX_FMT_YUVJ444P)) {
2970 /* yuv444 is the normalized format */
2971 if (src_pix->color_type == FF_COLOR_YUV_JPEG)
2972 int_pix_fmt = PIX_FMT_YUVJ444P;
2974 int_pix_fmt = PIX_FMT_YUV444P;
2975 } else if ((is_yuv_planar (dst_pix) &&
2976 dst_pix_fmt != PIX_FMT_YUV444P && dst_pix_fmt != PIX_FMT_YUVJ444P)) {
2977 /* yuv444 is the normalized format */
2978 if (dst_pix->color_type == FF_COLOR_YUV_JPEG)
2979 int_pix_fmt = PIX_FMT_YUVJ444P;
2981 int_pix_fmt = PIX_FMT_YUV444P;
2983 /* the two formats are rgb or gray8 or yuv[j]444p */
2984 if (src_pix->is_alpha && dst_pix->is_alpha)
2985 int_pix_fmt = PIX_FMT_RGBA32;
2987 int_pix_fmt = PIX_FMT_RGB24;
2989 if (avpicture_alloc (tmp, int_pix_fmt, dst_width, dst_height) < 0)
2992 if (img_convert (tmp, int_pix_fmt,
2993 src, src_pix_fmt, src_width, src_height) < 0)
2996 if (img_convert (dst, dst_pix_fmt,
2997 tmp, int_pix_fmt, dst_width, dst_height) < 0)
3001 avpicture_free (tmp);
3005 /* NOTE: we scan all the pixels to have an exact information */
3007 get_alpha_info_pal8 (const AVPicture * src, int width, int height)
3009 const unsigned char *p;
3010 int src_wrap, ret, x, y;
3012 uint32_t *palette = (uint32_t *) src->data[1];
3015 src_wrap = src->linesize[0] - width;
3017 for (y = 0; y < height; y++) {
3018 for (x = 0; x < width; x++) {
3019 a = palette[p[0]] >> 24;
3021 ret |= FF_ALPHA_TRANSP;
3022 } else if (a != 0xff) {
3023 ret |= FF_ALPHA_SEMI_TRANSP;
3033 * Tell if an image really has transparent alpha values.
3034 * @return ored mask of FF_ALPHA_xxx constants
3037 img_get_alpha_info (const AVPicture * src, int pix_fmt, int width, int height)
3039 const PixFmtInfo *pf;
3042 pf = get_pix_fmt_info (pix_fmt);
3043 /* no alpha can be represented in format */
3048 ret = get_alpha_info_rgb32 (src, width, height);
3051 ret = get_alpha_info_bgr32 (src, width, height);
3053 case PIX_FMT_xRGB32:
3054 ret = get_alpha_info_xrgb32 (src, width, height);
3056 case PIX_FMT_BGRx32:
3057 ret = get_alpha_info_bgrx32 (src, width, height);
3059 case PIX_FMT_RGBA32:
3060 ret = get_alpha_info_rgba32 (src, width, height);
3062 case PIX_FMT_BGRA32:
3063 ret = get_alpha_info_bgra32 (src, width, height);
3065 case PIX_FMT_ARGB32:
3066 ret = get_alpha_info_argb32 (src, width, height);
3068 case PIX_FMT_ABGR32:
3069 ret = get_alpha_info_abgr32 (src, width, height);
3071 case PIX_FMT_RGB555:
3072 ret = get_alpha_info_rgb555 (src, width, height);
3075 ret = get_alpha_info_pal8 (src, width, height);
3078 /* we do not know, so everything is indicated */
3079 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
3086 #define DEINT_INPLACE_LINE_LUM \
3087 movd_m2r(lum_m4[0],mm0);\
3088 movd_m2r(lum_m3[0],mm1);\
3089 movd_m2r(lum_m2[0],mm2);\
3090 movd_m2r(lum_m1[0],mm3);\
3091 movd_m2r(lum[0],mm4);\
3092 punpcklbw_r2r(mm7,mm0);\
3093 movd_r2m(mm2,lum_m4[0]);\
3094 punpcklbw_r2r(mm7,mm1);\
3095 punpcklbw_r2r(mm7,mm2);\
3096 punpcklbw_r2r(mm7,mm3);\
3097 punpcklbw_r2r(mm7,mm4);\
3098 paddw_r2r(mm3,mm1);\
3100 paddw_r2r(mm4,mm0);\
3102 paddw_r2r(mm6,mm2);\
3103 paddw_r2r(mm2,mm1);\
3104 psubusw_r2r(mm0,mm1);\
3106 packuswb_r2r(mm7,mm1);\
3107 movd_r2m(mm1,lum_m2[0]);
3109 #define DEINT_LINE_LUM \
3110 movd_m2r(lum_m4[0],mm0);\
3111 movd_m2r(lum_m3[0],mm1);\
3112 movd_m2r(lum_m2[0],mm2);\
3113 movd_m2r(lum_m1[0],mm3);\
3114 movd_m2r(lum[0],mm4);\
3115 punpcklbw_r2r(mm7,mm0);\
3116 punpcklbw_r2r(mm7,mm1);\
3117 punpcklbw_r2r(mm7,mm2);\
3118 punpcklbw_r2r(mm7,mm3);\
3119 punpcklbw_r2r(mm7,mm4);\
3120 paddw_r2r(mm3,mm1);\
3122 paddw_r2r(mm4,mm0);\
3124 paddw_r2r(mm6,mm2);\
3125 paddw_r2r(mm2,mm1);\
3126 psubusw_r2r(mm0,mm1);\
3128 packuswb_r2r(mm7,mm1);\
3129 movd_r2m(mm1,dst[0]);
3132 /* filter parameters: [-1 4 2 4 -1] // 8 */
3135 deinterlace_line (uint8_t * dst,
3136 const uint8_t * lum_m4, const uint8_t * lum_m3,
3137 const uint8_t * lum_m2, const uint8_t * lum_m1,
3138 const uint8_t * lum, int size)
3141 uint8_t *cm = cropTbl + MAX_NEG_CROP;
3144 for (; size > 0; size--) {
3146 sum += lum_m3[0] << 2;
3147 sum += lum_m2[0] << 1;
3148 sum += lum_m1[0] << 2;
3150 dst[0] = cm[(sum + 4) >> 3];
3167 pxor_r2r (mm7, mm7);
3168 movq_m2r (rounder, mm6);
3170 for (; size > 3; size -= 4) {
3171 DEINT_LINE_LUM lum_m4 += 4;
3183 deinterlace_line_inplace (uint8_t * lum_m4, uint8_t * lum_m3, uint8_t * lum_m2,
3184 uint8_t * lum_m1, uint8_t * lum, int size)
3187 uint8_t *cm = cropTbl + MAX_NEG_CROP;
3190 for (; size > 0; size--) {
3192 sum += lum_m3[0] << 2;
3193 sum += lum_m2[0] << 1;
3194 lum_m4[0] = lum_m2[0];
3195 sum += lum_m1[0] << 2;
3197 lum_m2[0] = cm[(sum + 4) >> 3];
3213 pxor_r2r (mm7, mm7);
3214 movq_m2r (rounder, mm6);
3216 for (; size > 3; size -= 4) {
3217 DEINT_INPLACE_LINE_LUM lum_m4 += 4;
3228 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
3229 top field is copied as is, but the bottom field is deinterlaced
3230 against the top field. */
3233 deinterlace_bottom_field (uint8_t * dst, int dst_wrap,
3234 const uint8_t * src1, int src_wrap, int width, int height)
3236 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
3241 src_0 = &src_m1[src_wrap];
3242 src_p1 = &src_0[src_wrap];
3243 src_p2 = &src_p1[src_wrap];
3244 for (y = 0; y < (height - 2); y += 2) {
3245 memcpy (dst, src_m1, width);
3247 deinterlace_line (dst, src_m2, src_m1, src_0, src_p1, src_p2, width);
3251 src_p1 += 2 * src_wrap;
3252 src_p2 += 2 * src_wrap;
3255 memcpy (dst, src_m1, width);
3258 deinterlace_line (dst, src_m2, src_m1, src_0, src_0, src_0, width);
3262 deinterlace_bottom_field_inplace (uint8_t * src1, int src_wrap,
3263 int width, int height)
3265 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
3269 buf = (uint8_t *) av_malloc (width);
3272 memcpy (buf, src_m1, width);
3273 src_0 = &src_m1[src_wrap];
3274 src_p1 = &src_0[src_wrap];
3275 src_p2 = &src_p1[src_wrap];
3276 for (y = 0; y < (height - 2); y += 2) {
3277 deinterlace_line_inplace (buf, src_m1, src_0, src_p1, src_p2, width);
3280 src_p1 += 2 * src_wrap;
3281 src_p2 += 2 * src_wrap;
3284 deinterlace_line_inplace (buf, src_m1, src_0, src_0, src_0, width);
3289 /* deinterlace - if not supported return -1 */
3292 avpicture_deinterlace (AVPicture * dst, const AVPicture * src,
3293 int pix_fmt, int width, int height)
3297 if (pix_fmt != PIX_FMT_YUV420P &&
3298 pix_fmt != PIX_FMT_YUV422P &&
3299 pix_fmt != PIX_FMT_YUV444P && pix_fmt != PIX_FMT_YUV411P)
3301 if ((width & 3) != 0 || (height & 3) != 0)
3304 for (i = 0; i < 3; i++) {
3307 case PIX_FMT_YUV420P:
3311 case PIX_FMT_YUV422P:
3314 case PIX_FMT_YUV411P:
3322 deinterlace_bottom_field_inplace (dst->data[i], dst->linesize[i],
3325 deinterlace_bottom_field (dst->data[i], dst->linesize[i],
3326 src->data[i], src->linesize[i], width, height);