2 * Misc image convertion routines
3 * Copyright (c) 2001, 2002, 2003 Fabrice Bellard.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * Misc image convertion routines.
28 * - write 'ffimg' program to test all the image related stuff
29 * - move all api to slice based system
30 * - integrate deinterlacing, postprocessing and scaling in the conversion process
37 #include "libvo/fastmemcpy.h"
44 #define xglue(x, y) x ## y
45 #define glue(x, y) xglue(x, y)
47 #define FF_COLOR_RGB 0 /**< RGB color space */
48 #define FF_COLOR_GRAY 1 /**< gray color space */
49 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
50 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
52 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
53 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
54 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
56 typedef struct PixFmtInfo
{
58 uint8_t nb_channels
; /**< number of channels (including alpha) */
59 uint8_t color_type
; /**< color type (see FF_COLOR_xxx constants) */
60 uint8_t pixel_type
; /**< pixel storage type (see FF_PIXEL_xxx constants) */
61 uint8_t is_alpha
: 1; /**< true if alpha can be specified */
62 uint8_t x_chroma_shift
; /**< X chroma subsampling factor is 2 ^ shift */
63 uint8_t y_chroma_shift
; /**< Y chroma subsampling factor is 2 ^ shift */
64 uint8_t depth
; /**< bit depth of the color components */
67 /* this table gives more information about formats */
68 static const PixFmtInfo pix_fmt_info
[PIX_FMT_NB
] = {
73 .color_type
= FF_COLOR_YUV
,
74 .pixel_type
= FF_PIXEL_PLANAR
,
76 .x_chroma_shift
= 1, .y_chroma_shift
= 1,
81 .color_type
= FF_COLOR_YUV
,
82 .pixel_type
= FF_PIXEL_PLANAR
,
84 .x_chroma_shift
= 1, .y_chroma_shift
= 0,
89 .color_type
= FF_COLOR_YUV
,
90 .pixel_type
= FF_PIXEL_PLANAR
,
92 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
97 .color_type
= FF_COLOR_YUV
,
98 .pixel_type
= FF_PIXEL_PACKED
,
100 .x_chroma_shift
= 1, .y_chroma_shift
= 0,
102 [PIX_FMT_UYVY422
] = {
105 .color_type
= FF_COLOR_YUV
,
106 .pixel_type
= FF_PIXEL_PACKED
,
108 .x_chroma_shift
= 1, .y_chroma_shift
= 0,
110 [PIX_FMT_YUV410P
] = {
113 .color_type
= FF_COLOR_YUV
,
114 .pixel_type
= FF_PIXEL_PLANAR
,
116 .x_chroma_shift
= 2, .y_chroma_shift
= 2,
118 [PIX_FMT_YUV411P
] = {
121 .color_type
= FF_COLOR_YUV
,
122 .pixel_type
= FF_PIXEL_PLANAR
,
124 .x_chroma_shift
= 2, .y_chroma_shift
= 0,
128 [PIX_FMT_YUVJ420P
] = {
131 .color_type
= FF_COLOR_YUV_JPEG
,
132 .pixel_type
= FF_PIXEL_PLANAR
,
134 .x_chroma_shift
= 1, .y_chroma_shift
= 1,
136 [PIX_FMT_YUVJ422P
] = {
139 .color_type
= FF_COLOR_YUV_JPEG
,
140 .pixel_type
= FF_PIXEL_PLANAR
,
142 .x_chroma_shift
= 1, .y_chroma_shift
= 0,
144 [PIX_FMT_YUVJ444P
] = {
147 .color_type
= FF_COLOR_YUV_JPEG
,
148 .pixel_type
= FF_PIXEL_PLANAR
,
150 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
157 .color_type
= FF_COLOR_RGB
,
158 .pixel_type
= FF_PIXEL_PACKED
,
160 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
165 .color_type
= FF_COLOR_RGB
,
166 .pixel_type
= FF_PIXEL_PACKED
,
168 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
172 .nb_channels
= 4, .is_alpha
= 1,
173 .color_type
= FF_COLOR_RGB
,
174 .pixel_type
= FF_PIXEL_PACKED
,
176 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
181 .color_type
= FF_COLOR_RGB
,
182 .pixel_type
= FF_PIXEL_PACKED
,
184 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
189 .color_type
= FF_COLOR_RGB
,
190 .pixel_type
= FF_PIXEL_PACKED
,
192 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
195 /* gray / mono formats */
196 [PIX_FMT_GRAY16BE
] = {
199 .color_type
= FF_COLOR_GRAY
,
200 .pixel_type
= FF_PIXEL_PLANAR
,
203 [PIX_FMT_GRAY16LE
] = {
206 .color_type
= FF_COLOR_GRAY
,
207 .pixel_type
= FF_PIXEL_PLANAR
,
213 .color_type
= FF_COLOR_GRAY
,
214 .pixel_type
= FF_PIXEL_PLANAR
,
217 [PIX_FMT_MONOWHITE
] = {
220 .color_type
= FF_COLOR_GRAY
,
221 .pixel_type
= FF_PIXEL_PLANAR
,
224 [PIX_FMT_MONOBLACK
] = {
227 .color_type
= FF_COLOR_GRAY
,
228 .pixel_type
= FF_PIXEL_PLANAR
,
232 /* paletted formats */
235 .nb_channels
= 4, .is_alpha
= 1,
236 .color_type
= FF_COLOR_RGB
,
237 .pixel_type
= FF_PIXEL_PALETTE
,
240 [PIX_FMT_XVMC_MPEG2_MC
] = {
243 [PIX_FMT_XVMC_MPEG2_IDCT
] = {
246 [PIX_FMT_UYYVYY411
] = {
249 .color_type
= FF_COLOR_YUV
,
250 .pixel_type
= FF_PIXEL_PACKED
,
252 .x_chroma_shift
= 2, .y_chroma_shift
= 0,
256 .nb_channels
= 4, .is_alpha
= 1,
257 .color_type
= FF_COLOR_RGB
,
258 .pixel_type
= FF_PIXEL_PACKED
,
260 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
265 .color_type
= FF_COLOR_RGB
,
266 .pixel_type
= FF_PIXEL_PACKED
,
268 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
273 .color_type
= FF_COLOR_RGB
,
274 .pixel_type
= FF_PIXEL_PACKED
,
276 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
281 .color_type
= FF_COLOR_RGB
,
282 .pixel_type
= FF_PIXEL_PACKED
,
284 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
289 .color_type
= FF_COLOR_RGB
,
290 .pixel_type
= FF_PIXEL_PACKED
,
292 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
294 [PIX_FMT_RGB4_BYTE
] = {
297 .color_type
= FF_COLOR_RGB
,
298 .pixel_type
= FF_PIXEL_PACKED
,
300 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
305 .color_type
= FF_COLOR_RGB
,
306 .pixel_type
= FF_PIXEL_PACKED
,
308 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
313 .color_type
= FF_COLOR_RGB
,
314 .pixel_type
= FF_PIXEL_PACKED
,
316 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
318 [PIX_FMT_BGR4_BYTE
] = {
321 .color_type
= FF_COLOR_RGB
,
322 .pixel_type
= FF_PIXEL_PACKED
,
324 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
329 .color_type
= FF_COLOR_YUV
,
330 .pixel_type
= FF_PIXEL_PLANAR
,
332 .x_chroma_shift
= 1, .y_chroma_shift
= 1,
337 .color_type
= FF_COLOR_YUV
,
338 .pixel_type
= FF_PIXEL_PLANAR
,
340 .x_chroma_shift
= 1, .y_chroma_shift
= 1,
343 [PIX_FMT_BGR32_1
] = {
345 .nb_channels
= 4, .is_alpha
= 1,
346 .color_type
= FF_COLOR_RGB
,
347 .pixel_type
= FF_PIXEL_PACKED
,
349 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
351 [PIX_FMT_RGB32_1
] = {
353 .nb_channels
= 4, .is_alpha
= 1,
354 .color_type
= FF_COLOR_RGB
,
355 .pixel_type
= FF_PIXEL_PACKED
,
357 .x_chroma_shift
= 0, .y_chroma_shift
= 0,
361 void avcodec_get_chroma_sub_sample(int pix_fmt
, int *h_shift
, int *v_shift
)
363 *h_shift
= pix_fmt_info
[pix_fmt
].x_chroma_shift
;
364 *v_shift
= pix_fmt_info
[pix_fmt
].y_chroma_shift
;
367 const char *avcodec_get_pix_fmt_name(int pix_fmt
)
369 if (pix_fmt
< 0 || pix_fmt
>= PIX_FMT_NB
)
372 return pix_fmt_info
[pix_fmt
].name
;
375 enum PixelFormat
avcodec_get_pix_fmt(const char* name
)
379 for (i
=0; i
< PIX_FMT_NB
; i
++)
380 if (!strcmp(pix_fmt_info
[i
].name
, name
))
385 int avpicture_fill(AVPicture
*picture
, uint8_t *ptr
,
386 int pix_fmt
, int width
, int height
)
388 int size
, w2
, h2
, size2
;
389 const PixFmtInfo
*pinfo
;
391 if(avcodec_check_dimensions(NULL
, width
, height
))
394 pinfo
= &pix_fmt_info
[pix_fmt
];
395 size
= width
* height
;
397 case PIX_FMT_YUV420P
:
398 case PIX_FMT_YUV422P
:
399 case PIX_FMT_YUV444P
:
400 case PIX_FMT_YUV410P
:
401 case PIX_FMT_YUV411P
:
402 case PIX_FMT_YUVJ420P
:
403 case PIX_FMT_YUVJ422P
:
404 case PIX_FMT_YUVJ444P
:
405 w2
= (width
+ (1 << pinfo
->x_chroma_shift
) - 1) >> pinfo
->x_chroma_shift
;
406 h2
= (height
+ (1 << pinfo
->y_chroma_shift
) - 1) >> pinfo
->y_chroma_shift
;
408 picture
->data
[0] = ptr
;
409 picture
->data
[1] = picture
->data
[0] + size
;
410 picture
->data
[2] = picture
->data
[1] + size2
;
411 picture
->linesize
[0] = width
;
412 picture
->linesize
[1] = w2
;
413 picture
->linesize
[2] = w2
;
414 return size
+ 2 * size2
;
417 w2
= (width
+ (1 << pinfo
->x_chroma_shift
) - 1) >> pinfo
->x_chroma_shift
;
418 h2
= (height
+ (1 << pinfo
->y_chroma_shift
) - 1) >> pinfo
->y_chroma_shift
;
420 picture
->data
[0] = ptr
;
421 picture
->data
[1] = picture
->data
[0] + size
;
422 picture
->data
[2] = NULL
;
423 picture
->linesize
[0] = width
;
424 picture
->linesize
[1] = w2
;
425 picture
->linesize
[2] = 0;
426 return size
+ 2 * size2
;
429 picture
->data
[0] = ptr
;
430 picture
->data
[1] = NULL
;
431 picture
->data
[2] = NULL
;
432 picture
->linesize
[0] = width
* 3;
436 case PIX_FMT_RGB32_1
:
437 case PIX_FMT_BGR32_1
:
438 picture
->data
[0] = ptr
;
439 picture
->data
[1] = NULL
;
440 picture
->data
[2] = NULL
;
441 picture
->linesize
[0] = width
* 4;
443 case PIX_FMT_GRAY16BE
:
444 case PIX_FMT_GRAY16LE
:
449 case PIX_FMT_YUYV422
:
450 picture
->data
[0] = ptr
;
451 picture
->data
[1] = NULL
;
452 picture
->data
[2] = NULL
;
453 picture
->linesize
[0] = width
* 2;
455 case PIX_FMT_UYVY422
:
456 picture
->data
[0] = ptr
;
457 picture
->data
[1] = NULL
;
458 picture
->data
[2] = NULL
;
459 picture
->linesize
[0] = width
* 2;
461 case PIX_FMT_UYYVYY411
:
462 picture
->data
[0] = ptr
;
463 picture
->data
[1] = NULL
;
464 picture
->data
[2] = NULL
;
465 picture
->linesize
[0] = width
+ width
/2;
466 return size
+ size
/2;
469 case PIX_FMT_RGB4_BYTE
:
470 case PIX_FMT_BGR4_BYTE
:
472 picture
->data
[0] = ptr
;
473 picture
->data
[1] = NULL
;
474 picture
->data
[2] = NULL
;
475 picture
->linesize
[0] = width
;
479 picture
->data
[0] = ptr
;
480 picture
->data
[1] = NULL
;
481 picture
->data
[2] = NULL
;
482 picture
->linesize
[0] = width
/ 2;
484 case PIX_FMT_MONOWHITE
:
485 case PIX_FMT_MONOBLACK
:
486 picture
->data
[0] = ptr
;
487 picture
->data
[1] = NULL
;
488 picture
->data
[2] = NULL
;
489 picture
->linesize
[0] = (width
+ 7) >> 3;
490 return picture
->linesize
[0] * height
;
492 size2
= (size
+ 3) & ~3;
493 picture
->data
[0] = ptr
;
494 picture
->data
[1] = ptr
+ size2
; /* palette is stored here as 256 32 bit words */
495 picture
->data
[2] = NULL
;
496 picture
->linesize
[0] = width
;
497 picture
->linesize
[1] = 4;
498 return size2
+ 256 * 4;
501 picture
->data
[0] = NULL
;
502 picture
->data
[1] = NULL
;
503 picture
->data
[2] = NULL
;
504 picture
->data
[3] = NULL
;
509 int avpicture_layout(const AVPicture
* src
, int pix_fmt
, int width
, int height
,
510 unsigned char *dest
, int dest_size
)
512 const PixFmtInfo
* pf
= &pix_fmt_info
[pix_fmt
];
513 int i
, j
, w
, h
, data_planes
;
514 const unsigned char* s
;
515 int size
= avpicture_get_size(pix_fmt
, width
, height
);
517 if (size
> dest_size
|| size
< 0)
520 if (pf
->pixel_type
== FF_PIXEL_PACKED
|| pf
->pixel_type
== FF_PIXEL_PALETTE
) {
521 if (pix_fmt
== PIX_FMT_YUYV422
||
522 pix_fmt
== PIX_FMT_UYVY422
||
523 pix_fmt
== PIX_FMT_BGR565
||
524 pix_fmt
== PIX_FMT_BGR555
||
525 pix_fmt
== PIX_FMT_RGB565
||
526 pix_fmt
== PIX_FMT_RGB555
)
528 else if (pix_fmt
== PIX_FMT_UYYVYY411
)
530 else if (pix_fmt
== PIX_FMT_PAL8
)
533 w
= width
* (pf
->depth
* pf
->nb_channels
/ 8);
538 data_planes
= pf
->nb_channels
;
539 w
= (width
*pf
->depth
+ 7)/8;
543 for (i
=0; i
<data_planes
; i
++) {
545 w
= width
>> pf
->x_chroma_shift
;
546 h
= height
>> pf
->y_chroma_shift
;
552 s
+= src
->linesize
[i
];
556 if (pf
->pixel_type
== FF_PIXEL_PALETTE
)
557 memcpy((unsigned char *)(((size_t)dest
+ 3) & ~3), src
->data
[1], 256 * 4);
562 int avpicture_get_size(int pix_fmt
, int width
, int height
)
564 AVPicture dummy_pict
;
565 return avpicture_fill(&dummy_pict
, NULL
, pix_fmt
, width
, height
);
568 int avcodec_get_pix_fmt_loss(int dst_pix_fmt
, int src_pix_fmt
,
571 const PixFmtInfo
*pf
, *ps
;
574 ps
= &pix_fmt_info
[src_pix_fmt
];
575 pf
= &pix_fmt_info
[dst_pix_fmt
];
579 pf
= &pix_fmt_info
[dst_pix_fmt
];
580 if (pf
->depth
< ps
->depth
||
581 (dst_pix_fmt
== PIX_FMT_RGB555
&& src_pix_fmt
== PIX_FMT_RGB565
))
582 loss
|= FF_LOSS_DEPTH
;
583 if (pf
->x_chroma_shift
> ps
->x_chroma_shift
||
584 pf
->y_chroma_shift
> ps
->y_chroma_shift
)
585 loss
|= FF_LOSS_RESOLUTION
;
586 switch(pf
->color_type
) {
588 if (ps
->color_type
!= FF_COLOR_RGB
&&
589 ps
->color_type
!= FF_COLOR_GRAY
)
590 loss
|= FF_LOSS_COLORSPACE
;
593 if (ps
->color_type
!= FF_COLOR_GRAY
)
594 loss
|= FF_LOSS_COLORSPACE
;
597 if (ps
->color_type
!= FF_COLOR_YUV
)
598 loss
|= FF_LOSS_COLORSPACE
;
600 case FF_COLOR_YUV_JPEG
:
601 if (ps
->color_type
!= FF_COLOR_YUV_JPEG
&&
602 ps
->color_type
!= FF_COLOR_YUV
&&
603 ps
->color_type
!= FF_COLOR_GRAY
)
604 loss
|= FF_LOSS_COLORSPACE
;
608 if (ps
->color_type
!= pf
->color_type
)
609 loss
|= FF_LOSS_COLORSPACE
;
612 if (pf
->color_type
== FF_COLOR_GRAY
&&
613 ps
->color_type
!= FF_COLOR_GRAY
)
614 loss
|= FF_LOSS_CHROMA
;
615 if (!pf
->is_alpha
&& (ps
->is_alpha
&& has_alpha
))
616 loss
|= FF_LOSS_ALPHA
;
617 if (pf
->pixel_type
== FF_PIXEL_PALETTE
&&
618 (ps
->pixel_type
!= FF_PIXEL_PALETTE
&& ps
->color_type
!= FF_COLOR_GRAY
))
619 loss
|= FF_LOSS_COLORQUANT
;
623 static int avg_bits_per_pixel(int pix_fmt
)
626 const PixFmtInfo
*pf
;
628 pf
= &pix_fmt_info
[pix_fmt
];
629 switch(pf
->pixel_type
) {
630 case FF_PIXEL_PACKED
:
632 case PIX_FMT_YUYV422
:
633 case PIX_FMT_UYVY422
:
640 case PIX_FMT_UYYVYY411
:
644 bits
= pf
->depth
* pf
->nb_channels
;
648 case FF_PIXEL_PLANAR
:
649 if (pf
->x_chroma_shift
== 0 && pf
->y_chroma_shift
== 0) {
650 bits
= pf
->depth
* pf
->nb_channels
;
652 bits
= pf
->depth
+ ((2 * pf
->depth
) >>
653 (pf
->x_chroma_shift
+ pf
->y_chroma_shift
));
656 case FF_PIXEL_PALETTE
:
666 static int avcodec_find_best_pix_fmt1(int pix_fmt_mask
,
671 int dist
, i
, loss
, min_dist
, dst_pix_fmt
;
673 /* find exact color match with smallest size */
675 min_dist
= 0x7fffffff;
676 for(i
= 0;i
< PIX_FMT_NB
; i
++) {
677 if (pix_fmt_mask
& (1 << i
)) {
678 loss
= avcodec_get_pix_fmt_loss(i
, src_pix_fmt
, has_alpha
) & loss_mask
;
680 dist
= avg_bits_per_pixel(i
);
681 if (dist
< min_dist
) {
691 int avcodec_find_best_pix_fmt(int pix_fmt_mask
, int src_pix_fmt
,
692 int has_alpha
, int *loss_ptr
)
694 int dst_pix_fmt
, loss_mask
, i
;
695 static const int loss_mask_order
[] = {
696 ~0, /* no loss first */
699 ~(FF_LOSS_COLORSPACE
| FF_LOSS_RESOLUTION
),
705 /* try with successive loss */
708 loss_mask
= loss_mask_order
[i
++];
709 dst_pix_fmt
= avcodec_find_best_pix_fmt1(pix_fmt_mask
, src_pix_fmt
,
710 has_alpha
, loss_mask
);
711 if (dst_pix_fmt
>= 0)
719 *loss_ptr
= avcodec_get_pix_fmt_loss(dst_pix_fmt
, src_pix_fmt
, has_alpha
);
723 void ff_img_copy_plane(uint8_t *dst
, int dst_wrap
,
724 const uint8_t *src
, int src_wrap
,
725 int width
, int height
)
729 for(;height
> 0; height
--) {
730 memcpy(dst
, src
, width
);
736 void av_picture_copy(AVPicture
*dst
, const AVPicture
*src
,
737 int pix_fmt
, int width
, int height
)
740 const PixFmtInfo
*pf
= &pix_fmt_info
[pix_fmt
];
742 pf
= &pix_fmt_info
[pix_fmt
];
743 switch(pf
->pixel_type
) {
744 case FF_PIXEL_PACKED
:
746 case PIX_FMT_YUYV422
:
747 case PIX_FMT_UYVY422
:
754 case PIX_FMT_UYYVYY411
:
758 bits
= pf
->depth
* pf
->nb_channels
;
761 bwidth
= (width
* bits
+ 7) >> 3;
762 ff_img_copy_plane(dst
->data
[0], dst
->linesize
[0],
763 src
->data
[0], src
->linesize
[0],
766 case FF_PIXEL_PLANAR
:
767 for(i
= 0; i
< pf
->nb_channels
; i
++) {
771 if (i
== 1 || i
== 2) {
772 w
>>= pf
->x_chroma_shift
;
773 h
>>= pf
->y_chroma_shift
;
775 bwidth
= (w
* pf
->depth
+ 7) >> 3;
776 ff_img_copy_plane(dst
->data
[i
], dst
->linesize
[i
],
777 src
->data
[i
], src
->linesize
[i
],
781 case FF_PIXEL_PALETTE
:
782 ff_img_copy_plane(dst
->data
[0], dst
->linesize
[0],
783 src
->data
[0], src
->linesize
[0],
785 /* copy the palette */
786 ff_img_copy_plane(dst
->data
[1], dst
->linesize
[1],
787 src
->data
[1], src
->linesize
[1],
793 /* XXX: totally non optimized */
795 static void yuyv422_to_yuv420p(AVPicture
*dst
, const AVPicture
*src
,
796 int width
, int height
)
798 const uint8_t *p
, *p1
;
799 uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
807 for(;height
>= 1; height
-= 2) {
812 for(w
= width
; w
>= 2; w
-= 2) {
829 p1
+= src
->linesize
[0];
830 lum1
+= dst
->linesize
[0];
834 for(w
= width
; w
>= 2; w
-= 2) {
843 p1
+= src
->linesize
[0];
844 lum1
+= dst
->linesize
[0];
846 cb1
+= dst
->linesize
[1];
847 cr1
+= dst
->linesize
[2];
851 static void uyvy422_to_yuv420p(AVPicture
*dst
, const AVPicture
*src
,
852 int width
, int height
)
854 const uint8_t *p
, *p1
;
855 uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
864 for(;height
>= 1; height
-= 2) {
869 for(w
= width
; w
>= 2; w
-= 2) {
886 p1
+= src
->linesize
[0];
887 lum1
+= dst
->linesize
[0];
891 for(w
= width
; w
>= 2; w
-= 2) {
900 p1
+= src
->linesize
[0];
901 lum1
+= dst
->linesize
[0];
903 cb1
+= dst
->linesize
[1];
904 cr1
+= dst
->linesize
[2];
909 static void uyvy422_to_yuv422p(AVPicture
*dst
, const AVPicture
*src
,
910 int width
, int height
)
912 const uint8_t *p
, *p1
;
913 uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
920 for(;height
> 0; height
--) {
925 for(w
= width
; w
>= 2; w
-= 2) {
935 p1
+= src
->linesize
[0];
936 lum1
+= dst
->linesize
[0];
937 cb1
+= dst
->linesize
[1];
938 cr1
+= dst
->linesize
[2];
943 static void yuyv422_to_yuv422p(AVPicture
*dst
, const AVPicture
*src
,
944 int width
, int height
)
946 const uint8_t *p
, *p1
;
947 uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
954 for(;height
> 0; height
--) {
959 for(w
= width
; w
>= 2; w
-= 2) {
969 p1
+= src
->linesize
[0];
970 lum1
+= dst
->linesize
[0];
971 cb1
+= dst
->linesize
[1];
972 cr1
+= dst
->linesize
[2];
976 static void yuv422p_to_yuyv422(AVPicture
*dst
, const AVPicture
*src
,
977 int width
, int height
)
980 const uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
987 for(;height
> 0; height
--) {
992 for(w
= width
; w
>= 2; w
-= 2) {
1002 p1
+= dst
->linesize
[0];
1003 lum1
+= src
->linesize
[0];
1004 cb1
+= src
->linesize
[1];
1005 cr1
+= src
->linesize
[2];
1009 static void yuv422p_to_uyvy422(AVPicture
*dst
, const AVPicture
*src
,
1010 int width
, int height
)
1013 const uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
1017 lum1
= src
->data
[0];
1020 for(;height
> 0; height
--) {
1025 for(w
= width
; w
>= 2; w
-= 2) {
1035 p1
+= dst
->linesize
[0];
1036 lum1
+= src
->linesize
[0];
1037 cb1
+= src
->linesize
[1];
1038 cr1
+= src
->linesize
[2];
1042 static void uyyvyy411_to_yuv411p(AVPicture
*dst
, const AVPicture
*src
,
1043 int width
, int height
)
1045 const uint8_t *p
, *p1
;
1046 uint8_t *lum
, *cr
, *cb
, *lum1
, *cr1
, *cb1
;
1050 lum1
= dst
->data
[0];
1053 for(;height
> 0; height
--) {
1058 for(w
= width
; w
>= 4; w
-= 4) {
1070 p1
+= src
->linesize
[0];
1071 lum1
+= dst
->linesize
[0];
1072 cb1
+= dst
->linesize
[1];
1073 cr1
+= dst
->linesize
[2];
1078 static void yuv420p_to_yuyv422(AVPicture
*dst
, const AVPicture
*src
,
1079 int width
, int height
)
1082 uint8_t *line1
, *line2
, *linesrc
= dst
->data
[0];
1083 uint8_t *lum1
, *lum2
, *lumsrc
= src
->data
[0];
1084 uint8_t *cb1
, *cb2
= src
->data
[1];
1085 uint8_t *cr1
, *cr2
= src
->data
[2];
1087 for(h
= height
/ 2; h
--;) {
1089 line2
= linesrc
+ dst
->linesize
[0];
1092 lum2
= lumsrc
+ src
->linesize
[0];
1097 for(w
= width
/ 2; w
--;) {
1098 *line1
++ = *lum1
++; *line2
++ = *lum2
++;
1099 *line1
++ = *line2
++ = *cb1
++;
1100 *line1
++ = *lum1
++; *line2
++ = *lum2
++;
1101 *line1
++ = *line2
++ = *cr1
++;
1104 linesrc
+= dst
->linesize
[0] * 2;
1105 lumsrc
+= src
->linesize
[0] * 2;
1106 cb2
+= src
->linesize
[1];
1107 cr2
+= src
->linesize
[2];
1111 static void yuv420p_to_uyvy422(AVPicture
*dst
, const AVPicture
*src
,
1112 int width
, int height
)
1115 uint8_t *line1
, *line2
, *linesrc
= dst
->data
[0];
1116 uint8_t *lum1
, *lum2
, *lumsrc
= src
->data
[0];
1117 uint8_t *cb1
, *cb2
= src
->data
[1];
1118 uint8_t *cr1
, *cr2
= src
->data
[2];
1120 for(h
= height
/ 2; h
--;) {
1122 line2
= linesrc
+ dst
->linesize
[0];
1125 lum2
= lumsrc
+ src
->linesize
[0];
1130 for(w
= width
/ 2; w
--;) {
1131 *line1
++ = *line2
++ = *cb1
++;
1132 *line1
++ = *lum1
++; *line2
++ = *lum2
++;
1133 *line1
++ = *line2
++ = *cr1
++;
1134 *line1
++ = *lum1
++; *line2
++ = *lum2
++;
1137 linesrc
+= dst
->linesize
[0] * 2;
1138 lumsrc
+= src
->linesize
[0] * 2;
1139 cb2
+= src
->linesize
[1];
1140 cr2
+= src
->linesize
[2];
1144 #define SCALEBITS 10
1145 #define ONE_HALF (1 << (SCALEBITS - 1))
1146 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
1148 #define YUV_TO_RGB1_CCIR(cb1, cr1)\
1152 r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
1153 g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
1155 b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
1158 #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
1160 y = ((y1) - 16) * FIX(255.0/219.0);\
1161 r = cm[(y + r_add) >> SCALEBITS];\
1162 g = cm[(y + g_add) >> SCALEBITS];\
1163 b = cm[(y + b_add) >> SCALEBITS];\
1166 #define YUV_TO_RGB1(cb1, cr1)\
1170 r_add = FIX(1.40200) * cr + ONE_HALF;\
1171 g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
1172 b_add = FIX(1.77200) * cb + ONE_HALF;\
1175 #define YUV_TO_RGB2(r, g, b, y1)\
1177 y = (y1) << SCALEBITS;\
1178 r = cm[(y + r_add) >> SCALEBITS];\
1179 g = cm[(y + g_add) >> SCALEBITS];\
1180 b = cm[(y + b_add) >> SCALEBITS];\
1183 #define Y_CCIR_TO_JPEG(y)\
1184 cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
1186 #define Y_JPEG_TO_CCIR(y)\
1187 (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1189 #define C_CCIR_TO_JPEG(y)\
1190 cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
1192 /* NOTE: the clamp is really necessary! */
1193 static inline int C_JPEG_TO_CCIR(int y
) {
1194 y
= (((y
- 128) * FIX(112.0/127.0) + (ONE_HALF
+ (128 << SCALEBITS
))) >> SCALEBITS
);
1201 #define RGB_TO_Y(r, g, b) \
1202 ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
1203 FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
1205 #define RGB_TO_U(r1, g1, b1, shift)\
1206 (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
1207 FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1209 #define RGB_TO_V(r1, g1, b1, shift)\
1210 (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
1211 FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1213 #define RGB_TO_Y_CCIR(r, g, b) \
1214 ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
1215 FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
1217 #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
1218 (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
1219 FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1221 #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
1222 (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
1223 FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
1225 static uint8_t y_ccir_to_jpeg
[256];
1226 static uint8_t y_jpeg_to_ccir
[256];
1227 static uint8_t c_ccir_to_jpeg
[256];
1228 static uint8_t c_jpeg_to_ccir
[256];
1230 /* init various conversion tables */
1231 static void img_convert_init(void)
1234 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
1236 for(i
= 0;i
< 256; i
++) {
1237 y_ccir_to_jpeg
[i
] = Y_CCIR_TO_JPEG(i
);
1238 y_jpeg_to_ccir
[i
] = Y_JPEG_TO_CCIR(i
);
1239 c_ccir_to_jpeg
[i
] = C_CCIR_TO_JPEG(i
);
1240 c_jpeg_to_ccir
[i
] = C_JPEG_TO_CCIR(i
);
1244 /* apply to each pixel the given table */
1245 static void img_apply_table(uint8_t *dst
, int dst_wrap
,
1246 const uint8_t *src
, int src_wrap
,
1247 int width
, int height
, const uint8_t *table1
)
1252 const uint8_t *table
;
1255 for(;height
> 0; height
--) {
1279 /* XXX: use generic filter ? */
1280 /* XXX: in most cases, the sampling position is incorrect */
1283 static void shrink41(uint8_t *dst
, int dst_wrap
,
1284 const uint8_t *src
, int src_wrap
,
1285 int width
, int height
)
1291 for(;height
> 0; height
--) {
1294 for(w
= width
;w
> 0; w
--) {
1295 d
[0] = (s
[0] + s
[1] + s
[2] + s
[3] + 2) >> 2;
1305 static void shrink21(uint8_t *dst
, int dst_wrap
,
1306 const uint8_t *src
, int src_wrap
,
1307 int width
, int height
)
1313 for(;height
> 0; height
--) {
1316 for(w
= width
;w
> 0; w
--) {
1317 d
[0] = (s
[0] + s
[1]) >> 1;
1327 static void shrink12(uint8_t *dst
, int dst_wrap
,
1328 const uint8_t *src
, int src_wrap
,
1329 int width
, int height
)
1333 const uint8_t *s1
, *s2
;
1335 for(;height
> 0; height
--) {
1339 for(w
= width
;w
>= 4; w
-=4) {
1340 d
[0] = (s1
[0] + s2
[0]) >> 1;
1341 d
[1] = (s1
[1] + s2
[1]) >> 1;
1342 d
[2] = (s1
[2] + s2
[2]) >> 1;
1343 d
[3] = (s1
[3] + s2
[3]) >> 1;
1349 d
[0] = (s1
[0] + s2
[0]) >> 1;
1354 src
+= 2 * src_wrap
;
1360 void ff_shrink22(uint8_t *dst
, int dst_wrap
,
1361 const uint8_t *src
, int src_wrap
,
1362 int width
, int height
)
1365 const uint8_t *s1
, *s2
;
1368 for(;height
> 0; height
--) {
1372 for(w
= width
;w
>= 4; w
-=4) {
1373 d
[0] = (s1
[0] + s1
[1] + s2
[0] + s2
[1] + 2) >> 2;
1374 d
[1] = (s1
[2] + s1
[3] + s2
[2] + s2
[3] + 2) >> 2;
1375 d
[2] = (s1
[4] + s1
[5] + s2
[4] + s2
[5] + 2) >> 2;
1376 d
[3] = (s1
[6] + s1
[7] + s2
[6] + s2
[7] + 2) >> 2;
1382 d
[0] = (s1
[0] + s1
[1] + s2
[0] + s2
[1] + 2) >> 2;
1387 src
+= 2 * src_wrap
;
1393 void ff_shrink44(uint8_t *dst
, int dst_wrap
,
1394 const uint8_t *src
, int src_wrap
,
1395 int width
, int height
)
1398 const uint8_t *s1
, *s2
, *s3
, *s4
;
1401 for(;height
> 0; height
--) {
1407 for(w
= width
;w
> 0; w
--) {
1408 d
[0] = (s1
[0] + s1
[1] + s1
[2] + s1
[3] +
1409 s2
[0] + s2
[1] + s2
[2] + s2
[3] +
1410 s3
[0] + s3
[1] + s3
[2] + s3
[3] +
1411 s4
[0] + s4
[1] + s4
[2] + s4
[3] + 8) >> 4;
1418 src
+= 4 * src_wrap
;
1424 void ff_shrink88(uint8_t *dst
, int dst_wrap
,
1425 const uint8_t *src
, int src_wrap
,
1426 int width
, int height
)
1430 for(;height
> 0; height
--) {
1431 for(w
= width
;w
> 0; w
--) {
1434 tmp
+= src
[0] + src
[1] + src
[2] + src
[3] + src
[4] + src
[5] + src
[6] + src
[7];
1437 *(dst
++) = (tmp
+ 32)>>6;
1438 src
+= 8 - 8*src_wrap
;
1440 src
+= 8*src_wrap
- 8*width
;
1441 dst
+= dst_wrap
- width
;
1445 static void grow21_line(uint8_t *dst
, const uint8_t *src
,
1454 for(w
= width
;w
>= 4; w
-=4) {
1455 d
[1] = d
[0] = s1
[0];
1456 d
[3] = d
[2] = s1
[1];
1460 for(;w
>= 2; w
-= 2) {
1461 d
[1] = d
[0] = s1
[0];
1465 /* only needed if width is not a multiple of two */
1466 /* XXX: veryfy that */
1472 static void grow41_line(uint8_t *dst
, const uint8_t *src
,
1481 for(w
= width
;w
>= 4; w
-=4) {
1493 static void grow21(uint8_t *dst
, int dst_wrap
,
1494 const uint8_t *src
, int src_wrap
,
1495 int width
, int height
)
1497 for(;height
> 0; height
--) {
1498 grow21_line(dst
, src
, width
);
1505 static void grow22(uint8_t *dst
, int dst_wrap
,
1506 const uint8_t *src
, int src_wrap
,
1507 int width
, int height
)
1509 for(;height
> 0; height
--) {
1510 grow21_line(dst
, src
, width
);
1518 static void grow41(uint8_t *dst
, int dst_wrap
,
1519 const uint8_t *src
, int src_wrap
,
1520 int width
, int height
)
1522 for(;height
> 0; height
--) {
1523 grow41_line(dst
, src
, width
);
1530 static void grow44(uint8_t *dst
, int dst_wrap
,
1531 const uint8_t *src
, int src_wrap
,
1532 int width
, int height
)
1534 for(;height
> 0; height
--) {
1535 grow41_line(dst
, src
, width
);
1536 if ((height
& 3) == 1)
1543 static void conv411(uint8_t *dst
, int dst_wrap
,
1544 const uint8_t *src
, int src_wrap
,
1545 int width
, int height
)
1548 const uint8_t *s1
, *s2
;
1553 for(;height
> 0; height
--) {
1555 s2
= src
+ src_wrap
;
1557 for(w
= width
;w
> 0; w
--) {
1558 c
= (s1
[0] + s2
[0]) >> 1;
1565 src
+= src_wrap
* 2;
1570 /* XXX: add jpeg quantize code */
1572 #define TRANSP_INDEX (6*6*6)
1574 /* this is maybe slow, but allows for extensions */
1575 static inline unsigned char gif_clut_index(uint8_t r
, uint8_t g
, uint8_t b
)
1577 return ((((r
)/47)%6)*6*6+(((g
)/47)%6)*6+(((b
)/47)%6));
1580 static void build_rgb_palette(uint8_t *palette
, int has_alpha
)
1583 static const uint8_t pal_value
[6] = { 0x00, 0x33, 0x66, 0x99, 0xcc, 0xff };
1586 pal
= (uint32_t *)palette
;
1588 for(r
= 0; r
< 6; r
++) {
1589 for(g
= 0; g
< 6; g
++) {
1590 for(b
= 0; b
< 6; b
++) {
1591 pal
[i
++] = (0xff << 24) | (pal_value
[r
] << 16) |
1592 (pal_value
[g
] << 8) | pal_value
[b
];
1599 pal
[i
++] = 0xff000000;
1602 /* copy bit n to bits 0 ... n - 1 */
1603 static inline unsigned int bitcopy_n(unsigned int a
, int n
)
1606 mask
= (1 << n
) - 1;
1607 return (a
& (0xff & ~mask
)) | ((-((a
>> n
) & 1)) & mask
);
1610 /* rgb555 handling */
1612 #define RGB_NAME rgb555
1614 #define RGB_IN(r, g, b, s)\
1616 unsigned int v = ((const uint16_t *)(s))[0];\
1617 r = bitcopy_n(v >> (10 - 3), 3);\
1618 g = bitcopy_n(v >> (5 - 3), 3);\
1619 b = bitcopy_n(v << 3, 3);\
1623 #define RGB_OUT(d, r, g, b)\
1625 ((uint16_t *)(d))[0] = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);\
1630 #include "imgconvert_template.h"
1632 /* rgb565 handling */
1634 #define RGB_NAME rgb565
1636 #define RGB_IN(r, g, b, s)\
1638 unsigned int v = ((const uint16_t *)(s))[0];\
1639 r = bitcopy_n(v >> (11 - 3), 3);\
1640 g = bitcopy_n(v >> (5 - 2), 2);\
1641 b = bitcopy_n(v << 3, 3);\
1644 #define RGB_OUT(d, r, g, b)\
1646 ((uint16_t *)(d))[0] = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);\
1651 #include "imgconvert_template.h"
1653 /* bgr24 handling */
1655 #define RGB_NAME bgr24
1657 #define RGB_IN(r, g, b, s)\
1664 #define RGB_OUT(d, r, g, b)\
1673 #include "imgconvert_template.h"
1679 /* rgb24 handling */
1681 #define RGB_NAME rgb24
1684 #define RGB_IN(r, g, b, s)\
1691 #define RGB_OUT(d, r, g, b)\
1700 #include "imgconvert_template.h"
1702 /* rgb32 handling */
1704 #define RGB_NAME rgb32
1707 #define RGB_IN(r, g, b, s)\
1709 unsigned int v = ((const uint32_t *)(s))[0];\
1710 r = (v >> 16) & 0xff;\
1711 g = (v >> 8) & 0xff;\
1715 #define RGBA_IN(r, g, b, a, s)\
1717 unsigned int v = ((const uint32_t *)(s))[0];\
1718 a = (v >> 24) & 0xff;\
1719 r = (v >> 16) & 0xff;\
1720 g = (v >> 8) & 0xff;\
1724 #define RGBA_OUT(d, r, g, b, a)\
1726 ((uint32_t *)(d))[0] = (a << 24) | (r << 16) | (g << 8) | b;\
1731 #include "imgconvert_template.h"
1733 static void mono_to_gray(AVPicture
*dst
, const AVPicture
*src
,
1734 int width
, int height
, int xor_mask
)
1736 const unsigned char *p
;
1738 int v
, dst_wrap
, src_wrap
;
1742 src_wrap
= src
->linesize
[0] - ((width
+ 7) >> 3);
1745 dst_wrap
= dst
->linesize
[0] - width
;
1746 for(y
=0;y
<height
;y
++) {
1749 v
= *p
++ ^ xor_mask
;
1751 q
[1] = -((v
>> 6) & 1);
1752 q
[2] = -((v
>> 5) & 1);
1753 q
[3] = -((v
>> 4) & 1);
1754 q
[4] = -((v
>> 3) & 1);
1755 q
[5] = -((v
>> 2) & 1);
1756 q
[6] = -((v
>> 1) & 1);
1757 q
[7] = -((v
>> 0) & 1);
1762 v
= *p
++ ^ xor_mask
;
1764 q
[0] = -((v
>> 7) & 1);
1774 static void monowhite_to_gray(AVPicture
*dst
, const AVPicture
*src
,
1775 int width
, int height
)
1777 mono_to_gray(dst
, src
, width
, height
, 0xff);
1780 static void monoblack_to_gray(AVPicture
*dst
, const AVPicture
*src
,
1781 int width
, int height
)
1783 mono_to_gray(dst
, src
, width
, height
, 0x00);
1786 static void gray_to_mono(AVPicture
*dst
, const AVPicture
*src
,
1787 int width
, int height
, int xor_mask
)
1792 int j
, b
, v
, n1
, src_wrap
, dst_wrap
, y
;
1795 src_wrap
= src
->linesize
[0] - width
;
1798 dst_wrap
= dst
->linesize
[0] - ((width
+ 7) >> 3);
1800 for(y
=0;y
<height
;y
++) {
1807 v
= (v
<< 1) | (b
>> 7);
1809 d
[0] = v
^ xor_mask
;
1819 v
= (v
<< 1) | (b
>> 7);
1822 d
[0] = (v
<< (8 - (n1
& 7))) ^ xor_mask
;
1830 static void gray_to_monowhite(AVPicture
*dst
, const AVPicture
*src
,
1831 int width
, int height
)
1833 gray_to_mono(dst
, src
, width
, height
, 0xff);
1836 static void gray_to_monoblack(AVPicture
*dst
, const AVPicture
*src
,
1837 int width
, int height
)
1839 gray_to_mono(dst
, src
, width
, height
, 0x00);
1842 static void gray_to_gray16(AVPicture
*dst
, const AVPicture
*src
,
1843 int width
, int height
)
1845 int x
, y
, src_wrap
, dst_wrap
;
1848 src_wrap
= src
->linesize
[0] - width
;
1850 dst_wrap
= dst
->linesize
[0] - width
* 2;
1851 for(y
=0; y
<height
; y
++){
1852 for(x
=0; x
<width
; x
++){
1861 static void gray16_to_gray(AVPicture
*dst
, const AVPicture
*src
,
1862 int width
, int height
)
1864 int x
, y
, src_wrap
, dst_wrap
;
1867 src_wrap
= src
->linesize
[0] - width
* 2;
1869 dst_wrap
= dst
->linesize
[0] - width
;
1870 for(y
=0; y
<height
; y
++){
1871 for(x
=0; x
<width
; x
++){
1880 static void gray16be_to_gray(AVPicture
*dst
, const AVPicture
*src
,
1881 int width
, int height
)
1883 gray16_to_gray(dst
, src
, width
, height
);
1886 static void gray16le_to_gray(AVPicture
*dst
, const AVPicture
*src
,
1887 int width
, int height
)
1889 gray16_to_gray(dst
, src
+ 1, width
, height
);
1892 static void gray16_to_gray16(AVPicture
*dst
, const AVPicture
*src
,
1893 int width
, int height
)
1895 int x
, y
, src_wrap
, dst_wrap
;
1898 src_wrap
= (src
->linesize
[0] - width
* 2)/2;
1900 dst_wrap
= (dst
->linesize
[0] - width
* 2)/2;
1901 for(y
=0; y
<height
; y
++){
1902 for(x
=0; x
<width
; x
++){
1903 *d
++ = bswap_16(*s
++);
1911 typedef struct ConvertEntry
{
1912 void (*convert
)(AVPicture
*dst
,
1913 const AVPicture
*src
, int width
, int height
);
1916 /* Add each new convertion function in this table. In order to be able
1917 to convert from any format to any format, the following constraints
1920 - all FF_COLOR_RGB formats must convert to and from PIX_FMT_RGB24
1922 - all FF_COLOR_GRAY formats must convert to and from PIX_FMT_GRAY8
1924 - all FF_COLOR_RGB formats with alpha must convert to and from PIX_FMT_RGB32
1926 - PIX_FMT_YUV444P and PIX_FMT_YUVJ444P must convert to and from
1929 - PIX_FMT_422 must convert to and from PIX_FMT_422P.
1931 The other conversion functions are just optimisations for common cases.
1933 static const ConvertEntry convert_table
[PIX_FMT_NB
][PIX_FMT_NB
] = {
1934 [PIX_FMT_YUV420P
] = {
1935 [PIX_FMT_YUYV422
] = {
1936 .convert
= yuv420p_to_yuyv422
,
1938 [PIX_FMT_RGB555
] = {
1939 .convert
= yuv420p_to_rgb555
1941 [PIX_FMT_RGB565
] = {
1942 .convert
= yuv420p_to_rgb565
1945 .convert
= yuv420p_to_bgr24
1948 .convert
= yuv420p_to_rgb24
1951 .convert
= yuv420p_to_rgb32
1953 [PIX_FMT_UYVY422
] = {
1954 .convert
= yuv420p_to_uyvy422
,
1957 [PIX_FMT_YUV422P
] = {
1958 [PIX_FMT_YUYV422
] = {
1959 .convert
= yuv422p_to_yuyv422
,
1961 [PIX_FMT_UYVY422
] = {
1962 .convert
= yuv422p_to_uyvy422
,
1965 [PIX_FMT_YUV444P
] = {
1967 .convert
= yuv444p_to_rgb24
1970 [PIX_FMT_YUVJ420P
] = {
1971 [PIX_FMT_RGB555
] = {
1972 .convert
= yuvj420p_to_rgb555
1974 [PIX_FMT_RGB565
] = {
1975 .convert
= yuvj420p_to_rgb565
1978 .convert
= yuvj420p_to_bgr24
1981 .convert
= yuvj420p_to_rgb24
1984 .convert
= yuvj420p_to_rgb32
1987 [PIX_FMT_YUVJ444P
] = {
1989 .convert
= yuvj444p_to_rgb24
1992 [PIX_FMT_YUYV422
] = {
1993 [PIX_FMT_YUV420P
] = {
1994 .convert
= yuyv422_to_yuv420p
,
1996 [PIX_FMT_YUV422P
] = {
1997 .convert
= yuyv422_to_yuv422p
,
2000 [PIX_FMT_UYVY422
] = {
2001 [PIX_FMT_YUV420P
] = {
2002 .convert
= uyvy422_to_yuv420p
,
2004 [PIX_FMT_YUV422P
] = {
2005 .convert
= uyvy422_to_yuv422p
,
2009 [PIX_FMT_YUV420P
] = {
2010 .convert
= rgb24_to_yuv420p
2012 [PIX_FMT_RGB565
] = {
2013 .convert
= rgb24_to_rgb565
2015 [PIX_FMT_RGB555
] = {
2016 .convert
= rgb24_to_rgb555
2019 .convert
= rgb24_to_rgb32
2022 .convert
= rgb24_to_bgr24
2025 .convert
= rgb24_to_gray
2028 .convert
= rgb24_to_pal8
2030 [PIX_FMT_YUV444P
] = {
2031 .convert
= rgb24_to_yuv444p
2033 [PIX_FMT_YUVJ420P
] = {
2034 .convert
= rgb24_to_yuvj420p
2036 [PIX_FMT_YUVJ444P
] = {
2037 .convert
= rgb24_to_yuvj444p
2042 .convert
= rgb32_to_rgb24
2045 .convert
= rgb32_to_bgr24
2047 [PIX_FMT_RGB565
] = {
2048 .convert
= rgb32_to_rgb565
2050 [PIX_FMT_RGB555
] = {
2051 .convert
= rgb32_to_rgb555
2054 .convert
= rgb32_to_pal8
2056 [PIX_FMT_YUV420P
] = {
2057 .convert
= rgb32_to_yuv420p
2060 .convert
= rgb32_to_gray
2065 .convert
= bgr24_to_rgb32
2068 .convert
= bgr24_to_rgb24
2070 [PIX_FMT_YUV420P
] = {
2071 .convert
= bgr24_to_yuv420p
2074 .convert
= bgr24_to_gray
2077 [PIX_FMT_RGB555
] = {
2079 .convert
= rgb555_to_rgb24
2082 .convert
= rgb555_to_rgb32
2084 [PIX_FMT_YUV420P
] = {
2085 .convert
= rgb555_to_yuv420p
2088 .convert
= rgb555_to_gray
2091 [PIX_FMT_RGB565
] = {
2093 .convert
= rgb565_to_rgb32
2096 .convert
= rgb565_to_rgb24
2098 [PIX_FMT_YUV420P
] = {
2099 .convert
= rgb565_to_yuv420p
2102 .convert
= rgb565_to_gray
2105 [PIX_FMT_GRAY16BE
] = {
2107 .convert
= gray16be_to_gray
2109 [PIX_FMT_GRAY16LE
] = {
2110 .convert
= gray16_to_gray16
2113 [PIX_FMT_GRAY16LE
] = {
2115 .convert
= gray16le_to_gray
2117 [PIX_FMT_GRAY16BE
] = {
2118 .convert
= gray16_to_gray16
2122 [PIX_FMT_RGB555
] = {
2123 .convert
= gray_to_rgb555
2125 [PIX_FMT_RGB565
] = {
2126 .convert
= gray_to_rgb565
2129 .convert
= gray_to_rgb24
2132 .convert
= gray_to_bgr24
2135 .convert
= gray_to_rgb32
2137 [PIX_FMT_MONOWHITE
] = {
2138 .convert
= gray_to_monowhite
2140 [PIX_FMT_MONOBLACK
] = {
2141 .convert
= gray_to_monoblack
2143 [PIX_FMT_GRAY16LE
] = {
2144 .convert
= gray_to_gray16
2146 [PIX_FMT_GRAY16BE
] = {
2147 .convert
= gray_to_gray16
2150 [PIX_FMT_MONOWHITE
] = {
2152 .convert
= monowhite_to_gray
2155 [PIX_FMT_MONOBLACK
] = {
2157 .convert
= monoblack_to_gray
2161 [PIX_FMT_RGB555
] = {
2162 .convert
= pal8_to_rgb555
2164 [PIX_FMT_RGB565
] = {
2165 .convert
= pal8_to_rgb565
2168 .convert
= pal8_to_bgr24
2171 .convert
= pal8_to_rgb24
2174 .convert
= pal8_to_rgb32
2177 [PIX_FMT_UYYVYY411
] = {
2178 [PIX_FMT_YUV411P
] = {
2179 .convert
= uyyvyy411_to_yuv411p
,
2185 int avpicture_alloc(AVPicture
*picture
,
2186 int pix_fmt
, int width
, int height
)
2191 size
= avpicture_get_size(pix_fmt
, width
, height
);
2194 ptr
= av_malloc(size
);
2197 avpicture_fill(picture
, ptr
, pix_fmt
, width
, height
);
2200 memset(picture
, 0, sizeof(AVPicture
));
2204 void avpicture_free(AVPicture
*picture
)
2206 av_free(picture
->data
[0]);
2209 /* return true if yuv planar */
2210 static inline int is_yuv_planar(const PixFmtInfo
*ps
)
2212 return (ps
->color_type
== FF_COLOR_YUV
||
2213 ps
->color_type
== FF_COLOR_YUV_JPEG
) &&
2214 ps
->pixel_type
== FF_PIXEL_PLANAR
;
2217 int av_picture_crop(AVPicture
*dst
, const AVPicture
*src
,
2218 int pix_fmt
, int top_band
, int left_band
)
2223 if (pix_fmt
< 0 || pix_fmt
>= PIX_FMT_NB
|| !is_yuv_planar(&pix_fmt_info
[pix_fmt
]))
2226 y_shift
= pix_fmt_info
[pix_fmt
].y_chroma_shift
;
2227 x_shift
= pix_fmt_info
[pix_fmt
].x_chroma_shift
;
2229 dst
->data
[0] = src
->data
[0] + (top_band
* src
->linesize
[0]) + left_band
;
2230 dst
->data
[1] = src
->data
[1] + ((top_band
>> y_shift
) * src
->linesize
[1]) + (left_band
>> x_shift
);
2231 dst
->data
[2] = src
->data
[2] + ((top_band
>> y_shift
) * src
->linesize
[2]) + (left_band
>> x_shift
);
2233 dst
->linesize
[0] = src
->linesize
[0];
2234 dst
->linesize
[1] = src
->linesize
[1];
2235 dst
->linesize
[2] = src
->linesize
[2];
2239 int av_picture_pad(AVPicture
*dst
, const AVPicture
*src
, int height
, int width
,
2240 int pix_fmt
, int padtop
, int padbottom
, int padleft
, int padright
,
2249 if (pix_fmt
< 0 || pix_fmt
>= PIX_FMT_NB
||
2250 !is_yuv_planar(&pix_fmt_info
[pix_fmt
])) return -1;
2252 for (i
= 0; i
< 3; i
++) {
2253 x_shift
= i ? pix_fmt_info
[pix_fmt
].x_chroma_shift
: 0;
2254 y_shift
= i ? pix_fmt_info
[pix_fmt
].y_chroma_shift
: 0;
2256 if (padtop
|| padleft
) {
2257 memset(dst
->data
[i
], color
[i
],
2258 dst
->linesize
[i
] * (padtop
>> y_shift
) + (padleft
>> x_shift
));
2261 if (padleft
|| padright
) {
2262 optr
= dst
->data
[i
] + dst
->linesize
[i
] * (padtop
>> y_shift
) +
2263 (dst
->linesize
[i
] - (padright
>> x_shift
));
2264 yheight
= (height
- 1 - (padtop
+ padbottom
)) >> y_shift
;
2265 for (y
= 0; y
< yheight
; y
++) {
2266 memset(optr
, color
[i
], (padleft
+ padright
) >> x_shift
);
2267 optr
+= dst
->linesize
[i
];
2271 if (src
) { /* first line */
2272 uint8_t *iptr
= src
->data
[i
];
2273 optr
= dst
->data
[i
] + dst
->linesize
[i
] * (padtop
>> y_shift
) +
2274 (padleft
>> x_shift
);
2275 memcpy(optr
, iptr
, src
->linesize
[i
]);
2276 iptr
+= src
->linesize
[i
];
2277 optr
= dst
->data
[i
] + dst
->linesize
[i
] * (padtop
>> y_shift
) +
2278 (dst
->linesize
[i
] - (padright
>> x_shift
));
2279 yheight
= (height
- 1 - (padtop
+ padbottom
)) >> y_shift
;
2280 for (y
= 0; y
< yheight
; y
++) {
2281 memset(optr
, color
[i
], (padleft
+ padright
) >> x_shift
);
2282 memcpy(optr
+ ((padleft
+ padright
) >> x_shift
), iptr
,
2284 iptr
+= src
->linesize
[i
];
2285 optr
+= dst
->linesize
[i
];
2289 if (padbottom
|| padright
) {
2290 optr
= dst
->data
[i
] + dst
->linesize
[i
] *
2291 ((height
- padbottom
) >> y_shift
) - (padright
>> x_shift
);
2292 memset(optr
, color
[i
],dst
->linesize
[i
] *
2293 (padbottom
>> y_shift
) + (padright
>> x_shift
));
2299 #if LIBAVCODEC_VERSION_INT < ((52<<16)+(0<<8)+0)
2300 void img_copy(AVPicture
*dst
, const AVPicture
*src
,
2301 int pix_fmt
, int width
, int height
)
2303 av_picture_copy(dst
, src
, pix_fmt
, width
, height
);
2306 int img_crop(AVPicture
*dst
, const AVPicture
*src
,
2307 int pix_fmt
, int top_band
, int left_band
)
2309 return av_picture_crop(dst
, src
, pix_fmt
, top_band
, left_band
);
2312 int img_pad(AVPicture
*dst
, const AVPicture
*src
, int height
, int width
,
2313 int pix_fmt
, int padtop
, int padbottom
, int padleft
, int padright
,
2316 return av_picture_pad(dst
, src
, height
, width
, pix_fmt
, padtop
, padbottom
, padleft
, padright
, color
);
2320 #ifndef CONFIG_SWSCALER
2321 /* XXX: always use linesize. Return -1 if not supported */
2322 int img_convert(AVPicture
*dst
, int dst_pix_fmt
,
2323 const AVPicture
*src
, int src_pix_fmt
,
2324 int src_width
, int src_height
)
2327 int i
, ret
, dst_width
, dst_height
, int_pix_fmt
;
2328 const PixFmtInfo
*src_pix
, *dst_pix
;
2329 const ConvertEntry
*ce
;
2330 AVPicture tmp1
, *tmp
= &tmp1
;
2332 if (src_pix_fmt
< 0 || src_pix_fmt
>= PIX_FMT_NB
||
2333 dst_pix_fmt
< 0 || dst_pix_fmt
>= PIX_FMT_NB
)
2335 if (src_width
<= 0 || src_height
<= 0)
2343 dst_width
= src_width
;
2344 dst_height
= src_height
;
2346 dst_pix
= &pix_fmt_info
[dst_pix_fmt
];
2347 src_pix
= &pix_fmt_info
[src_pix_fmt
];
2348 if (src_pix_fmt
== dst_pix_fmt
) {
2349 /* no conversion needed: just copy */
2350 av_picture_copy(dst
, src
, dst_pix_fmt
, dst_width
, dst_height
);
2354 ce
= &convert_table
[src_pix_fmt
][dst_pix_fmt
];
2356 /* specific conversion routine */
2357 ce
->convert(dst
, src
, dst_width
, dst_height
);
2362 if (is_yuv_planar(dst_pix
) &&
2363 src_pix_fmt
== PIX_FMT_GRAY8
) {
2367 if (dst_pix
->color_type
== FF_COLOR_YUV_JPEG
) {
2368 ff_img_copy_plane(dst
->data
[0], dst
->linesize
[0],
2369 src
->data
[0], src
->linesize
[0],
2370 dst_width
, dst_height
);
2372 img_apply_table(dst
->data
[0], dst
->linesize
[0],
2373 src
->data
[0], src
->linesize
[0],
2374 dst_width
, dst_height
,
2377 /* fill U and V with 128 */
2380 w
>>= dst_pix
->x_chroma_shift
;
2381 h
>>= dst_pix
->y_chroma_shift
;
2382 for(i
= 1; i
<= 2; i
++) {
2384 for(y
= 0; y
< h
; y
++) {
2386 d
+= dst
->linesize
[i
];
2393 if (is_yuv_planar(src_pix
) &&
2394 dst_pix_fmt
== PIX_FMT_GRAY8
) {
2395 if (src_pix
->color_type
== FF_COLOR_YUV_JPEG
) {
2396 ff_img_copy_plane(dst
->data
[0], dst
->linesize
[0],
2397 src
->data
[0], src
->linesize
[0],
2398 dst_width
, dst_height
);
2400 img_apply_table(dst
->data
[0], dst
->linesize
[0],
2401 src
->data
[0], src
->linesize
[0],
2402 dst_width
, dst_height
,
2408 /* YUV to YUV planar */
2409 if (is_yuv_planar(dst_pix
) && is_yuv_planar(src_pix
)) {
2410 int x_shift
, y_shift
, w
, h
, xy_shift
;
2411 void (*resize_func
)(uint8_t *dst
, int dst_wrap
,
2412 const uint8_t *src
, int src_wrap
,
2413 int width
, int height
);
2415 /* compute chroma size of the smallest dimensions */
2418 if (dst_pix
->x_chroma_shift
>= src_pix
->x_chroma_shift
)
2419 w
>>= dst_pix
->x_chroma_shift
;
2421 w
>>= src_pix
->x_chroma_shift
;
2422 if (dst_pix
->y_chroma_shift
>= src_pix
->y_chroma_shift
)
2423 h
>>= dst_pix
->y_chroma_shift
;
2425 h
>>= src_pix
->y_chroma_shift
;
2427 x_shift
= (dst_pix
->x_chroma_shift
- src_pix
->x_chroma_shift
);
2428 y_shift
= (dst_pix
->y_chroma_shift
- src_pix
->y_chroma_shift
);
2429 xy_shift
= ((x_shift
& 0xf) << 4) | (y_shift
& 0xf);
2430 /* there must be filters for conversion at least from and to
2434 resize_func
= ff_img_copy_plane
;
2437 resize_func
= shrink21
;
2440 resize_func
= shrink41
;
2443 resize_func
= shrink12
;
2446 resize_func
= ff_shrink22
;
2449 resize_func
= ff_shrink44
;
2452 resize_func
= grow21
;
2455 resize_func
= grow41
;
2458 resize_func
= grow22
;
2461 resize_func
= grow44
;
2464 resize_func
= conv411
;
2467 /* currently not handled */
2468 goto no_chroma_filter
;
2471 ff_img_copy_plane(dst
->data
[0], dst
->linesize
[0],
2472 src
->data
[0], src
->linesize
[0],
2473 dst_width
, dst_height
);
2475 for(i
= 1;i
<= 2; i
++)
2476 resize_func(dst
->data
[i
], dst
->linesize
[i
],
2477 src
->data
[i
], src
->linesize
[i
],
2478 dst_width
>>dst_pix
->x_chroma_shift
, dst_height
>>dst_pix
->y_chroma_shift
);
2479 /* if yuv color space conversion is needed, we do it here on
2480 the destination image */
2481 if (dst_pix
->color_type
!= src_pix
->color_type
) {
2482 const uint8_t *y_table
, *c_table
;
2483 if (dst_pix
->color_type
== FF_COLOR_YUV
) {
2484 y_table
= y_jpeg_to_ccir
;
2485 c_table
= c_jpeg_to_ccir
;
2487 y_table
= y_ccir_to_jpeg
;
2488 c_table
= c_ccir_to_jpeg
;
2490 img_apply_table(dst
->data
[0], dst
->linesize
[0],
2491 dst
->data
[0], dst
->linesize
[0],
2492 dst_width
, dst_height
,
2495 for(i
= 1;i
<= 2; i
++)
2496 img_apply_table(dst
->data
[i
], dst
->linesize
[i
],
2497 dst
->data
[i
], dst
->linesize
[i
],
2498 dst_width
>>dst_pix
->x_chroma_shift
,
2499 dst_height
>>dst_pix
->y_chroma_shift
,
2506 /* try to use an intermediate format */
2507 if (src_pix_fmt
== PIX_FMT_YUYV422
||
2508 dst_pix_fmt
== PIX_FMT_YUYV422
) {
2509 /* specific case: convert to YUV422P first */
2510 int_pix_fmt
= PIX_FMT_YUV422P
;
2511 } else if (src_pix_fmt
== PIX_FMT_UYVY422
||
2512 dst_pix_fmt
== PIX_FMT_UYVY422
) {
2513 /* specific case: convert to YUV422P first */
2514 int_pix_fmt
= PIX_FMT_YUV422P
;
2515 } else if (src_pix_fmt
== PIX_FMT_UYYVYY411
||
2516 dst_pix_fmt
== PIX_FMT_UYYVYY411
) {
2517 /* specific case: convert to YUV411P first */
2518 int_pix_fmt
= PIX_FMT_YUV411P
;
2519 } else if ((src_pix
->color_type
== FF_COLOR_GRAY
&&
2520 src_pix_fmt
!= PIX_FMT_GRAY8
) ||
2521 (dst_pix
->color_type
== FF_COLOR_GRAY
&&
2522 dst_pix_fmt
!= PIX_FMT_GRAY8
)) {
2523 /* gray8 is the normalized format */
2524 int_pix_fmt
= PIX_FMT_GRAY8
;
2525 } else if ((is_yuv_planar(src_pix
) &&
2526 src_pix_fmt
!= PIX_FMT_YUV444P
&&
2527 src_pix_fmt
!= PIX_FMT_YUVJ444P
)) {
2528 /* yuv444 is the normalized format */
2529 if (src_pix
->color_type
== FF_COLOR_YUV_JPEG
)
2530 int_pix_fmt
= PIX_FMT_YUVJ444P
;
2532 int_pix_fmt
= PIX_FMT_YUV444P
;
2533 } else if ((is_yuv_planar(dst_pix
) &&
2534 dst_pix_fmt
!= PIX_FMT_YUV444P
&&
2535 dst_pix_fmt
!= PIX_FMT_YUVJ444P
)) {
2536 /* yuv444 is the normalized format */
2537 if (dst_pix
->color_type
== FF_COLOR_YUV_JPEG
)
2538 int_pix_fmt
= PIX_FMT_YUVJ444P
;
2540 int_pix_fmt
= PIX_FMT_YUV444P
;
2542 /* the two formats are rgb or gray8 or yuv[j]444p */
2543 if (src_pix
->is_alpha
&& dst_pix
->is_alpha
)
2544 int_pix_fmt
= PIX_FMT_RGB32
;
2546 int_pix_fmt
= PIX_FMT_RGB24
;
2548 if (avpicture_alloc(tmp
, int_pix_fmt
, dst_width
, dst_height
) < 0)
2551 if (img_convert(tmp
, int_pix_fmt
,
2552 src
, src_pix_fmt
, src_width
, src_height
) < 0)
2554 if (img_convert(dst
, dst_pix_fmt
,
2555 tmp
, int_pix_fmt
, dst_width
, dst_height
) < 0)
2559 avpicture_free(tmp
);
2564 /* NOTE: we scan all the pixels to have an exact information */
2565 static int get_alpha_info_pal8(const AVPicture
*src
, int width
, int height
)
2567 const unsigned char *p
;
2568 int src_wrap
, ret
, x
, y
;
2570 uint32_t *palette
= (uint32_t *)src
->data
[1];
2573 src_wrap
= src
->linesize
[0] - width
;
2575 for(y
=0;y
<height
;y
++) {
2576 for(x
=0;x
<width
;x
++) {
2577 a
= palette
[p
[0]] >> 24;
2579 ret
|= FF_ALPHA_TRANSP
;
2580 } else if (a
!= 0xff) {
2581 ret
|= FF_ALPHA_SEMI_TRANSP
;
2590 int img_get_alpha_info(const AVPicture
*src
,
2591 int pix_fmt
, int width
, int height
)
2593 const PixFmtInfo
*pf
= &pix_fmt_info
[pix_fmt
];
2596 pf
= &pix_fmt_info
[pix_fmt
];
2597 /* no alpha can be represented in format */
2602 ret
= get_alpha_info_rgb32(src
, width
, height
);
2605 ret
= get_alpha_info_pal8(src
, width
, height
);
2608 /* we do not know, so everything is indicated */
2609 ret
= FF_ALPHA_TRANSP
| FF_ALPHA_SEMI_TRANSP
;
2616 #define DEINT_INPLACE_LINE_LUM \
2617 movd_m2r(lum_m4[0],mm0);\
2618 movd_m2r(lum_m3[0],mm1);\
2619 movd_m2r(lum_m2[0],mm2);\
2620 movd_m2r(lum_m1[0],mm3);\
2621 movd_m2r(lum[0],mm4);\
2622 punpcklbw_r2r(mm7,mm0);\
2623 movd_r2m(mm2,lum_m4[0]);\
2624 punpcklbw_r2r(mm7,mm1);\
2625 punpcklbw_r2r(mm7,mm2);\
2626 punpcklbw_r2r(mm7,mm3);\
2627 punpcklbw_r2r(mm7,mm4);\
2628 paddw_r2r(mm3,mm1);\
2630 paddw_r2r(mm4,mm0);\
2632 paddw_r2r(mm6,mm2);\
2633 paddw_r2r(mm2,mm1);\
2634 psubusw_r2r(mm0,mm1);\
2636 packuswb_r2r(mm7,mm1);\
2637 movd_r2m(mm1,lum_m2[0]);
2639 #define DEINT_LINE_LUM \
2640 movd_m2r(lum_m4[0],mm0);\
2641 movd_m2r(lum_m3[0],mm1);\
2642 movd_m2r(lum_m2[0],mm2);\
2643 movd_m2r(lum_m1[0],mm3);\
2644 movd_m2r(lum[0],mm4);\
2645 punpcklbw_r2r(mm7,mm0);\
2646 punpcklbw_r2r(mm7,mm1);\
2647 punpcklbw_r2r(mm7,mm2);\
2648 punpcklbw_r2r(mm7,mm3);\
2649 punpcklbw_r2r(mm7,mm4);\
2650 paddw_r2r(mm3,mm1);\
2652 paddw_r2r(mm4,mm0);\
2654 paddw_r2r(mm6,mm2);\
2655 paddw_r2r(mm2,mm1);\
2656 psubusw_r2r(mm0,mm1);\
2658 packuswb_r2r(mm7,mm1);\
2659 movd_r2m(mm1,dst[0]);
2662 /* filter parameters: [-1 4 2 4 -1] // 8 */
2663 static void deinterlace_line(uint8_t *dst
,
2664 const uint8_t *lum_m4
, const uint8_t *lum_m3
,
2665 const uint8_t *lum_m2
, const uint8_t *lum_m1
,
2670 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
2673 for(;size
> 0;size
--) {
2675 sum
+= lum_m3
[0] << 2;
2676 sum
+= lum_m2
[0] << 1;
2677 sum
+= lum_m1
[0] << 2;
2679 dst
[0] = cm
[(sum
+ 4) >> 3];
2696 movq_m2r(rounder
,mm6
);
2698 for (;size
> 3; size
-=4) {
2709 static void deinterlace_line_inplace(uint8_t *lum_m4
, uint8_t *lum_m3
, uint8_t *lum_m2
, uint8_t *lum_m1
, uint8_t *lum
,
2713 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
2716 for(;size
> 0;size
--) {
2718 sum
+= lum_m3
[0] << 2;
2719 sum
+= lum_m2
[0] << 1;
2720 lum_m4
[0]=lum_m2
[0];
2721 sum
+= lum_m1
[0] << 2;
2723 lum_m2
[0] = cm
[(sum
+ 4) >> 3];
2739 movq_m2r(rounder
,mm6
);
2741 for (;size
> 3; size
-=4) {
2742 DEINT_INPLACE_LINE_LUM
2752 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
2753 top field is copied as is, but the bottom field is deinterlaced
2754 against the top field. */
2755 static void deinterlace_bottom_field(uint8_t *dst
, int dst_wrap
,
2756 const uint8_t *src1
, int src_wrap
,
2757 int width
, int height
)
2759 const uint8_t *src_m2
, *src_m1
, *src_0
, *src_p1
, *src_p2
;
2764 src_0
=&src_m1
[src_wrap
];
2765 src_p1
=&src_0
[src_wrap
];
2766 src_p2
=&src_p1
[src_wrap
];
2767 for(y
=0;y
<(height
-2);y
+=2) {
2768 memcpy(dst
,src_m1
,width
);
2770 deinterlace_line(dst
,src_m2
,src_m1
,src_0
,src_p1
,src_p2
,width
);
2774 src_p1
+= 2*src_wrap
;
2775 src_p2
+= 2*src_wrap
;
2778 memcpy(dst
,src_m1
,width
);
2781 deinterlace_line(dst
,src_m2
,src_m1
,src_0
,src_0
,src_0
,width
);
2784 static void deinterlace_bottom_field_inplace(uint8_t *src1
, int src_wrap
,
2785 int width
, int height
)
2787 uint8_t *src_m1
, *src_0
, *src_p1
, *src_p2
;
2790 buf
= (uint8_t*)av_malloc(width
);
2793 memcpy(buf
,src_m1
,width
);
2794 src_0
=&src_m1
[src_wrap
];
2795 src_p1
=&src_0
[src_wrap
];
2796 src_p2
=&src_p1
[src_wrap
];
2797 for(y
=0;y
<(height
-2);y
+=2) {
2798 deinterlace_line_inplace(buf
,src_m1
,src_0
,src_p1
,src_p2
,width
);
2801 src_p1
+= 2*src_wrap
;
2802 src_p2
+= 2*src_wrap
;
2805 deinterlace_line_inplace(buf
,src_m1
,src_0
,src_0
,src_0
,width
);
2809 int avpicture_deinterlace(AVPicture
*dst
, const AVPicture
*src
,
2810 int pix_fmt
, int width
, int height
)
2814 if (pix_fmt
!= PIX_FMT_YUV420P
&&
2815 pix_fmt
!= PIX_FMT_YUV422P
&&
2816 pix_fmt
!= PIX_FMT_YUV444P
&&
2817 pix_fmt
!= PIX_FMT_YUV411P
)
2819 if ((width
& 3) != 0 || (height
& 3) != 0)
2825 case PIX_FMT_YUV420P
:
2829 case PIX_FMT_YUV422P
:
2832 case PIX_FMT_YUV411P
:
2840 deinterlace_bottom_field_inplace(dst
->data
[i
], dst
->linesize
[i
],
2843 deinterlace_bottom_field(dst
->data
[i
],dst
->linesize
[i
],
2844 src
->data
[i
], src
->linesize
[i
],