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 "imgconvert.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[PIX_FMT_NB] = {
71 .color_type = FF_COLOR_YUV,
74 .color_type = FF_COLOR_YUV,
77 .color_type = FF_COLOR_YUV,
80 .color_type = FF_COLOR_YUV,
83 .color_type = FF_COLOR_YUV,
86 .color_type = FF_COLOR_YUV,
89 .color_type = FF_COLOR_YUV,
92 .color_type = FF_COLOR_YUV,
94 [PIX_FMT_YUV420P9LE] = {
95 .color_type = FF_COLOR_YUV,
97 [PIX_FMT_YUV422P9LE] = {
98 .color_type = FF_COLOR_YUV,
100 [PIX_FMT_YUV444P9LE] = {
101 .color_type = FF_COLOR_YUV,
103 [PIX_FMT_YUV420P9BE] = {
104 .color_type = FF_COLOR_YUV,
106 [PIX_FMT_YUV422P9BE] = {
107 .color_type = FF_COLOR_YUV,
109 [PIX_FMT_YUV444P9BE] = {
110 .color_type = FF_COLOR_YUV,
112 [PIX_FMT_YUV420P10LE] = {
113 .color_type = FF_COLOR_YUV,
115 [PIX_FMT_YUV422P10LE] = {
116 .color_type = FF_COLOR_YUV,
118 [PIX_FMT_YUV444P10LE] = {
119 .color_type = FF_COLOR_YUV,
121 [PIX_FMT_YUV420P10BE] = {
122 .color_type = FF_COLOR_YUV,
124 [PIX_FMT_YUV422P10BE] = {
125 .color_type = FF_COLOR_YUV,
127 [PIX_FMT_YUV444P10BE] = {
128 .color_type = FF_COLOR_YUV,
130 [PIX_FMT_YUV420P12LE] = {
131 .color_type = FF_COLOR_YUV,
133 [PIX_FMT_YUV422P12LE] = {
134 .color_type = FF_COLOR_YUV,
136 [PIX_FMT_YUV444P12LE] = {
137 .color_type = FF_COLOR_YUV,
139 [PIX_FMT_YUV420P12BE] = {
140 .color_type = FF_COLOR_YUV,
142 [PIX_FMT_YUV422P12BE] = {
143 .color_type = FF_COLOR_YUV,
145 [PIX_FMT_YUV444P12BE] = {
146 .color_type = FF_COLOR_YUV,
148 [PIX_FMT_YUV420P14LE] = {
149 .color_type = FF_COLOR_YUV,
151 [PIX_FMT_YUV422P14LE] = {
152 .color_type = FF_COLOR_YUV,
154 [PIX_FMT_YUV444P14LE] = {
155 .color_type = FF_COLOR_YUV,
157 [PIX_FMT_YUV420P14BE] = {
158 .color_type = FF_COLOR_YUV,
160 [PIX_FMT_YUV422P14BE] = {
161 .color_type = FF_COLOR_YUV,
163 [PIX_FMT_YUV444P14BE] = {
164 .color_type = FF_COLOR_YUV,
166 [PIX_FMT_YUV420P16LE] = {
167 .color_type = FF_COLOR_YUV,
169 [PIX_FMT_YUV422P16LE] = {
170 .color_type = FF_COLOR_YUV,
172 [PIX_FMT_YUV444P16LE] = {
173 .color_type = FF_COLOR_YUV,
175 [PIX_FMT_YUV420P16BE] = {
176 .color_type = FF_COLOR_YUV,
178 [PIX_FMT_YUV422P16BE] = {
179 .color_type = FF_COLOR_YUV,
181 [PIX_FMT_YUV444P16BE] = {
182 .color_type = FF_COLOR_YUV,
185 /* YUV formats with alpha plane */
186 [PIX_FMT_YUVA420P] = {
187 .color_type = FF_COLOR_YUV,
190 [PIX_FMT_YUVA422P] = {
191 .color_type = FF_COLOR_YUV,
194 [PIX_FMT_YUVA444P] = {
195 .color_type = FF_COLOR_YUV,
199 [PIX_FMT_YUVJ420P] = {
200 .color_type = FF_COLOR_YUV_JPEG,
202 [PIX_FMT_YUVJ422P] = {
203 .color_type = FF_COLOR_YUV_JPEG,
205 [PIX_FMT_YUVJ444P] = {
206 .color_type = FF_COLOR_YUV_JPEG,
208 [PIX_FMT_YUVJ440P] = {
209 .color_type = FF_COLOR_YUV_JPEG,
214 .color_type = FF_COLOR_RGB,
217 .color_type = FF_COLOR_RGB,
220 .color_type = FF_COLOR_RGB,
222 [PIX_FMT_RGB48BE] = {
223 .color_type = FF_COLOR_RGB,
225 [PIX_FMT_RGB48LE] = {
226 .color_type = FF_COLOR_RGB,
228 [PIX_FMT_RGBA64BE] = {
229 .color_type = FF_COLOR_RGB,
231 [PIX_FMT_RGBA64LE] = {
232 .color_type = FF_COLOR_RGB,
234 [PIX_FMT_RGB565BE] = {
235 .color_type = FF_COLOR_RGB,
237 [PIX_FMT_RGB565LE] = {
238 .color_type = FF_COLOR_RGB,
240 [PIX_FMT_RGB555BE] = {
241 .color_type = FF_COLOR_RGB,
244 [PIX_FMT_RGB555LE] = {
245 .color_type = FF_COLOR_RGB,
248 [PIX_FMT_RGB444BE] = {
249 .color_type = FF_COLOR_RGB,
252 [PIX_FMT_RGB444LE] = {
253 .color_type = FF_COLOR_RGB,
257 /* gray / mono formats */
258 [PIX_FMT_GRAY16BE] = {
259 .color_type = FF_COLOR_GRAY,
261 [PIX_FMT_GRAY16LE] = {
262 .color_type = FF_COLOR_GRAY,
265 .color_type = FF_COLOR_GRAY,
268 .color_type = FF_COLOR_GRAY,
270 [PIX_FMT_MONOWHITE] = {
271 .color_type = FF_COLOR_GRAY,
273 [PIX_FMT_MONOBLACK] = {
274 .color_type = FF_COLOR_GRAY,
277 /* paletted formats */
279 .color_type = FF_COLOR_RGB,
281 [PIX_FMT_UYYVYY411] = {
282 .color_type = FF_COLOR_YUV,
285 .color_type = FF_COLOR_RGB,
287 [PIX_FMT_BGR48BE] = {
288 .color_type = FF_COLOR_RGB,
290 [PIX_FMT_BGR48LE] = {
291 .color_type = FF_COLOR_RGB,
293 [PIX_FMT_BGRA64BE] = {
294 .color_type = FF_COLOR_RGB,
296 [PIX_FMT_BGRA64LE] = {
297 .color_type = FF_COLOR_RGB,
299 [PIX_FMT_BGR565BE] = {
300 .color_type = FF_COLOR_RGB,
303 [PIX_FMT_BGR565LE] = {
304 .color_type = FF_COLOR_RGB,
307 [PIX_FMT_BGR555BE] = {
308 .color_type = FF_COLOR_RGB,
311 [PIX_FMT_BGR555LE] = {
312 .color_type = FF_COLOR_RGB,
315 [PIX_FMT_BGR444BE] = {
316 .color_type = FF_COLOR_RGB,
319 [PIX_FMT_BGR444LE] = {
320 .color_type = FF_COLOR_RGB,
324 .color_type = FF_COLOR_RGB,
327 .color_type = FF_COLOR_RGB,
329 [PIX_FMT_RGB4_BYTE] = {
330 .color_type = FF_COLOR_RGB,
334 .color_type = FF_COLOR_RGB,
337 .color_type = FF_COLOR_RGB,
339 [PIX_FMT_BGR4_BYTE] = {
340 .color_type = FF_COLOR_RGB,
344 .color_type = FF_COLOR_YUV,
347 .color_type = FF_COLOR_YUV,
351 .color_type = FF_COLOR_RGB,
354 .color_type = FF_COLOR_RGB,
358 .color_type = FF_COLOR_RGB,
360 [PIX_FMT_GBRP9BE] = {
361 .color_type = FF_COLOR_RGB,
363 [PIX_FMT_GBRP9LE] = {
364 .color_type = FF_COLOR_RGB,
366 [PIX_FMT_GBRP10BE] = {
367 .color_type = FF_COLOR_RGB,
369 [PIX_FMT_GBRP10LE] = {
370 .color_type = FF_COLOR_RGB,
372 [PIX_FMT_GBRP12BE] = {
373 .color_type = FF_COLOR_RGB,
375 [PIX_FMT_GBRP12LE] = {
376 .color_type = FF_COLOR_RGB,
378 [PIX_FMT_GBRP14BE] = {
379 .color_type = FF_COLOR_RGB,
381 [PIX_FMT_GBRP14LE] = {
382 .color_type = FF_COLOR_RGB,
384 [PIX_FMT_GBRP16BE] = {
385 .color_type = FF_COLOR_RGB,
387 [PIX_FMT_GBRP16LE] = {
388 .color_type = FF_COLOR_RGB,
392 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
394 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
395 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
398 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
400 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
403 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
404 enum PixelFormat pix_fmt, int width, int height)
406 return av_image_fill_arrays(picture->data, picture->linesize,
407 ptr, pix_fmt, width, height, 1);
410 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
411 unsigned char *dest, int dest_size)
413 return av_image_copy_to_buffer(dest, dest_size,
414 src->data, src->linesize,
415 pix_fmt, width, height, 1);
418 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
420 return av_image_get_buffer_size(pix_fmt, width, height, 1);
423 static int get_pix_fmt_depth(int *min, int *max, enum PixelFormat pix_fmt)
425 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
428 if (!desc->nb_components) {
430 return AVERROR(EINVAL);
433 *min = INT_MAX, *max = -INT_MAX;
434 for (i = 0; i < desc->nb_components; i++) {
435 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
436 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
441 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
444 const PixFmtInfo *pf, *ps;
445 const AVPixFmtDescriptor *src_desc;
446 const AVPixFmtDescriptor *dst_desc;
447 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
450 if (dst_pix_fmt >= PIX_FMT_NB || dst_pix_fmt <= PIX_FMT_NONE)
453 src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
454 dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
455 ps = &pix_fmt_info[src_pix_fmt];
460 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
462 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
464 if (dst_min_depth < src_min_depth ||
465 dst_max_depth < src_max_depth)
466 loss |= FF_LOSS_DEPTH;
467 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
468 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
469 loss |= FF_LOSS_RESOLUTION;
471 pf = &pix_fmt_info[dst_pix_fmt];
472 switch(pf->color_type) {
474 if (ps->color_type != FF_COLOR_RGB &&
475 ps->color_type != FF_COLOR_GRAY)
476 loss |= FF_LOSS_COLORSPACE;
479 if (ps->color_type != FF_COLOR_GRAY)
480 loss |= FF_LOSS_COLORSPACE;
483 if (ps->color_type != FF_COLOR_YUV)
484 loss |= FF_LOSS_COLORSPACE;
486 case FF_COLOR_YUV_JPEG:
487 if (ps->color_type != FF_COLOR_YUV_JPEG &&
488 ps->color_type != FF_COLOR_YUV &&
489 ps->color_type != FF_COLOR_GRAY)
490 loss |= FF_LOSS_COLORSPACE;
494 if (ps->color_type != pf->color_type)
495 loss |= FF_LOSS_COLORSPACE;
498 if (pf->color_type == FF_COLOR_GRAY &&
499 ps->color_type != FF_COLOR_GRAY)
500 loss |= FF_LOSS_CHROMA;
501 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
502 loss |= FF_LOSS_ALPHA;
503 if (dst_pix_fmt == PIX_FMT_PAL8 &&
504 (src_pix_fmt != PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
505 loss |= FF_LOSS_COLORQUANT;
510 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
512 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
513 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
515 return info->padded_size ?
516 info->padded_size : av_get_bits_per_pixel(desc);
519 #if FF_API_FIND_BEST_PIX_FMT
520 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
521 int has_alpha, int *loss_ptr)
523 enum PixelFormat dst_pix_fmt;
526 if (loss_ptr) /* all losses count (for backward compatibility) */
529 dst_pix_fmt = PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
530 for(i = 0; i< FFMIN(PIX_FMT_NB, 64); i++){
531 if (pix_fmt_mask & (1ULL << i))
532 dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
536 #endif /* FF_API_FIND_BEST_PIX_FMT */
538 enum PixelFormat avcodec_find_best_pix_fmt_of_2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
539 enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
541 enum PixelFormat dst_pix_fmt;
542 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
543 static const int loss_mask_order[] = {
544 ~0, /* no loss first */
548 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
551 ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
552 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
553 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
554 0x80000, //non zero entry that combines all loss variants including future additions
558 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
559 dst_pix_fmt = PIX_FMT_NONE;
560 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
561 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
563 /* try with successive loss */
564 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == PIX_FMT_NONE;i++) {
565 loss_order1 = loss1 & loss_mask_order[i];
566 loss_order2 = loss2 & loss_mask_order[i];
568 if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
569 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
570 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
571 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
576 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
580 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
581 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat *pix_fmt_list,
582 enum PixelFormat src_pix_fmt,
583 int has_alpha, int *loss_ptr){
584 return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
587 enum PixelFormat avcodec_find_best_pix_fmt2(enum PixelFormat dst_pix_fmt1, enum PixelFormat dst_pix_fmt2,
588 enum PixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
590 return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
594 enum PixelFormat avcodec_find_best_pix_fmt_of_list(enum PixelFormat *pix_fmt_list,
595 enum PixelFormat src_pix_fmt,
596 int has_alpha, int *loss_ptr){
599 enum PixelFormat best = PIX_FMT_NONE;
601 for(i=0; pix_fmt_list[i] != PIX_FMT_NONE; i++)
602 best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
607 void av_picture_copy(AVPicture *dst, const AVPicture *src,
608 enum PixelFormat pix_fmt, int width, int height)
610 av_image_copy(dst->data, dst->linesize, src->data,
611 src->linesize, pix_fmt, width, height);
615 void ff_shrink22(uint8_t *dst, int dst_wrap,
616 const uint8_t *src, int src_wrap,
617 int width, int height)
620 const uint8_t *s1, *s2;
623 for(;height > 0; height--) {
627 for(w = width;w >= 4; w-=4) {
628 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
629 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
630 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
631 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
637 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
648 void ff_shrink44(uint8_t *dst, int dst_wrap,
649 const uint8_t *src, int src_wrap,
650 int width, int height)
653 const uint8_t *s1, *s2, *s3, *s4;
656 for(;height > 0; height--) {
662 for(w = width;w > 0; w--) {
663 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
664 s2[0] + s2[1] + s2[2] + s2[3] +
665 s3[0] + s3[1] + s3[2] + s3[3] +
666 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
679 void ff_shrink88(uint8_t *dst, int dst_wrap,
680 const uint8_t *src, int src_wrap,
681 int width, int height)
685 for(;height > 0; height--) {
686 for(w = width;w > 0; w--) {
689 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
692 *(dst++) = (tmp + 32)>>6;
693 src += 8 - 8*src_wrap;
695 src += 8*src_wrap - 8*width;
696 dst += dst_wrap - width;
701 int avpicture_alloc(AVPicture *picture,
702 enum PixelFormat pix_fmt, int width, int height)
706 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
707 memset(picture, 0, sizeof(AVPicture));
714 void avpicture_free(AVPicture *picture)
716 av_free(picture->data[0]);
719 /* return true if yuv planar */
720 static inline int is_yuv_planar(enum PixelFormat fmt)
722 const PixFmtInfo *info = &pix_fmt_info[fmt];
723 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
725 int planes[4] = { 0 };
727 if (info->color_type != FF_COLOR_YUV &&
728 info->color_type != FF_COLOR_YUV_JPEG)
731 /* set the used planes */
732 for (i = 0; i < desc->nb_components; i++)
733 planes[desc->comp[i].plane] = 1;
735 /* if there is an unused plane, the format is not planar */
736 for (i = 0; i < desc->nb_components; i++)
742 int av_picture_crop(AVPicture *dst, const AVPicture *src,
743 enum PixelFormat pix_fmt, int top_band, int left_band)
748 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
751 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
752 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
754 if (is_yuv_planar(pix_fmt)) {
755 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
756 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
757 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
759 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
761 if(left_band) //FIXME add support for this too
763 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
766 dst->linesize[0] = src->linesize[0];
767 dst->linesize[1] = src->linesize[1];
768 dst->linesize[2] = src->linesize[2];
772 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
773 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
782 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
783 !is_yuv_planar(pix_fmt)) return -1;
785 for (i = 0; i < 3; i++) {
786 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
787 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
789 if (padtop || padleft) {
790 memset(dst->data[i], color[i],
791 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
794 if (padleft || padright) {
795 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
796 (dst->linesize[i] - (padright >> x_shift));
797 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
798 for (y = 0; y < yheight; y++) {
799 memset(optr, color[i], (padleft + padright) >> x_shift);
800 optr += dst->linesize[i];
804 if (src) { /* first line */
805 uint8_t *iptr = src->data[i];
806 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
807 (padleft >> x_shift);
808 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
809 iptr += src->linesize[i];
810 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
811 (dst->linesize[i] - (padright >> x_shift));
812 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
813 for (y = 0; y < yheight; y++) {
814 memset(optr, color[i], (padleft + padright) >> x_shift);
815 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
816 (width - padleft - padright) >> x_shift);
817 iptr += src->linesize[i];
818 optr += dst->linesize[i];
822 if (padbottom || padright) {
823 optr = dst->data[i] + dst->linesize[i] *
824 ((height - padbottom) >> y_shift) - (padright >> x_shift);
825 memset(optr, color[i],dst->linesize[i] *
826 (padbottom >> y_shift) + (padright >> x_shift));
832 #if !HAVE_MMX_EXTERNAL
833 /* filter parameters: [-1 4 2 4 -1] // 8 */
834 static void deinterlace_line_c(uint8_t *dst,
835 const uint8_t *lum_m4, const uint8_t *lum_m3,
836 const uint8_t *lum_m2, const uint8_t *lum_m1,
840 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
843 for(;size > 0;size--) {
845 sum += lum_m3[0] << 2;
846 sum += lum_m2[0] << 1;
847 sum += lum_m1[0] << 2;
849 dst[0] = cm[(sum + 4) >> 3];
859 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
860 uint8_t *lum_m2, uint8_t *lum_m1,
861 uint8_t *lum, int size)
863 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
866 for(;size > 0;size--) {
868 sum += lum_m3[0] << 2;
869 sum += lum_m2[0] << 1;
871 sum += lum_m1[0] << 2;
873 lum_m2[0] = cm[(sum + 4) >> 3];
881 #endif /* !HAVE_MMX_EXTERNAL */
883 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
884 top field is copied as is, but the bottom field is deinterlaced
885 against the top field. */
886 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
887 const uint8_t *src1, int src_wrap,
888 int width, int height)
890 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
895 src_0=&src_m1[src_wrap];
896 src_p1=&src_0[src_wrap];
897 src_p2=&src_p1[src_wrap];
898 for(y=0;y<(height-2);y+=2) {
899 memcpy(dst,src_m1,width);
901 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
905 src_p1 += 2*src_wrap;
906 src_p2 += 2*src_wrap;
909 memcpy(dst,src_m1,width);
912 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
915 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
916 int width, int height)
918 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
921 buf = av_malloc(width);
924 memcpy(buf,src_m1,width);
925 src_0=&src_m1[src_wrap];
926 src_p1=&src_0[src_wrap];
927 src_p2=&src_p1[src_wrap];
928 for(y=0;y<(height-2);y+=2) {
929 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
932 src_p1 += 2*src_wrap;
933 src_p2 += 2*src_wrap;
936 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
940 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
941 enum PixelFormat pix_fmt, int width, int height)
945 if (pix_fmt != PIX_FMT_YUV420P &&
946 pix_fmt != PIX_FMT_YUVJ420P &&
947 pix_fmt != PIX_FMT_YUV422P &&
948 pix_fmt != PIX_FMT_YUVJ422P &&
949 pix_fmt != PIX_FMT_YUV444P &&
950 pix_fmt != PIX_FMT_YUV411P &&
951 pix_fmt != PIX_FMT_GRAY8)
953 if ((width & 3) != 0 || (height & 3) != 0)
959 case PIX_FMT_YUVJ420P:
960 case PIX_FMT_YUV420P:
964 case PIX_FMT_YUV422P:
965 case PIX_FMT_YUVJ422P:
968 case PIX_FMT_YUV411P:
974 if (pix_fmt == PIX_FMT_GRAY8) {
979 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
982 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
983 src->data[i], src->linesize[i],