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
34 #include "imgconvert.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/colorspace.h"
39 #include "libavutil/common.h"
40 #include "libavutil/pixdesc.h"
41 #include "libavutil/imgutils.h"
44 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
45 #define deinterlace_line ff_deinterlace_line_mmx
47 #define deinterlace_line_inplace deinterlace_line_inplace_c
48 #define deinterlace_line deinterlace_line_c
51 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
53 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
55 *h_shift = desc->log2_chroma_w;
56 *v_shift = desc->log2_chroma_h;
59 int avcodec_get_pix_fmt_loss(enum AVPixelFormat dst_pix_fmt,
60 enum AVPixelFormat src_pix_fmt,
63 return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
66 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
67 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
69 return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
72 #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
73 enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
74 enum AVPixelFormat src_pix_fmt,
75 int has_alpha, int *loss_ptr){
76 return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
79 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
80 enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
82 return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
86 enum AVPixelFormat avcodec_find_best_pix_fmt_of_list(const enum AVPixelFormat *pix_fmt_list,
87 enum AVPixelFormat src_pix_fmt,
88 int has_alpha, int *loss_ptr){
91 enum AVPixelFormat best = AV_PIX_FMT_NONE;
93 for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
94 best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
100 void ff_shrink22(uint8_t *dst, int dst_wrap,
101 const uint8_t *src, int src_wrap,
102 int width, int height)
105 const uint8_t *s1, *s2;
108 for(;height > 0; height--) {
112 for(w = width;w >= 4; w-=4) {
113 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
114 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
115 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
116 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
122 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
133 void ff_shrink44(uint8_t *dst, int dst_wrap,
134 const uint8_t *src, int src_wrap,
135 int width, int height)
138 const uint8_t *s1, *s2, *s3, *s4;
141 for(;height > 0; height--) {
147 for(w = width;w > 0; w--) {
148 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
149 s2[0] + s2[1] + s2[2] + s2[3] +
150 s3[0] + s3[1] + s3[2] + s3[3] +
151 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
164 void ff_shrink88(uint8_t *dst, int dst_wrap,
165 const uint8_t *src, int src_wrap,
166 int width, int height)
170 for(;height > 0; height--) {
171 for(w = width;w > 0; w--) {
174 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
177 *(dst++) = (tmp + 32)>>6;
178 src += 8 - 8*src_wrap;
180 src += 8*src_wrap - 8*width;
181 dst += dst_wrap - width;
185 /* return true if yuv planar */
186 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
189 int planes[4] = { 0 };
191 if ( desc->flags & AV_PIX_FMT_FLAG_RGB
192 || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
195 /* set the used planes */
196 for (i = 0; i < desc->nb_components; i++)
197 planes[desc->comp[i].plane] = 1;
199 /* if there is an unused plane, the format is not planar */
200 for (i = 0; i < desc->nb_components; i++)
206 int av_picture_crop(AVPicture *dst, const AVPicture *src,
207 enum AVPixelFormat pix_fmt, int top_band, int left_band)
209 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
213 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
216 y_shift = desc->log2_chroma_h;
217 x_shift = desc->log2_chroma_w;
219 if (is_yuv_planar(desc)) {
220 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
221 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
222 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
224 if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
226 if(left_band) //FIXME add support for this too
228 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
231 dst->linesize[0] = src->linesize[0];
232 dst->linesize[1] = src->linesize[1];
233 dst->linesize[2] = src->linesize[2];
237 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
238 enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
241 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
248 if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
249 !is_yuv_planar(desc)) return -1;
251 for (i = 0; i < 3; i++) {
252 x_shift = i ? desc->log2_chroma_w : 0;
253 y_shift = i ? desc->log2_chroma_h : 0;
255 if (padtop || padleft) {
256 memset(dst->data[i], color[i],
257 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
260 if (padleft || padright) {
261 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
262 (dst->linesize[i] - (padright >> x_shift));
263 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
264 for (y = 0; y < yheight; y++) {
265 memset(optr, color[i], (padleft + padright) >> x_shift);
266 optr += dst->linesize[i];
270 if (src) { /* first line */
271 uint8_t *iptr = src->data[i];
272 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
273 (padleft >> x_shift);
274 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
275 iptr += src->linesize[i];
276 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
277 (dst->linesize[i] - (padright >> x_shift));
278 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
279 for (y = 0; y < yheight; y++) {
280 memset(optr, color[i], (padleft + padright) >> x_shift);
281 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
282 (width - padleft - padright) >> x_shift);
283 iptr += src->linesize[i];
284 optr += dst->linesize[i];
288 if (padbottom || padright) {
289 optr = dst->data[i] + dst->linesize[i] *
290 ((height - padbottom) >> y_shift) - (padright >> x_shift);
291 memset(optr, color[i],dst->linesize[i] *
292 (padbottom >> y_shift) + (padright >> x_shift));
298 #if FF_API_DEINTERLACE
300 #if !HAVE_MMX_EXTERNAL
301 /* filter parameters: [-1 4 2 4 -1] // 8 */
302 static void deinterlace_line_c(uint8_t *dst,
303 const uint8_t *lum_m4, const uint8_t *lum_m3,
304 const uint8_t *lum_m2, const uint8_t *lum_m1,
308 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
311 for(;size > 0;size--) {
313 sum += lum_m3[0] << 2;
314 sum += lum_m2[0] << 1;
315 sum += lum_m1[0] << 2;
317 dst[0] = cm[(sum + 4) >> 3];
327 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
328 uint8_t *lum_m2, uint8_t *lum_m1,
329 uint8_t *lum, int size)
331 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
334 for(;size > 0;size--) {
336 sum += lum_m3[0] << 2;
337 sum += lum_m2[0] << 1;
339 sum += lum_m1[0] << 2;
341 lum_m2[0] = cm[(sum + 4) >> 3];
349 #endif /* !HAVE_MMX_EXTERNAL */
351 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
352 top field is copied as is, but the bottom field is deinterlaced
353 against the top field. */
354 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
355 const uint8_t *src1, int src_wrap,
356 int width, int height)
358 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
363 src_0=&src_m1[src_wrap];
364 src_p1=&src_0[src_wrap];
365 src_p2=&src_p1[src_wrap];
366 for(y=0;y<(height-2);y+=2) {
367 memcpy(dst,src_m1,width);
369 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
373 src_p1 += 2*src_wrap;
374 src_p2 += 2*src_wrap;
377 memcpy(dst,src_m1,width);
380 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
383 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
384 int width, int height)
386 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
389 buf = av_malloc(width);
392 memcpy(buf,src_m1,width);
393 src_0=&src_m1[src_wrap];
394 src_p1=&src_0[src_wrap];
395 src_p2=&src_p1[src_wrap];
396 for(y=0;y<(height-2);y+=2) {
397 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
400 src_p1 += 2*src_wrap;
401 src_p2 += 2*src_wrap;
404 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
408 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
409 enum AVPixelFormat pix_fmt, int width, int height)
413 if (pix_fmt != AV_PIX_FMT_YUV420P &&
414 pix_fmt != AV_PIX_FMT_YUVJ420P &&
415 pix_fmt != AV_PIX_FMT_YUV422P &&
416 pix_fmt != AV_PIX_FMT_YUVJ422P &&
417 pix_fmt != AV_PIX_FMT_YUV444P &&
418 pix_fmt != AV_PIX_FMT_YUV411P &&
419 pix_fmt != AV_PIX_FMT_GRAY8)
421 if ((width & 3) != 0 || (height & 3) != 0)
427 case AV_PIX_FMT_YUVJ420P:
428 case AV_PIX_FMT_YUV420P:
432 case AV_PIX_FMT_YUV422P:
433 case AV_PIX_FMT_YUVJ422P:
436 case AV_PIX_FMT_YUV411P:
442 if (pix_fmt == AV_PIX_FMT_GRAY8) {
447 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
450 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
451 src->data[i], src->linesize[i],
459 #endif /* FF_API_DEINTERLACE */
468 for (i=0; i<AV_PIX_FMT_NB*2; i++) {
469 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
470 if(!desc || !desc->name) {
475 av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
478 av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc));
479 if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
480 av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");