FFmpeg
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
imgconvert.c
Go to the documentation of this file.
1 /*
2  * Misc image conversion routines
3  * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 
22 /**
23  * @file
24  * misc image conversion routines
25  */
26 
27 /* TODO:
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
31  */
32 
33 #include "avcodec.h"
34 #include "dsputil.h"
35 #include "imgconvert.h"
36 #include "internal.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"
42 
43 #if HAVE_MMX_EXTERNAL
44 #include "x86/dsputil_mmx.h"
45 #endif
46 
47 #define FF_COLOR_NA -1
48 #define FF_COLOR_RGB 0 /**< RGB color space */
49 #define FF_COLOR_GRAY 1 /**< gray color space */
50 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
51 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
52 
53 #if HAVE_MMX_EXTERNAL
54 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
55 #define deinterlace_line ff_deinterlace_line_mmx
56 #else
57 #define deinterlace_line_inplace deinterlace_line_inplace_c
58 #define deinterlace_line deinterlace_line_c
59 #endif
60 
61 #define pixdesc_has_alpha(pixdesc) \
62  ((pixdesc)->nb_components == 2 || (pixdesc)->nb_components == 4 || (pixdesc)->flags & PIX_FMT_PAL)
63 
64 
65 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
66 {
67  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
68  av_assert0(desc);
69  *h_shift = desc->log2_chroma_w;
70  *v_shift = desc->log2_chroma_h;
71 }
72 
73 static int get_color_type(const AVPixFmtDescriptor *desc) {
74  if(desc->nb_components == 1 || desc->nb_components == 2)
75  return FF_COLOR_GRAY;
76 
77  if(desc->name && !strncmp(desc->name, "yuvj", 4))
78  return FF_COLOR_YUV_JPEG;
79 
80  if(desc->flags & PIX_FMT_RGB)
81  return FF_COLOR_RGB;
82 
83  if(desc->nb_components == 0)
84  return FF_COLOR_NA;
85 
86  return FF_COLOR_YUV;
87 }
88 
89 static int get_pix_fmt_depth(int *min, int *max, enum AVPixelFormat pix_fmt)
90 {
91  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
92  int i;
93 
94  if (!desc || !desc->nb_components) {
95  *min = *max = 0;
96  return AVERROR(EINVAL);
97  }
98 
99  *min = INT_MAX, *max = -INT_MAX;
100  for (i = 0; i < desc->nb_components; i++) {
101  *min = FFMIN(desc->comp[i].depth_minus1+1, *min);
102  *max = FFMAX(desc->comp[i].depth_minus1+1, *max);
103  }
104  return 0;
105 }
106 
107 static int get_pix_fmt_score(enum AVPixelFormat dst_pix_fmt,
108  enum AVPixelFormat src_pix_fmt,
109  unsigned *lossp, unsigned consider)
110 {
111  const AVPixFmtDescriptor *src_desc = av_pix_fmt_desc_get(src_pix_fmt);
112  const AVPixFmtDescriptor *dst_desc = av_pix_fmt_desc_get(dst_pix_fmt);
113  int src_color, dst_color;
114  int src_min_depth, src_max_depth, dst_min_depth, dst_max_depth;
115  int ret, loss, i, nb_components;
116  int score = INT_MAX;
117 
118  if (dst_pix_fmt >= AV_PIX_FMT_NB || dst_pix_fmt <= AV_PIX_FMT_NONE)
119  return ~0;
120 
121  /* compute loss */
122  *lossp = loss = 0;
123 
124  if (dst_pix_fmt == src_pix_fmt)
125  return INT_MAX;
126 
127  if ((ret = get_pix_fmt_depth(&src_min_depth, &src_max_depth, src_pix_fmt)) < 0)
128  return ret;
129  if ((ret = get_pix_fmt_depth(&dst_min_depth, &dst_max_depth, dst_pix_fmt)) < 0)
130  return ret;
131 
132  src_color = get_color_type(src_desc);
133  dst_color = get_color_type(dst_desc);
134  nb_components = FFMIN(src_desc->nb_components, dst_desc->nb_components);
135 
136  for (i = 0; i < nb_components; i++)
137  if (src_desc->comp[i].depth_minus1 > dst_desc->comp[i].depth_minus1 && (consider & FF_LOSS_DEPTH)) {
138  loss |= FF_LOSS_DEPTH;
139  score -= 65536 >> dst_desc->comp[i].depth_minus1;
140  }
141 
142  if (consider & FF_LOSS_RESOLUTION) {
143  if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w) {
144  loss |= FF_LOSS_RESOLUTION;
145  score -= 256 << dst_desc->log2_chroma_w;
146  }
147  if (dst_desc->log2_chroma_h > src_desc->log2_chroma_h) {
148  loss |= FF_LOSS_RESOLUTION;
149  score -= 256 << dst_desc->log2_chroma_h;
150  }
151  // dont favor 422 over 420 if downsampling is needed, because 420 has much better support on the decoder side
152  if (dst_desc->log2_chroma_w == 1 && src_desc->log2_chroma_w == 0 &&
153  dst_desc->log2_chroma_h == 1 && src_desc->log2_chroma_h == 0 ) {
154  score += 512;
155  }
156  }
157 
158  if(consider & FF_LOSS_COLORSPACE)
159  switch(dst_color) {
160  case FF_COLOR_RGB:
161  if (src_color != FF_COLOR_RGB &&
162  src_color != FF_COLOR_GRAY)
163  loss |= FF_LOSS_COLORSPACE;
164  break;
165  case FF_COLOR_GRAY:
166  if (src_color != FF_COLOR_GRAY)
167  loss |= FF_LOSS_COLORSPACE;
168  break;
169  case FF_COLOR_YUV:
170  if (src_color != FF_COLOR_YUV)
171  loss |= FF_LOSS_COLORSPACE;
172  break;
173  case FF_COLOR_YUV_JPEG:
174  if (src_color != FF_COLOR_YUV_JPEG &&
175  src_color != FF_COLOR_YUV &&
176  src_color != FF_COLOR_GRAY)
177  loss |= FF_LOSS_COLORSPACE;
178  break;
179  default:
180  /* fail safe test */
181  if (src_color != dst_color)
182  loss |= FF_LOSS_COLORSPACE;
183  break;
184  }
185  if(loss & FF_LOSS_COLORSPACE)
186  score -= (nb_components * 65536) >> FFMIN(dst_desc->comp[0].depth_minus1, src_desc->comp[0].depth_minus1);
187 
188  if (dst_color == FF_COLOR_GRAY &&
189  src_color != FF_COLOR_GRAY && (consider & FF_LOSS_CHROMA)) {
190  loss |= FF_LOSS_CHROMA;
191  score -= 2 * 65536;
192  }
193  if (!pixdesc_has_alpha(dst_desc) && (pixdesc_has_alpha(src_desc) && (consider & FF_LOSS_ALPHA))) {
194  loss |= FF_LOSS_ALPHA;
195  score -= 65536;
196  }
197  if (dst_pix_fmt == AV_PIX_FMT_PAL8 && (consider & FF_LOSS_COLORQUANT) &&
198  (src_pix_fmt != AV_PIX_FMT_PAL8 && (src_color != FF_COLOR_GRAY || (pixdesc_has_alpha(src_desc) && (consider & FF_LOSS_ALPHA))))) {
199  loss |= FF_LOSS_COLORQUANT;
200  score -= 65536;
201  }
202 
203  *lossp = loss;
204  return score;
205 }
206 
208  enum AVPixelFormat src_pix_fmt,
209  int has_alpha)
210 {
211  int loss;
212  int ret = get_pix_fmt_score(dst_pix_fmt, src_pix_fmt, &loss, has_alpha ? ~0 : ~FF_LOSS_ALPHA);
213  if (ret < 0)
214  return ret;
215  return loss;
216 }
217 
218 #if FF_API_FIND_BEST_PIX_FMT
219 enum AVPixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum AVPixelFormat src_pix_fmt,
220  int has_alpha, int *loss_ptr)
221 {
222  enum AVPixelFormat dst_pix_fmt;
223  int i;
224 
225  if (loss_ptr) /* all losses count (for backward compatibility) */
226  *loss_ptr = 0;
227 
228  dst_pix_fmt = AV_PIX_FMT_NONE; /* so first iteration doesn't have to be treated special */
229  for(i = 0; i< FFMIN(AV_PIX_FMT_NB, 64); i++){
230  if (pix_fmt_mask & (1ULL << i))
231  dst_pix_fmt = avcodec_find_best_pix_fmt_of_2(dst_pix_fmt, i, src_pix_fmt, has_alpha, loss_ptr);
232  }
233  return dst_pix_fmt;
234 }
235 #endif /* FF_API_FIND_BEST_PIX_FMT */
236 
238  enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
239 {
240  enum AVPixelFormat dst_pix_fmt;
241  int loss1, loss2, loss_mask;
242  const AVPixFmtDescriptor *desc1 = av_pix_fmt_desc_get(dst_pix_fmt1);
243  const AVPixFmtDescriptor *desc2 = av_pix_fmt_desc_get(dst_pix_fmt2);
244  int score1, score2;
245 
246  loss_mask= loss_ptr?~*loss_ptr:~0; /* use loss mask if provided */
247  if(!has_alpha)
248  loss_mask &= ~FF_LOSS_ALPHA;
249 
250  dst_pix_fmt = AV_PIX_FMT_NONE;
251  score1 = get_pix_fmt_score(dst_pix_fmt1, src_pix_fmt, &loss1, loss_mask);
252  score2 = get_pix_fmt_score(dst_pix_fmt2, src_pix_fmt, &loss2, loss_mask);
253 
254  if (score1 == score2) {
256  dst_pix_fmt = av_get_padded_bits_per_pixel(desc2) < av_get_padded_bits_per_pixel(desc1) ? dst_pix_fmt2 : dst_pix_fmt1;
257  } else {
258  dst_pix_fmt = desc2->nb_components < desc1->nb_components ? dst_pix_fmt2 : dst_pix_fmt1;
259  }
260  } else {
261  dst_pix_fmt = score1 < score2 ? dst_pix_fmt2 : dst_pix_fmt1;
262  }
263 
264  if (loss_ptr)
265  *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
266  return dst_pix_fmt;
267 }
268 
269 #if AV_HAVE_INCOMPATIBLE_FORK_ABI
271  enum AVPixelFormat src_pix_fmt,
272  int has_alpha, int *loss_ptr){
273  return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
274 }
275 #else
276 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
277  enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
278 {
279  return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
280 }
281 #endif
282 
284  enum AVPixelFormat src_pix_fmt,
285  int has_alpha, int *loss_ptr){
286  int i;
287 
288  enum AVPixelFormat best = AV_PIX_FMT_NONE;
289 
290  for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
291  best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
292 
293  return best;
294 }
295 
296 /* 2x2 -> 1x1 */
297 void ff_shrink22(uint8_t *dst, int dst_wrap,
298  const uint8_t *src, int src_wrap,
299  int width, int height)
300 {
301  int w;
302  const uint8_t *s1, *s2;
303  uint8_t *d;
304 
305  for(;height > 0; height--) {
306  s1 = src;
307  s2 = s1 + src_wrap;
308  d = dst;
309  for(w = width;w >= 4; w-=4) {
310  d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
311  d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
312  d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
313  d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
314  s1 += 8;
315  s2 += 8;
316  d += 4;
317  }
318  for(;w > 0; w--) {
319  d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
320  s1 += 2;
321  s2 += 2;
322  d++;
323  }
324  src += 2 * src_wrap;
325  dst += dst_wrap;
326  }
327 }
328 
329 /* 4x4 -> 1x1 */
330 void ff_shrink44(uint8_t *dst, int dst_wrap,
331  const uint8_t *src, int src_wrap,
332  int width, int height)
333 {
334  int w;
335  const uint8_t *s1, *s2, *s3, *s4;
336  uint8_t *d;
337 
338  for(;height > 0; height--) {
339  s1 = src;
340  s2 = s1 + src_wrap;
341  s3 = s2 + src_wrap;
342  s4 = s3 + src_wrap;
343  d = dst;
344  for(w = width;w > 0; w--) {
345  d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
346  s2[0] + s2[1] + s2[2] + s2[3] +
347  s3[0] + s3[1] + s3[2] + s3[3] +
348  s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
349  s1 += 4;
350  s2 += 4;
351  s3 += 4;
352  s4 += 4;
353  d++;
354  }
355  src += 4 * src_wrap;
356  dst += dst_wrap;
357  }
358 }
359 
360 /* 8x8 -> 1x1 */
361 void ff_shrink88(uint8_t *dst, int dst_wrap,
362  const uint8_t *src, int src_wrap,
363  int width, int height)
364 {
365  int w, i;
366 
367  for(;height > 0; height--) {
368  for(w = width;w > 0; w--) {
369  int tmp=0;
370  for(i=0; i<8; i++){
371  tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
372  src += src_wrap;
373  }
374  *(dst++) = (tmp + 32)>>6;
375  src += 8 - 8*src_wrap;
376  }
377  src += 8*src_wrap - 8*width;
378  dst += dst_wrap - width;
379  }
380 }
381 
382 /* return true if yuv planar */
383 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
384 {
385  int i;
386  int planes[4] = { 0 };
387 
388  if ( desc->flags & PIX_FMT_RGB
389  || !(desc->flags & PIX_FMT_PLANAR))
390  return 0;
391 
392  /* set the used planes */
393  for (i = 0; i < desc->nb_components; i++)
394  planes[desc->comp[i].plane] = 1;
395 
396  /* if there is an unused plane, the format is not planar */
397  for (i = 0; i < desc->nb_components; i++)
398  if (!planes[i])
399  return 0;
400  return 1;
401 }
402 
404  enum AVPixelFormat pix_fmt, int top_band, int left_band)
405 {
406  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
407  int y_shift;
408  int x_shift;
409 
410  if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
411  return -1;
412 
413  y_shift = desc->log2_chroma_h;
414  x_shift = desc->log2_chroma_w;
415 
416  if (is_yuv_planar(desc)) {
417  dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
418  dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
419  dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
420  } else{
421  if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
422  return -1;
423  if(left_band) //FIXME add support for this too
424  return -1;
425  dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
426  }
427 
428  dst->linesize[0] = src->linesize[0];
429  dst->linesize[1] = src->linesize[1];
430  dst->linesize[2] = src->linesize[2];
431  return 0;
432 }
433 
434 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
435  enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
436  int *color)
437 {
438  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
439  uint8_t *optr;
440  int y_shift;
441  int x_shift;
442  int yheight;
443  int i, y;
444 
445  if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB ||
446  !is_yuv_planar(desc)) return -1;
447 
448  for (i = 0; i < 3; i++) {
449  x_shift = i ? desc->log2_chroma_w : 0;
450  y_shift = i ? desc->log2_chroma_h : 0;
451 
452  if (padtop || padleft) {
453  memset(dst->data[i], color[i],
454  dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
455  }
456 
457  if (padleft || padright) {
458  optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
459  (dst->linesize[i] - (padright >> x_shift));
460  yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
461  for (y = 0; y < yheight; y++) {
462  memset(optr, color[i], (padleft + padright) >> x_shift);
463  optr += dst->linesize[i];
464  }
465  }
466 
467  if (src) { /* first line */
468  uint8_t *iptr = src->data[i];
469  optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
470  (padleft >> x_shift);
471  memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
472  iptr += src->linesize[i];
473  optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
474  (dst->linesize[i] - (padright >> x_shift));
475  yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
476  for (y = 0; y < yheight; y++) {
477  memset(optr, color[i], (padleft + padright) >> x_shift);
478  memcpy(optr + ((padleft + padright) >> x_shift), iptr,
479  (width - padleft - padright) >> x_shift);
480  iptr += src->linesize[i];
481  optr += dst->linesize[i];
482  }
483  }
484 
485  if (padbottom || padright) {
486  optr = dst->data[i] + dst->linesize[i] *
487  ((height - padbottom) >> y_shift) - (padright >> x_shift);
488  memset(optr, color[i],dst->linesize[i] *
489  (padbottom >> y_shift) + (padright >> x_shift));
490  }
491  }
492  return 0;
493 }
494 
495 #if FF_API_DEINTERLACE
496 
497 #if !HAVE_MMX_EXTERNAL
498 /* filter parameters: [-1 4 2 4 -1] // 8 */
499 static void deinterlace_line_c(uint8_t *dst,
500  const uint8_t *lum_m4, const uint8_t *lum_m3,
501  const uint8_t *lum_m2, const uint8_t *lum_m1,
502  const uint8_t *lum,
503  int size)
504 {
506  int sum;
507 
508  for(;size > 0;size--) {
509  sum = -lum_m4[0];
510  sum += lum_m3[0] << 2;
511  sum += lum_m2[0] << 1;
512  sum += lum_m1[0] << 2;
513  sum += -lum[0];
514  dst[0] = cm[(sum + 4) >> 3];
515  lum_m4++;
516  lum_m3++;
517  lum_m2++;
518  lum_m1++;
519  lum++;
520  dst++;
521  }
522 }
523 
524 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
525  uint8_t *lum_m2, uint8_t *lum_m1,
526  uint8_t *lum, int size)
527 {
529  int sum;
530 
531  for(;size > 0;size--) {
532  sum = -lum_m4[0];
533  sum += lum_m3[0] << 2;
534  sum += lum_m2[0] << 1;
535  lum_m4[0]=lum_m2[0];
536  sum += lum_m1[0] << 2;
537  sum += -lum[0];
538  lum_m2[0] = cm[(sum + 4) >> 3];
539  lum_m4++;
540  lum_m3++;
541  lum_m2++;
542  lum_m1++;
543  lum++;
544  }
545 }
546 #endif /* !HAVE_MMX_EXTERNAL */
547 
548 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
549  top field is copied as is, but the bottom field is deinterlaced
550  against the top field. */
551 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
552  const uint8_t *src1, int src_wrap,
553  int width, int height)
554 {
555  const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
556  int y;
557 
558  src_m2 = src1;
559  src_m1 = src1;
560  src_0=&src_m1[src_wrap];
561  src_p1=&src_0[src_wrap];
562  src_p2=&src_p1[src_wrap];
563  for(y=0;y<(height-2);y+=2) {
564  memcpy(dst,src_m1,width);
565  dst += dst_wrap;
566  deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
567  src_m2 = src_0;
568  src_m1 = src_p1;
569  src_0 = src_p2;
570  src_p1 += 2*src_wrap;
571  src_p2 += 2*src_wrap;
572  dst += dst_wrap;
573  }
574  memcpy(dst,src_m1,width);
575  dst += dst_wrap;
576  /* do last line */
577  deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
578 }
579 
580 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
581  int width, int height)
582 {
583  uint8_t *src_m1, *src_0, *src_p1, *src_p2;
584  int y;
585  uint8_t *buf;
586  buf = av_malloc(width);
587 
588  src_m1 = src1;
589  memcpy(buf,src_m1,width);
590  src_0=&src_m1[src_wrap];
591  src_p1=&src_0[src_wrap];
592  src_p2=&src_p1[src_wrap];
593  for(y=0;y<(height-2);y+=2) {
594  deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
595  src_m1 = src_p1;
596  src_0 = src_p2;
597  src_p1 += 2*src_wrap;
598  src_p2 += 2*src_wrap;
599  }
600  /* do last line */
601  deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
602  av_free(buf);
603 }
604 
605 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
606  enum AVPixelFormat pix_fmt, int width, int height)
607 {
608  int i;
609 
610  if (pix_fmt != AV_PIX_FMT_YUV420P &&
611  pix_fmt != AV_PIX_FMT_YUVJ420P &&
612  pix_fmt != AV_PIX_FMT_YUV422P &&
613  pix_fmt != AV_PIX_FMT_YUVJ422P &&
614  pix_fmt != AV_PIX_FMT_YUV444P &&
615  pix_fmt != AV_PIX_FMT_YUV411P &&
616  pix_fmt != AV_PIX_FMT_GRAY8)
617  return -1;
618  if ((width & 3) != 0 || (height & 3) != 0)
619  return -1;
620 
621  for(i=0;i<3;i++) {
622  if (i == 1) {
623  switch(pix_fmt) {
624  case AV_PIX_FMT_YUVJ420P:
625  case AV_PIX_FMT_YUV420P:
626  width >>= 1;
627  height >>= 1;
628  break;
629  case AV_PIX_FMT_YUV422P:
630  case AV_PIX_FMT_YUVJ422P:
631  width >>= 1;
632  break;
633  case AV_PIX_FMT_YUV411P:
634  width >>= 2;
635  break;
636  default:
637  break;
638  }
639  if (pix_fmt == AV_PIX_FMT_GRAY8) {
640  break;
641  }
642  }
643  if (src == dst) {
644  deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
645  width, height);
646  } else {
647  deinterlace_bottom_field(dst->data[i],dst->linesize[i],
648  src->data[i], src->linesize[i],
649  width, height);
650  }
651  }
652  emms_c();
653  return 0;
654 }
655 
656 #endif /* FF_API_DEINTERLACE */
657 
658 #ifdef TEST
659 
660 int main(void){
661  int i;
662  int err=0;
663  int skip = 0;
664 
665  for (i=0; i<AV_PIX_FMT_NB*2; i++) {
667  if(!desc || !desc->name) {
668  skip ++;
669  continue;
670  }
671  if (skip) {
672  av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
673  skip = 0;
674  }
675  av_log(NULL, AV_LOG_INFO, "pix fmt %s yuv_plan:%d avg_bpp:%d colortype:%d\n", desc->name, is_yuv_planar(desc), av_get_padded_bits_per_pixel(desc), get_color_type(desc));
676  if ((!(desc->flags & PIX_FMT_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
677  av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
678  err = 1;
679  }
680  }
681  return err;
682 }
683 
684 #endif