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/colorspace.h"
37 #include "libavutil/common.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavutil/imgutils.h"
42 #include "x86/dsputil_mmx.h"
45 #define FF_COLOR_RGB 0 /**< RGB color space */
46 #define FF_COLOR_GRAY 1 /**< gray color space */
47 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
48 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
51 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
52 #define deinterlace_line ff_deinterlace_line_mmx
54 #define deinterlace_line_inplace deinterlace_line_inplace_c
55 #define deinterlace_line deinterlace_line_c
58 #define pixdesc_has_alpha(pixdesc) \
59 ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
61 typedef struct PixFmtInfo {
62 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
63 uint8_t padded_size; /**< padded size in bits if different from the non-padded size */
66 /* this table gives more information about formats */
67 static const PixFmtInfo pix_fmt_info[AV_PIX_FMT_NB] = {
69 [AV_PIX_FMT_YUV420P] = {
70 .color_type = FF_COLOR_YUV,
72 [AV_PIX_FMT_YUV422P] = {
73 .color_type = FF_COLOR_YUV,
75 [AV_PIX_FMT_YUV444P] = {
76 .color_type = FF_COLOR_YUV,
78 [AV_PIX_FMT_YUYV422] = {
79 .color_type = FF_COLOR_YUV,
81 [AV_PIX_FMT_UYVY422] = {
82 .color_type = FF_COLOR_YUV,
84 [AV_PIX_FMT_YUV410P] = {
85 .color_type = FF_COLOR_YUV,
87 [AV_PIX_FMT_YUV411P] = {
88 .color_type = FF_COLOR_YUV,
90 [AV_PIX_FMT_YUV440P] = {
91 .color_type = FF_COLOR_YUV,
93 [AV_PIX_FMT_YUV420P9LE] = {
94 .color_type = FF_COLOR_YUV,
96 [AV_PIX_FMT_YUV422P9LE] = {
97 .color_type = FF_COLOR_YUV,
99 [AV_PIX_FMT_YUV444P9LE] = {
100 .color_type = FF_COLOR_YUV,
102 [AV_PIX_FMT_YUV420P9BE] = {
103 .color_type = FF_COLOR_YUV,
105 [AV_PIX_FMT_YUV422P9BE] = {
106 .color_type = FF_COLOR_YUV,
108 [AV_PIX_FMT_YUV444P9BE] = {
109 .color_type = FF_COLOR_YUV,
111 [AV_PIX_FMT_YUV420P10LE] = {
112 .color_type = FF_COLOR_YUV,
114 [AV_PIX_FMT_YUV422P10LE] = {
115 .color_type = FF_COLOR_YUV,
117 [AV_PIX_FMT_YUV444P10LE] = {
118 .color_type = FF_COLOR_YUV,
120 [AV_PIX_FMT_YUV420P10BE] = {
121 .color_type = FF_COLOR_YUV,
123 [AV_PIX_FMT_YUV422P10BE] = {
124 .color_type = FF_COLOR_YUV,
126 [AV_PIX_FMT_YUV444P10BE] = {
127 .color_type = FF_COLOR_YUV,
129 [AV_PIX_FMT_YUV420P12LE] = {
130 .color_type = FF_COLOR_YUV,
132 [AV_PIX_FMT_YUV422P12LE] = {
133 .color_type = FF_COLOR_YUV,
135 [AV_PIX_FMT_YUV444P12LE] = {
136 .color_type = FF_COLOR_YUV,
138 [AV_PIX_FMT_YUV420P12BE] = {
139 .color_type = FF_COLOR_YUV,
141 [AV_PIX_FMT_YUV422P12BE] = {
142 .color_type = FF_COLOR_YUV,
144 [AV_PIX_FMT_YUV444P12BE] = {
145 .color_type = FF_COLOR_YUV,
147 [AV_PIX_FMT_YUV420P14LE] = {
148 .color_type = FF_COLOR_YUV,
150 [AV_PIX_FMT_YUV422P14LE] = {
151 .color_type = FF_COLOR_YUV,
153 [AV_PIX_FMT_YUV444P14LE] = {
154 .color_type = FF_COLOR_YUV,
156 [AV_PIX_FMT_YUV420P14BE] = {
157 .color_type = FF_COLOR_YUV,
159 [AV_PIX_FMT_YUV422P14BE] = {
160 .color_type = FF_COLOR_YUV,
162 [AV_PIX_FMT_YUV444P14BE] = {
163 .color_type = FF_COLOR_YUV,
165 [AV_PIX_FMT_YUV420P16LE] = {
166 .color_type = FF_COLOR_YUV,
168 [AV_PIX_FMT_YUV422P16LE] = {
169 .color_type = FF_COLOR_YUV,
171 [AV_PIX_FMT_YUV444P16LE] = {
172 .color_type = FF_COLOR_YUV,
174 [AV_PIX_FMT_YUV420P16BE] = {
175 .color_type = FF_COLOR_YUV,
177 [AV_PIX_FMT_YUV422P16BE] = {
178 .color_type = FF_COLOR_YUV,
180 [AV_PIX_FMT_YUV444P16BE] = {
181 .color_type = FF_COLOR_YUV,
184 /* YUV formats with alpha plane */
185 [AV_PIX_FMT_YUVA420P] = {
186 .color_type = FF_COLOR_YUV,
189 [AV_PIX_FMT_YUVA422P] = {
190 .color_type = FF_COLOR_YUV,
193 [AV_PIX_FMT_YUVA444P] = {
194 .color_type = FF_COLOR_YUV,
198 [AV_PIX_FMT_YUVJ420P] = {
199 .color_type = FF_COLOR_YUV_JPEG,
201 [AV_PIX_FMT_YUVJ422P] = {
202 .color_type = FF_COLOR_YUV_JPEG,
204 [AV_PIX_FMT_YUVJ444P] = {
205 .color_type = FF_COLOR_YUV_JPEG,
207 [AV_PIX_FMT_YUVJ440P] = {
208 .color_type = FF_COLOR_YUV_JPEG,
212 [AV_PIX_FMT_RGB24] = {
213 .color_type = FF_COLOR_RGB,
215 [AV_PIX_FMT_BGR24] = {
216 .color_type = FF_COLOR_RGB,
218 [AV_PIX_FMT_ARGB] = {
219 .color_type = FF_COLOR_RGB,
221 [AV_PIX_FMT_RGB48BE] = {
222 .color_type = FF_COLOR_RGB,
224 [AV_PIX_FMT_RGB48LE] = {
225 .color_type = FF_COLOR_RGB,
227 [AV_PIX_FMT_RGBA64BE] = {
228 .color_type = FF_COLOR_RGB,
230 [AV_PIX_FMT_RGBA64LE] = {
231 .color_type = FF_COLOR_RGB,
233 [AV_PIX_FMT_RGB565BE] = {
234 .color_type = FF_COLOR_RGB,
236 [AV_PIX_FMT_RGB565LE] = {
237 .color_type = FF_COLOR_RGB,
239 [AV_PIX_FMT_RGB555BE] = {
240 .color_type = FF_COLOR_RGB,
243 [AV_PIX_FMT_RGB555LE] = {
244 .color_type = FF_COLOR_RGB,
247 [AV_PIX_FMT_RGB444BE] = {
248 .color_type = FF_COLOR_RGB,
251 [AV_PIX_FMT_RGB444LE] = {
252 .color_type = FF_COLOR_RGB,
256 /* gray / mono formats */
257 [AV_PIX_FMT_GRAY16BE] = {
258 .color_type = FF_COLOR_GRAY,
260 [AV_PIX_FMT_GRAY16LE] = {
261 .color_type = FF_COLOR_GRAY,
263 [AV_PIX_FMT_GRAY8] = {
264 .color_type = FF_COLOR_GRAY,
266 [AV_PIX_FMT_GRAY8A] = {
267 .color_type = FF_COLOR_GRAY,
269 [AV_PIX_FMT_MONOWHITE] = {
270 .color_type = FF_COLOR_GRAY,
272 [AV_PIX_FMT_MONOBLACK] = {
273 .color_type = FF_COLOR_GRAY,
276 /* paletted formats */
277 [AV_PIX_FMT_PAL8] = {
278 .color_type = FF_COLOR_RGB,
280 [AV_PIX_FMT_UYYVYY411] = {
281 .color_type = FF_COLOR_YUV,
283 [AV_PIX_FMT_ABGR] = {
284 .color_type = FF_COLOR_RGB,
286 [AV_PIX_FMT_BGR48BE] = {
287 .color_type = FF_COLOR_RGB,
289 [AV_PIX_FMT_BGR48LE] = {
290 .color_type = FF_COLOR_RGB,
292 [AV_PIX_FMT_BGRA64BE] = {
293 .color_type = FF_COLOR_RGB,
295 [AV_PIX_FMT_BGRA64LE] = {
296 .color_type = FF_COLOR_RGB,
298 [AV_PIX_FMT_BGR565BE] = {
299 .color_type = FF_COLOR_RGB,
302 [AV_PIX_FMT_BGR565LE] = {
303 .color_type = FF_COLOR_RGB,
306 [AV_PIX_FMT_BGR555BE] = {
307 .color_type = FF_COLOR_RGB,
310 [AV_PIX_FMT_BGR555LE] = {
311 .color_type = FF_COLOR_RGB,
314 [AV_PIX_FMT_BGR444BE] = {
315 .color_type = FF_COLOR_RGB,
318 [AV_PIX_FMT_BGR444LE] = {
319 .color_type = FF_COLOR_RGB,
322 [AV_PIX_FMT_RGB8] = {
323 .color_type = FF_COLOR_RGB,
325 [AV_PIX_FMT_RGB4] = {
326 .color_type = FF_COLOR_RGB,
328 [AV_PIX_FMT_RGB4_BYTE] = {
329 .color_type = FF_COLOR_RGB,
332 [AV_PIX_FMT_BGR8] = {
333 .color_type = FF_COLOR_RGB,
335 [AV_PIX_FMT_BGR4] = {
336 .color_type = FF_COLOR_RGB,
338 [AV_PIX_FMT_BGR4_BYTE] = {
339 .color_type = FF_COLOR_RGB,
342 [AV_PIX_FMT_NV12] = {
343 .color_type = FF_COLOR_YUV,
345 [AV_PIX_FMT_NV21] = {
346 .color_type = FF_COLOR_YUV,
349 [AV_PIX_FMT_BGRA] = {
350 .color_type = FF_COLOR_RGB,
352 [AV_PIX_FMT_RGBA] = {
353 .color_type = FF_COLOR_RGB,
356 [AV_PIX_FMT_GBRP] = {
357 .color_type = FF_COLOR_RGB,
359 [AV_PIX_FMT_GBRP9BE] = {
360 .color_type = FF_COLOR_RGB,
362 [AV_PIX_FMT_GBRP9LE] = {
363 .color_type = FF_COLOR_RGB,
365 [AV_PIX_FMT_GBRP10BE] = {
366 .color_type = FF_COLOR_RGB,
368 [AV_PIX_FMT_GBRP10LE] = {
369 .color_type = FF_COLOR_RGB,
371 [AV_PIX_FMT_GBRP12BE] = {
372 .color_type = FF_COLOR_RGB,
374 [AV_PIX_FMT_GBRP12LE] = {
375 .color_type = FF_COLOR_RGB,
377 [AV_PIX_FMT_GBRP14BE] = {
378 .color_type = FF_COLOR_RGB,
380 [AV_PIX_FMT_GBRP14LE] = {
381 .color_type = FF_COLOR_RGB,
383 [AV_PIX_FMT_GBRP16BE] = {
384 .color_type = FF_COLOR_RGB,
386 [AV_PIX_FMT_GBRP16LE] = {
387 .color_type = FF_COLOR_RGB,
391 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
393 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
394 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
397 int ff_is_hwaccel_pix_fmt(enum AVPixelFormat pix_fmt)
399 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
402 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
403 enum AVPixelFormat pix_fmt, int width, int height)
405 return av_image_fill_arrays(picture->data, picture->linesize,
406 ptr, pix_fmt, width, height, 1);
409 int avpicture_layout(const AVPicture* src, enum AVPixelFormat pix_fmt, int width, int height,
410 unsigned char *dest, int dest_size)
412 return av_image_copy_to_buffer(dest, dest_size,
413 (const uint8_t * const*)src->data, src->linesize,
414 pix_fmt, width, height, 1);
417 int avpicture_get_size(enum AVPixelFormat pix_fmt, int width, int height)
419 return av_image_get_buffer_size(pix_fmt, width, height, 1);
422 static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
424 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
427 if (!desc->nb_components) {
429 return AVERROR(EINVAL);
432 *min = INT_MAX, *max = -INT_MAX;
433 for (i = 0; i < desc->nb_components; i++) {
434 *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
435 *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
440 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt, enum AVPixelFormat src_pix_fmt,
443 const PixFmtInfo *pf, *ps;
444 const AVPixFmtDescriptor *src_desc;
445 const AVPixFmtDescriptor *dst_desc;
446 int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
449 if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
452 src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
453 dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
454 ps = &pix_fmt_info[src_pix_fmt];
459 if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
461 if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
463 if (dst_min_depth < src_min_depth ||
464 dst_max_depth < src_max_depth)
465 loss |= FF_LOSS_DEPTH;
466 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
467 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
468 loss |= FF_LOSS_RESOLUTION;
470 pf = &pix_fmt_info[dst_pix_fmt];
471 switch(pf->color_type) {
473 if (ps->color_type != FF_COLOR_RGB &&
474 ps->color_type != FF_COLOR_GRAY)
475 loss |= FF_LOSS_COLORSPACE;
478 if (ps->color_type != FF_COLOR_GRAY)
479 loss |= FF_LOSS_COLORSPACE;
482 if (ps->color_type != FF_COLOR_YUV)
483 loss |= FF_LOSS_COLORSPACE;
485 case FF_COLOR_YUV_JPEG:
486 if (ps->color_type != FF_COLOR_YUV_JPEG &&
487 ps->color_type != FF_COLOR_YUV &&
488 ps->color_type != FF_COLOR_GRAY)
489 loss |= FF_LOSS_COLORSPACE;
493 if (ps->color_type != pf->color_type)
494 loss |= FF_LOSS_COLORSPACE;
497 if (pf->color_type == FF_COLOR_GRAY &&
498 ps->color_type != FF_COLOR_GRAY)
499 loss |= FF_LOSS_CHROMA;
500 if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && has_alpha))
501 loss |= FF_LOSS_ALPHA;
502 if (dst_pix_fmt == AV_PIX_FMT_PAL8 &&
503 (src_pix_fmt != AV_PIX_FMT_PAL8 && (ps->color_type != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && has_alpha))))
504 loss |= FF_LOSS_COLORQUANT;
509 static int avg_bits_per_pixel(enum AVPixelFormat pix_fmt)
511 const PixFmtInfo *info = &pix_fmt_info[pix_fmt];
512 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
514 return info->padded_size ?
515 info->padded_size : av_get_bits_per_pixel(desc);
518 #if FF_API_FIND_BEST_PIX_FMT
519 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
520 int has_alpha, int *loss_ptr)
522 enum AVPixelFormat dst_pix_fmt;
525 if (loss_ptr) /* all losses count (for backward compatibility) */
528 dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
529 for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
530 if (pix_fmt_mask & (1ULL << i))
531 dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
535 #endif /* FF_API_FIND_BEST_PIX_FMT */
537 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
538 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
540 enum AVPixelFormat dst_pix_fmt;
541 int loss1, loss2, loss_order1, loss_order2, i, loss_mask;
542 static const int loss_mask_order[] = {
543 ~0, /* no loss first */
547 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
550 ~(FF_LOSS_DEPTH|FF_LOSS_COLORSPACE),
551 ~(FF_LOSS_RESOLUTION | FF_LOSS_DEPTH | FF_LOSS_COLORSPACE | FF_LOSS_ALPHA |
552 FF_LOSS_COLORQUANT | FF_LOSS_CHROMA),
553 0x80000, //non zero entry that combines all loss variants including future additions
557 loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
558 dst_pix_fmt = AV_PIX_FMT_NONE;
559 loss1 = avcodec_get_pix_fmt_loss(dst_pix_fmt1, src_pix_fmt, has_alpha) & loss_mask;
560 loss2 = avcodec_get_pix_fmt_loss(dst_pix_fmt2, src_pix_fmt, has_alpha) & loss_mask;
562 /* try with successive loss */
563 for(i = 0;loss_mask_order[i] != 0 && dst_pix_fmt == AV_PIX_FMT_NONE;i++) {
564 loss_order1 = loss1 & loss_mask_order[i];
565 loss_order2 = loss2 & loss_mask_order[i];
567 if (loss_order1 == 0 && loss_order2 == 0){ /* use format with smallest depth */
568 dst_pix_fmt = avg_bits_per_pixel(dst_pix_fmt2) < avg_bits_per_pixel(dst_pix_fmt1) ? dst_pix_fmt2 : dst_pix_fmt1;
569 } else if (loss_order1 == 0 || loss_order2 == 0) { /* use format with no loss */
570 dst_pix_fmt = loss_order2 ? dst_pix_fmt1 : dst_pix_fmt2;
575 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
579 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
580 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat *pix_fmt_list,
581 enum AVPixelFormat src_pix_fmt,
582 int has_alpha, int *loss_ptr){
583 return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
586 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
587 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
589 return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
593 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(enum AVPixelFormat *pix_fmt_list,
594 enum AVPixelFormat src_pix_fmt,
595 int has_alpha, int *loss_ptr){
598 enum AVPixelFormat best = AV_PIX_FMT_NONE;
600 for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
601 best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
606 void av_picture_copy(AVPicture *dst, const AVPicture *src,
607 enum AVPixelFormat pix_fmt, int width, int height)
609 av_image_copy(dst->data, dst->linesize, (const uint8_t **)src->data,
610 src->linesize, pix_fmt, width, height);
614 void ff_shrink22(uint8_t *dst, int dst_wrap,
615 const uint8_t *src, int src_wrap,
616 int width, int height)
619 const uint8_t *s1, *s2;
622 for(;height > 0; height--) {
626 for(w = width;w >= 4; w-=4) {
627 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
628 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
629 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
630 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
636 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
647 void ff_shrink44(uint8_t *dst, int dst_wrap,
648 const uint8_t *src, int src_wrap,
649 int width, int height)
652 const uint8_t *s1, *s2, *s3, *s4;
655 for(;height > 0; height--) {
661 for(w = width;w > 0; w--) {
662 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
663 s2[0] + s2[1] + s2[2] + s2[3] +
664 s3[0] + s3[1] + s3[2] + s3[3] +
665 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
678 void ff_shrink88(uint8_t *dst, int dst_wrap,
679 const uint8_t *src, int src_wrap,
680 int width, int height)
684 for(;height > 0; height--) {
685 for(w = width;w > 0; w--) {
688 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
691 *(dst++) = (tmp + 32)>>6;
692 src += 8 - 8*src_wrap;
694 src += 8*src_wrap - 8*width;
695 dst += dst_wrap - width;
700 int avpicture_alloc(AVPicture *picture,
701 enum AVPixelFormat pix_fmt, int width, int height)
705 if ((ret = av_image_alloc(picture->data, picture->linesize, width, height, pix_fmt, 1)) < 0) {
706 memset(picture, 0, sizeof(AVPicture));
713 void avpicture_free(AVPicture *picture)
715 av_free(picture->data[0]);
718 /* return true if yuv planar */
719 static inline int is_yuv_planar(enum AVPixelFormat fmt)
721 const PixFmtInfo *info = &pix_fmt_info[fmt];
722 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[fmt];
724 int planes[4] = { 0 };
726 if (info->color_type != FF_COLOR_YUV &&
727 info->color_type != FF_COLOR_YUV_JPEG)
730 /* set the used planes */
731 for (i = 0; i < desc->nb_components; i++)
732 planes[desc->comp[i].plane] = 1;
734 /* if there is an unused plane, the format is not planar */
735 for (i = 0; i < desc->nb_components; i++)
741 int av_picture_crop(AVPicture *dst, const AVPicture *src,
742 enum AVPixelFormat pix_fmt, int top_band, int left_band)
747 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
750 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
751 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
753 if (is_yuv_planar(pix_fmt)) {
754 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
755 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
756 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
758 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
760 if(left_band) //FIXME add support for this too
762 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
765 dst->linesize[0] = src->linesize[0];
766 dst->linesize[1] = src->linesize[1];
767 dst->linesize[2] = src->linesize[2];
771 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
772 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
781 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
782 !is_yuv_planar(pix_fmt)) return -1;
784 for (i = 0; i < 3; i++) {
785 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
786 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
788 if (padtop || padleft) {
789 memset(dst->data[i], color[i],
790 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
793 if (padleft || padright) {
794 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
795 (dst->linesize[i] - (padright >> x_shift));
796 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
797 for (y = 0; y < yheight; y++) {
798 memset(optr, color[i], (padleft + padright) >> x_shift);
799 optr += dst->linesize[i];
803 if (src) { /* first line */
804 uint8_t *iptr = src->data[i];
805 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
806 (padleft >> x_shift);
807 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
808 iptr += src->linesize[i];
809 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
810 (dst->linesize[i] - (padright >> x_shift));
811 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
812 for (y = 0; y < yheight; y++) {
813 memset(optr, color[i], (padleft + padright) >> x_shift);
814 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
815 (width - padleft - padright) >> x_shift);
816 iptr += src->linesize[i];
817 optr += dst->linesize[i];
821 if (padbottom || padright) {
822 optr = dst->data[i] + dst->linesize[i] *
823 ((height - padbottom) >> y_shift) - (padright >> x_shift);
824 memset(optr, color[i],dst->linesize[i] *
825 (padbottom >> y_shift) + (padright >> x_shift));
831 #if !HAVE_MMX_EXTERNAL
832 /* filter parameters: [-1 4 2 4 -1] // 8 */
833 static void deinterlace_line_c(uint8_t *dst,
834 const uint8_t *lum_m4, const uint8_t *lum_m3,
835 const uint8_t *lum_m2, const uint8_t *lum_m1,
839 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
842 for(;size > 0;size--) {
844 sum += lum_m3[0] << 2;
845 sum += lum_m2[0] << 1;
846 sum += lum_m1[0] << 2;
848 dst[0] = cm[(sum + 4) >> 3];
858 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
859 uint8_t *lum_m2, uint8_t *lum_m1,
860 uint8_t *lum, int size)
862 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
865 for(;size > 0;size--) {
867 sum += lum_m3[0] << 2;
868 sum += lum_m2[0] << 1;
870 sum += lum_m1[0] << 2;
872 lum_m2[0] = cm[(sum + 4) >> 3];
880 #endif /* !HAVE_MMX_EXTERNAL */
882 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
883 top field is copied as is, but the bottom field is deinterlaced
884 against the top field. */
885 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
886 const uint8_t *src1, int src_wrap,
887 int width, int height)
889 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
894 src_0=&src_m1[src_wrap];
895 src_p1=&src_0[src_wrap];
896 src_p2=&src_p1[src_wrap];
897 for(y=0;y<(height-2);y+=2) {
898 memcpy(dst,src_m1,width);
900 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
904 src_p1 += 2*src_wrap;
905 src_p2 += 2*src_wrap;
908 memcpy(dst,src_m1,width);
911 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
914 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
915 int width, int height)
917 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
920 buf = av_malloc(width);
923 memcpy(buf,src_m1,width);
924 src_0=&src_m1[src_wrap];
925 src_p1=&src_0[src_wrap];
926 src_p2=&src_p1[src_wrap];
927 for(y=0;y<(height-2);y+=2) {
928 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
931 src_p1 += 2*src_wrap;
932 src_p2 += 2*src_wrap;
935 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
939 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
940 enum AVPixelFormat pix_fmt, int width, int height)
944 if (pix_fmt != AV_PIX_FMT_YUV420P &&
945 pix_fmt != AV_PIX_FMT_YUVJ420P &&
946 pix_fmt != AV_PIX_FMT_YUV422P &&
947 pix_fmt != AV_PIX_FMT_YUVJ422P &&
948 pix_fmt != AV_PIX_FMT_YUV444P &&
949 pix_fmt != AV_PIX_FMT_YUV411P &&
950 pix_fmt != AV_PIX_FMT_GRAY8)
952 if ((width & 3) != 0 || (height & 3) != 0)
958 case AV_PIX_FMT_YUVJ420P:
959 case AV_PIX_FMT_YUV420P:
963 case AV_PIX_FMT_YUV422P:
964 case AV_PIX_FMT_YUVJ422P:
967 case AV_PIX_FMT_YUV411P:
973 if (pix_fmt == AV_PIX_FMT_GRAY8) {
978 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
981 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
982 src->data[i], src->linesize[i],