2 * Misc image conversion 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 conversion 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
36 #include "libavutil/avassert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/common.h"
39 #include "libavutil/pixdesc.h"
40 #include "libavutil/imgutils.h"
43 #include "x86/dsputil_mmx.h"
46 #define FF_COLOR_RGB 0 /**< RGB color space */
47 #define FF_COLOR_GRAY 1 /**< gray color space */
48 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
49 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
52 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
53 #define deinterlace_line ff_deinterlace_line_mmx
55 #define deinterlace_line_inplace deinterlace_line_inplace_c
56 #define deinterlace_line deinterlace_line_c
59 #define pixdesc_has_alpha(pixdesc) \
60 ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
62 typedef struct PixFmtInfo {
63 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
64 uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
67 /* this table gives more information about formats */
68 static const PixFmtInfo pix_fmt_info[AV_PIX_FMT_NB] = {
70 [AV_PIX_FMT_YUV420P] = {
71 .color_type = FF_COLOR_YUV,
73 [AV_PIX_FMT_YUV422P] = {
74 .color_type = FF_COLOR_YUV,
76 [AV_PIX_FMT_YUV444P] = {
77 .color_type = FF_COLOR_YUV,
79 [AV_PIX_FMT_YUYV422] = {
80 .color_type = FF_COLOR_YUV,
82 [AV_PIX_FMT_UYVY422] = {
83 .color_type = FF_COLOR_YUV,
85 [AV_PIX_FMT_YUV410P] = {
86 .color_type = FF_COLOR_YUV,
88 [AV_PIX_FMT_YUV411P] = {
89 .color_type = FF_COLOR_YUV,
91 [AV_PIX_FMT_YUV440P] = {
92 .color_type = FF_COLOR_YUV,
94 [AV_PIX_FMT_YUV420P9LE] = {
95 .color_type = FF_COLOR_YUV,
97 [AV_PIX_FMT_YUV422P9LE] = {
98 .color_type = FF_COLOR_YUV,
100 [AV_PIX_FMT_YUV444P9LE] = {
101 .color_type = FF_COLOR_YUV,
103 [AV_PIX_FMT_YUV420P9BE] = {
104 .color_type = FF_COLOR_YUV,
106 [AV_PIX_FMT_YUV422P9BE] = {
107 .color_type = FF_COLOR_YUV,
109 [AV_PIX_FMT_YUV444P9BE] = {
110 .color_type = FF_COLOR_YUV,
112 [AV_PIX_FMT_YUV420P10LE] = {
113 .color_type = FF_COLOR_YUV,
115 [AV_PIX_FMT_YUV422P10LE] = {
116 .color_type = FF_COLOR_YUV,
118 [AV_PIX_FMT_YUV444P10LE] = {
119 .color_type = FF_COLOR_YUV,
121 [AV_PIX_FMT_YUV420P10BE] = {
122 .color_type = FF_COLOR_YUV,
124 [AV_PIX_FMT_YUV422P10BE] = {
125 .color_type = FF_COLOR_YUV,
127 [AV_PIX_FMT_YUV444P10BE] = {
128 .color_type = FF_COLOR_YUV,
130 [AV_PIX_FMT_YUV420P12LE] = {
131 .color_type = FF_COLOR_YUV,
133 [AV_PIX_FMT_YUV422P12LE] = {
134 .color_type = FF_COLOR_YUV,
136 [AV_PIX_FMT_YUV444P12LE] = {
137 .color_type = FF_COLOR_YUV,
139 [AV_PIX_FMT_YUV420P12BE] = {
140 .color_type = FF_COLOR_YUV,
142 [AV_PIX_FMT_YUV422P12BE] = {
143 .color_type = FF_COLOR_YUV,
145 [AV_PIX_FMT_YUV444P12BE] = {
146 .color_type = FF_COLOR_YUV,
148 [AV_PIX_FMT_YUV420P14LE] = {
149 .color_type = FF_COLOR_YUV,
151 [AV_PIX_FMT_YUV422P14LE] = {
152 .color_type = FF_COLOR_YUV,
154 [AV_PIX_FMT_YUV444P14LE] = {
155 .color_type = FF_COLOR_YUV,
157 [AV_PIX_FMT_YUV420P14BE] = {
158 .color_type = FF_COLOR_YUV,
160 [AV_PIX_FMT_YUV422P14BE] = {
161 .color_type = FF_COLOR_YUV,
163 [AV_PIX_FMT_YUV444P14BE] = {
164 .color_type = FF_COLOR_YUV,
166 [AV_PIX_FMT_YUV420P16LE] = {
167 .color_type = FF_COLOR_YUV,
169 [AV_PIX_FMT_YUV422P16LE] = {
170 .color_type = FF_COLOR_YUV,
172 [AV_PIX_FMT_YUV444P16LE] = {
173 .color_type = FF_COLOR_YUV,
175 [AV_PIX_FMT_YUV420P16BE] = {
176 .color_type = FF_COLOR_YUV,
178 [AV_PIX_FMT_YUV422P16BE] = {
179 .color_type = FF_COLOR_YUV,
181 [AV_PIX_FMT_YUV444P16BE] = {
182 .color_type = FF_COLOR_YUV,
185 /* YUV formats with alpha plane */
186 [AV_PIX_FMT_YUVA420P] = {
187 .color_type = FF_COLOR_YUV,
190 [AV_PIX_FMT_YUVA422P] = {
191 .color_type = FF_COLOR_YUV,
194 [AV_PIX_FMT_YUVA444P] = {
195 .color_type = FF_COLOR_YUV,
197 [AV_PIX_FMT_YUVA420P9LE] = {
198 .color_type = FF_COLOR_YUV,
200 [AV_PIX_FMT_YUVA422P9LE] = {
201 .color_type = FF_COLOR_YUV,
203 [AV_PIX_FMT_YUVA444P9LE] = {
204 .color_type = FF_COLOR_YUV,
206 [AV_PIX_FMT_YUVA420P9BE] = {
207 .color_type = FF_COLOR_YUV,
209 [AV_PIX_FMT_YUVA422P9BE] = {
210 .color_type = FF_COLOR_YUV,
212 [AV_PIX_FMT_YUVA444P9BE] = {
213 .color_type = FF_COLOR_YUV,
215 [AV_PIX_FMT_YUVA420P10LE] = {
216 .color_type = FF_COLOR_YUV,
218 [AV_PIX_FMT_YUVA422P10LE] = {
219 .color_type = FF_COLOR_YUV,
221 [AV_PIX_FMT_YUVA444P10LE] = {
222 .color_type = FF_COLOR_YUV,
224 [AV_PIX_FMT_YUVA420P10BE] = {
225 .color_type = FF_COLOR_YUV,
227 [AV_PIX_FMT_YUVA422P10BE] = {
228 .color_type = FF_COLOR_YUV,
230 [AV_PIX_FMT_YUVA444P10BE] = {
231 .color_type = FF_COLOR_YUV,
235 [AV_PIX_FMT_YUVJ420P] = {
236 .color_type = FF_COLOR_YUV_JPEG,
238 [AV_PIX_FMT_YUVJ422P] = {
239 .color_type = FF_COLOR_YUV_JPEG,
241 [AV_PIX_FMT_YUVJ444P] = {
242 .color_type = FF_COLOR_YUV_JPEG,
244 [AV_PIX_FMT_YUVJ440P] = {
245 .color_type = FF_COLOR_YUV_JPEG,
249 [AV_PIX_FMT_RGB24] = {
250 .color_type = FF_COLOR_RGB,
252 [AV_PIX_FMT_BGR24] = {
253 .color_type = FF_COLOR_RGB,
255 [AV_PIX_FMT_ARGB] = {
256 .color_type = FF_COLOR_RGB,
258 [AV_PIX_FMT_RGB48BE] = {
259 .color_type = FF_COLOR_RGB,
261 [AV_PIX_FMT_RGB48LE] = {
262 .color_type = FF_COLOR_RGB,
264 [AV_PIX_FMT_RGBA64BE] = {
265 .color_type = FF_COLOR_RGB,
267 [AV_PIX_FMT_RGBA64LE] = {
268 .color_type = FF_COLOR_RGB,
270 [AV_PIX_FMT_RGB565BE] = {
271 .color_type = FF_COLOR_RGB,
273 [AV_PIX_FMT_RGB565LE] = {
274 .color_type = FF_COLOR_RGB,
276 [AV_PIX_FMT_RGB555BE] = {
277 .color_type = FF_COLOR_RGB,
280 [AV_PIX_FMT_RGB555LE] = {
281 .color_type = FF_COLOR_RGB,
284 [AV_PIX_FMT_RGB444BE] = {
285 .color_type = FF_COLOR_RGB,
288 [AV_PIX_FMT_RGB444LE] = {
289 .color_type = FF_COLOR_RGB,
293 /* gray / mono formats */
294 [AV_PIX_FMT_GRAY16BE] = {
295 .color_type = FF_COLOR_GRAY,
297 [AV_PIX_FMT_GRAY16LE] = {
298 .color_type = FF_COLOR_GRAY,
300 [AV_PIX_FMT_GRAY8] = {
301 .color_type = FF_COLOR_GRAY,
303 [AV_PIX_FMT_GRAY8A] = {
304 .color_type = FF_COLOR_GRAY,
306 [AV_PIX_FMT_MONOWHITE] = {
307 .color_type = FF_COLOR_GRAY,
309 [AV_PIX_FMT_MONOBLACK] = {
310 .color_type = FF_COLOR_GRAY,
313 /* paletted formats */
314 [AV_PIX_FMT_PAL8] = {
315 .color_type = FF_COLOR_RGB,
317 [AV_PIX_FMT_UYYVYY411] = {
318 .color_type = FF_COLOR_YUV,
320 [AV_PIX_FMT_ABGR] = {
321 .color_type = FF_COLOR_RGB,
323 [AV_PIX_FMT_BGR48BE] = {
324 .color_type = FF_COLOR_RGB,
326 [AV_PIX_FMT_BGR48LE] = {
327 .color_type = FF_COLOR_RGB,
329 [AV_PIX_FMT_BGRA64BE] = {
330 .color_type = FF_COLOR_RGB,
332 [AV_PIX_FMT_BGRA64LE] = {
333 .color_type = FF_COLOR_RGB,
335 [AV_PIX_FMT_BGR565BE] = {
336 .color_type = FF_COLOR_RGB,
339 [AV_PIX_FMT_BGR565LE] = {
340 .color_type = FF_COLOR_RGB,
343 [AV_PIX_FMT_BGR555BE] = {
344 .color_type = FF_COLOR_RGB,
347 [AV_PIX_FMT_BGR555LE] = {
348 .color_type = FF_COLOR_RGB,
351 [AV_PIX_FMT_BGR444BE] = {
352 .color_type = FF_COLOR_RGB,
355 [AV_PIX_FMT_BGR444LE] = {
356 .color_type = FF_COLOR_RGB,
359 [AV_PIX_FMT_RGB8] = {
360 .color_type = FF_COLOR_RGB,
362 [AV_PIX_FMT_RGB4] = {
363 .color_type = FF_COLOR_RGB,
365 [AV_PIX_FMT_RGB4_BYTE] = {
366 .color_type = FF_COLOR_RGB,
369 [AV_PIX_FMT_BGR8] = {
370 .color_type = FF_COLOR_RGB,
372 [AV_PIX_FMT_BGR4] = {
373 .color_type = FF_COLOR_RGB,
375 [AV_PIX_FMT_BGR4_BYTE] = {
376 .color_type = FF_COLOR_RGB,
379 [AV_PIX_FMT_NV12] = {
380 .color_type = FF_COLOR_YUV,
382 [AV_PIX_FMT_NV21] = {
383 .color_type = FF_COLOR_YUV,
386 [AV_PIX_FMT_BGRA] = {
387 .color_type = FF_COLOR_RGB,
389 [AV_PIX_FMT_RGBA] = {
390 .color_type = FF_COLOR_RGB,
393 [AV_PIX_FMT_GBRP] = {
394 .color_type = FF_COLOR_RGB,
396 [AV_PIX_FMT_GBRP9BE] = {
397 .color_type = FF_COLOR_RGB,
399 [AV_PIX_FMT_GBRP9LE] = {
400 .color_type = FF_COLOR_RGB,
402 [AV_PIX_FMT_GBRP10BE] = {
403 .color_type = FF_COLOR_RGB,
405 [AV_PIX_FMT_GBRP10LE] = {
406 .color_type = FF_COLOR_RGB,
408 [AV_PIX_FMT_GBRP12BE] = {
409 .color_type = FF_COLOR_RGB,
411 [AV_PIX_FMT_GBRP12LE] = {
412 .color_type = FF_COLOR_RGB,
414 [AV_PIX_FMT_GBRP14BE] = {
415 .color_type = FF_COLOR_RGB,
417 [AV_PIX_FMT_GBRP14LE] = {
418 .color_type = FF_COLOR_RGB,
420 [AV_PIX_FMT_GBRP16BE] = {
421 .color_type = FF_COLOR_RGB,
423 [AV_PIX_FMT_GBRP16LE] = {
424 .color_type = FF_COLOR_RGB,
428 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
430 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
432 *h_shift = desc->log2_chroma_w;
433 *v_shift = desc->log2_chroma_h;
436 int avpicture_fill(AVPicture *picture, const uint8_t *ptr,
437 enum AVPixelFormat pix_fmt, int width, int height)
439 return av_image_fill_arrays(picture->data, picture->linesize,
440 ptr, pix_fmt, width, height, 1);
443 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
444 unsigned char *dest, int dest_size)
446 return av_image_copy_to_buffer(dest, dest_size,
447 (const uint8_t * const*)src->data, src->linesize,
448 pix_fmt, width, height, 1);
451 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
453 return av_image_get_buffer_size(pix_fmt, width, height, 1);
456 static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
458 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
461 if (!desc || !desc->nb_components) {
463 return AVERROR(EINVAL);
466 *min = INT_MAX, *max = -INT_MAX;
467 for (i = 0; i < desc->nb_components; i++) {
468 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
469 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
474 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
477 const PixFmtInfo *pf, *ps;
478 const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
479 const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
480 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
483 if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
486 ps = &pix_fmt_info[src_pix_fmt];
491 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
493 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
495 if (dst_min_depth < src_min_depth ||
496 dst_max_depth < src_max_depth)
497 loss |= FF_LOSS_DEPTH;
498 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
499 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
500 loss |= FF_LOSS_RESOLUTION;
502 pf = &pix_fmt_info[dst_pix_fmt];
503 switch(pf->color_type) {
505 if (ps->color_type != FF_COLOR_RGB &&
506 ps->color_type != FF_COLOR_GRAY)
507 loss |= FF_LOSS_COLORSPACE;
510 if (ps->color_type != FF_COLOR_GRAY)
511 loss |= FF_LOSS_COLORSPACE;
514 if (ps->color_type != FF_COLOR_YUV)
515 loss |= FF_LOSS_COLORSPACE;
517 case FF_COLOR_YUV_JPEG:
518 if (ps->color_type != FF_COLOR_YUV_JPEG &&
519 ps->color_type != FF_COLOR_YUV &&
520 ps->color_type != FF_COLOR_GRAY)
521 loss |= FF_LOSS_COLORSPACE;
525 if (ps->color_type != pf->color_type)
526 loss |= FF_LOSS_COLORSPACE;
529 if (pf->color_type == FF_COLOR_GRAY &&
530 ps->color_type != FF_COLOR_GRAY)
531 loss |= FF_LOSS_CHROMA;
532 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
533 loss |= FF_LOSS_ALPHA;
534 if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
535 (src_pix_fmt != AV_PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
536 loss |= FF_LOSS_COLORQUANT;
541 static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
543 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
544 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
546 return info->padded_size ?
547 info->padded_size : av_get_bits_per_pixel(desc);
550 #if FF_API_FIND_BEST_PIX_FMT
551 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
552 int has_alpha, int *loss_ptr)
554 enum AVPixelFormat dst_pix_fmt;
557 if (loss_ptr) /* all losses count (for backward compatibility) */
560 dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
561 for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
562 if (pix_fmt_mask & (1ULL << i))
563 dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
567 #endif /* FF_API_FIND_BEST_PIX_FMT */
569 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
570 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
572 enum AVPixelFormat dst_pix_fmt;
573 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
574 static const int loss_mask_order[] = {
575 ~0, /* no loss first */
579 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
582 ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
583 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
584 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
585 0x80000, //non zero entry that combines all loss variants including future additions
589 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
590 dst_pix_fmt = AV_PIX_FMT_NONE;
591 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
592 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
594 /* try with successive loss */
595 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
596 loss_order1 = loss1 & loss_mask_order[i];
597 loss_order2 = loss2 & loss_mask_order[i];
599 if (loss_order1 == 0 && loss_order2 == 0 && dst_pix_fmt2 != AV_PIX_FMT_NONE && dst_pix_fmt1 != AV_PIX_FMT_NONE){ /* use format with smallest depth */
600 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
601 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
602 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
607 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
611 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
612 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
613 enum AVPixelFormat src_pix_fmt,
614 int has_alpha, int *loss_ptr){
615 return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
618 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
619 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
621 return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
625 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
626 enum AVPixelFormat src_pix_fmt,
627 int has_alpha, int *loss_ptr){
630 enum AVPixelFormat best = AV_PIX_FMT_NONE;
632 for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
633 best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
638 void av_picture_copy(AVPicture *dst, const AVPicture *src,
639 enum AVPixelFormat pix_fmt, int width, int height)
641 av_image_copy(dst->data, dst->linesize, (const uint8_t **)src->data,
642 src->linesize, pix_fmt, width, height);
646 void ff_shrink22(uint8_t *dst, int dst_wrap,
647 const uint8_t *src, int src_wrap,
648 int width, int height)
651 const uint8_t *s1, *s2;
654 for(;height > 0; height--) {
658 for(w = width;w >= 4; w-=4) {
659 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
660 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
661 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
662 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
668 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
679 void ff_shrink44(uint8_t *dst, int dst_wrap,
680 const uint8_t *src, int src_wrap,
681 int width, int height)
684 const uint8_t *s1, *s2, *s3, *s4;
687 for(;height > 0; height--) {
693 for(w = width;w > 0; w--) {
694 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
695 s2[0] + s2[1] + s2[2] + s2[3] +
696 s3[0] + s3[1] + s3[2] + s3[3] +
697 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
710 void ff_shrink88(uint8_t *dst, int dst_wrap,
711 const uint8_t *src, int src_wrap,
712 int width, int height)
716 for(;height > 0; height--) {
717 for(w = width;w > 0; w--) {
720 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
723 *(dst++) = (tmp + 32)>>6;
724 src += 8 - 8*src_wrap;
726 src += 8*src_wrap - 8*width;
727 dst += dst_wrap - width;
732 int avpicture_alloc(AVPicture *picture,
733 enum AVPixelFormat pix_fmt, int width, int height)
737 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
738 memset(picture, 0, sizeof(AVPicture));
745 void avpicture_free(AVPicture *picture)
747 av_free(picture->data[0]);
750 /* return true if yuv planar */
751 static inline int is_yuv_planar(enum AVPixelFormat fmt)
753 const PixFmtInfo *info = &pix_fmt_info[fmt];
754 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
756 int planes[4] = { 0 };
758 if (info->color_type != FF_COLOR_YUV &&
759 info->color_type != FF_COLOR_YUV_JPEG)
762 /* set the used planes */
763 for (i = 0; i < desc->nb_components; i++)
764 planes[desc->comp[i].plane] = 1;
766 /* if there is an unused plane, the format is not planar */
767 for (i = 0; i < desc->nb_components; i++)
773 int av_picture_crop(AVPicture *dst, const AVPicture *src,
774 enum AVPixelFormat pix_fmt, int top_band, int left_band)
776 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
780 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
783 y_shift = desc->log2_chroma_h;
784 x_shift = desc->log2_chroma_w;
786 if (is_yuv_planar(pix_fmt)) {
787 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
788 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
789 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
791 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
793 if(left_band) //FIXME add support for this too
795 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
798 dst->linesize[0] = src->linesize[0];
799 dst->linesize[1] = src->linesize[1];
800 dst->linesize[2] = src->linesize[2];
804 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
805 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
808 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
815 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
816 !is_yuv_planar(pix_fmt)) return -1;
818 for (i = 0; i < 3; i++) {
819 x_shift = i ? desc->log2_chroma_w : 0;
820 y_shift = i ? desc->log2_chroma_h : 0;
822 if (padtop || padleft) {
823 memset(dst->data[i], color[i],
824 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
827 if (padleft || padright) {
828 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
829 (dst->linesize[i] - (padright >> x_shift));
830 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
831 for (y = 0; y < yheight; y++) {
832 memset(optr, color[i], (padleft + padright) >> x_shift);
833 optr += dst->linesize[i];
837 if (src) { /* first line */
838 uint8_t *iptr = src->data[i];
839 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
840 (padleft >> x_shift);
841 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
842 iptr += src->linesize[i];
843 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
844 (dst->linesize[i] - (padright >> x_shift));
845 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
846 for (y = 0; y < yheight; y++) {
847 memset(optr, color[i], (padleft + padright) >> x_shift);
848 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
849 (width - padleft - padright) >> x_shift);
850 iptr += src->linesize[i];
851 optr += dst->linesize[i];
855 if (padbottom || padright) {
856 optr = dst->data[i] + dst->linesize[i] *
857 ((height - padbottom) >> y_shift) - (padright >> x_shift);
858 memset(optr, color[i],dst->linesize[i] *
859 (padbottom >> y_shift) + (padright >> x_shift));
865 #if !HAVE_MMX_EXTERNAL
866 /* filter parameters: [-1 4 2 4 -1] // 8 */
867 static void deinterlace_line_c(uint8_t *dst,
868 const uint8_t *lum_m4, const uint8_t *lum_m3,
869 const uint8_t *lum_m2, const uint8_t *lum_m1,
873 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
876 for(;size > 0;size--) {
878 sum += lum_m3[0] << 2;
879 sum += lum_m2[0] << 1;
880 sum += lum_m1[0] << 2;
882 dst[0] = cm[(sum + 4) >> 3];
892 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
893 uint8_t *lum_m2, uint8_t *lum_m1,
894 uint8_t *lum, int size)
896 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
899 for(;size > 0;size--) {
901 sum += lum_m3[0] << 2;
902 sum += lum_m2[0] << 1;
904 sum += lum_m1[0] << 2;
906 lum_m2[0] = cm[(sum + 4) >> 3];
914 #endif /* !HAVE_MMX_EXTERNAL */
916 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
917 top field is copied as is, but the bottom field is deinterlaced
918 against the top field. */
919 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
920 const uint8_t *src1, int src_wrap,
921 int width, int height)
923 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
928 src_0=&src_m1[src_wrap];
929 src_p1=&src_0[src_wrap];
930 src_p2=&src_p1[src_wrap];
931 for(y=0;y<(height-2);y+=2) {
932 memcpy(dst,src_m1,width);
934 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
938 src_p1 += 2*src_wrap;
939 src_p2 += 2*src_wrap;
942 memcpy(dst,src_m1,width);
945 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
948 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
949 int width, int height)
951 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
954 buf = av_malloc(width);
957 memcpy(buf,src_m1,width);
958 src_0=&src_m1[src_wrap];
959 src_p1=&src_0[src_wrap];
960 src_p2=&src_p1[src_wrap];
961 for(y=0;y<(height-2);y+=2) {
962 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
965 src_p1 += 2*src_wrap;
966 src_p2 += 2*src_wrap;
969 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
973 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
974 enum AVPixelFormat pix_fmt, int width, int height)
978 if (pix_fmt != AV_PIX_FMT_YUV420P &&
979 pix_fmt != AV_PIX_FMT_YUVJ420P &&
980 pix_fmt != AV_PIX_FMT_YUV422P &&
981 pix_fmt != AV_PIX_FMT_YUVJ422P &&
982 pix_fmt != AV_PIX_FMT_YUV444P &&
983 pix_fmt != AV_PIX_FMT_YUV411P &&
984 pix_fmt != AV_PIX_FMT_GRAY8)
986 if ((width & 3) != 0 || (height & 3) != 0)
992 case AV_PIX_FMT_YUVJ420P:
993 case AV_PIX_FMT_YUV420P:
997 case AV_PIX_FMT_YUV422P:
998 case AV_PIX_FMT_YUVJ422P:
1001 case AV_PIX_FMT_YUV411P:
1007 if (pix_fmt == AV_PIX_FMT_GRAY8) {
1012 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1015 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1016 src->data[i], src->linesize[i],