FFmpeg  2.6.9
 All Data Structures Namespaces 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 "imgconvert.h"
35 #include "internal.h"
36 #include "mathops.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 void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
44 {
45  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
46  av_assert0(desc);
47  *h_shift = desc->log2_chroma_w;
48  *v_shift = desc->log2_chroma_h;
49 }
50 
52  enum AVPixelFormat src_pix_fmt,
53  int has_alpha)
54 {
55  return av_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
56 }
57 
58 enum AVPixelFormat avcodec_find_best_pix_fmt_of_2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
59  enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
60 {
61  return av_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
62 }
63 
64 #if AV_HAVE_INCOMPATIBLE_LIBAV_ABI
65 enum AVPixelFormat avcodec_find_best_pix_fmt2(const enum AVPixelFormat *pix_fmt_list,
66  enum AVPixelFormat src_pix_fmt,
67  int has_alpha, int *loss_ptr){
68  return avcodec_find_best_pix_fmt_of_list(pix_fmt_list, src_pix_fmt, has_alpha, loss_ptr);
69 }
70 #else
71 enum AVPixelFormat avcodec_find_best_pix_fmt2(enum AVPixelFormat dst_pix_fmt1, enum AVPixelFormat dst_pix_fmt2,
72  enum AVPixelFormat src_pix_fmt, int has_alpha, int *loss_ptr)
73 {
74  return avcodec_find_best_pix_fmt_of_2(dst_pix_fmt1, dst_pix_fmt2, src_pix_fmt, has_alpha, loss_ptr);
75 }
76 #endif
77 
79  enum AVPixelFormat src_pix_fmt,
80  int has_alpha, int *loss_ptr){
81  int i;
82 
83  enum AVPixelFormat best = AV_PIX_FMT_NONE;
84 
85  for(i=0; pix_fmt_list[i] != AV_PIX_FMT_NONE; i++)
86  best = avcodec_find_best_pix_fmt_of_2(best, pix_fmt_list[i], src_pix_fmt, has_alpha, loss_ptr);
87 
88  return best;
89 }
90 
91 /* 2x2 -> 1x1 */
92 void ff_shrink22(uint8_t *dst, int dst_wrap,
93  const uint8_t *src, int src_wrap,
94  int width, int height)
95 {
96  int w;
97  const uint8_t *s1, *s2;
98  uint8_t *d;
99 
100  for(;height > 0; height--) {
101  s1 = src;
102  s2 = s1 + src_wrap;
103  d = dst;
104  for(w = width;w >= 4; w-=4) {
105  d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
106  d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
107  d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
108  d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
109  s1 += 8;
110  s2 += 8;
111  d += 4;
112  }
113  for(;w > 0; w--) {
114  d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
115  s1 += 2;
116  s2 += 2;
117  d++;
118  }
119  src += 2 * src_wrap;
120  dst += dst_wrap;
121  }
122 }
123 
124 /* 4x4 -> 1x1 */
125 void ff_shrink44(uint8_t *dst, int dst_wrap,
126  const uint8_t *src, int src_wrap,
127  int width, int height)
128 {
129  int w;
130  const uint8_t *s1, *s2, *s3, *s4;
131  uint8_t *d;
132 
133  for(;height > 0; height--) {
134  s1 = src;
135  s2 = s1 + src_wrap;
136  s3 = s2 + src_wrap;
137  s4 = s3 + src_wrap;
138  d = dst;
139  for(w = width;w > 0; w--) {
140  d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
141  s2[0] + s2[1] + s2[2] + s2[3] +
142  s3[0] + s3[1] + s3[2] + s3[3] +
143  s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
144  s1 += 4;
145  s2 += 4;
146  s3 += 4;
147  s4 += 4;
148  d++;
149  }
150  src += 4 * src_wrap;
151  dst += dst_wrap;
152  }
153 }
154 
155 /* 8x8 -> 1x1 */
156 void ff_shrink88(uint8_t *dst, int dst_wrap,
157  const uint8_t *src, int src_wrap,
158  int width, int height)
159 {
160  int w, i;
161 
162  for(;height > 0; height--) {
163  for(w = width;w > 0; w--) {
164  int tmp=0;
165  for(i=0; i<8; i++){
166  tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
167  src += src_wrap;
168  }
169  *(dst++) = (tmp + 32)>>6;
170  src += 8 - 8*src_wrap;
171  }
172  src += 8*src_wrap - 8*width;
173  dst += dst_wrap - width;
174  }
175 }
176 
177 /* return true if yuv planar */
178 static inline int is_yuv_planar(const AVPixFmtDescriptor *desc)
179 {
180  int i;
181  int planes[4] = { 0 };
182 
183  if ( desc->flags & AV_PIX_FMT_FLAG_RGB
184  || !(desc->flags & AV_PIX_FMT_FLAG_PLANAR))
185  return 0;
186 
187  /* set the used planes */
188  for (i = 0; i < desc->nb_components; i++)
189  planes[desc->comp[i].plane] = 1;
190 
191  /* if there is an unused plane, the format is not planar */
192  for (i = 0; i < desc->nb_components; i++)
193  if (!planes[i])
194  return 0;
195  return 1;
196 }
197 
199  enum AVPixelFormat pix_fmt, int top_band, int left_band)
200 {
201  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
202  int y_shift;
203  int x_shift;
204 
205  if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
206  return -1;
207 
208  y_shift = desc->log2_chroma_h;
209  x_shift = desc->log2_chroma_w;
210 
211  if (is_yuv_planar(desc)) {
212  dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
213  dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
214  dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
215  } else{
216  if(top_band % (1<<y_shift) || left_band % (1<<x_shift))
217  return -1;
218  if(left_band) //FIXME add support for this too
219  return -1;
220  dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
221  }
222 
223  dst->linesize[0] = src->linesize[0];
224  dst->linesize[1] = src->linesize[1];
225  dst->linesize[2] = src->linesize[2];
226  return 0;
227 }
228 
229 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
230  enum AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
231  int *color)
232 {
233  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
234  uint8_t *optr;
235  int y_shift;
236  int x_shift;
237  int yheight;
238  int i, y;
239  int max_step[4];
240 
241  if (pix_fmt < 0 || pix_fmt >= AV_PIX_FMT_NB)
242  return -1;
243 
244  if (!is_yuv_planar(desc)) {
245  if (src)
246  return -1; //TODO: Not yet implemented
247 
248  av_image_fill_max_pixsteps(max_step, NULL, desc);
249 
250  if (padtop || padleft) {
251  memset(dst->data[0], color[0],
252  dst->linesize[0] * padtop + (padleft * max_step[0]));
253  }
254 
255  if (padleft || padright) {
256  optr = dst->data[0] + dst->linesize[0] * padtop +
257  (dst->linesize[0] - (padright * max_step[0]));
258  yheight = height - 1 - (padtop + padbottom);
259  for (y = 0; y < yheight; y++) {
260  memset(optr, color[0], (padleft + padright) * max_step[0]);
261  optr += dst->linesize[0];
262  }
263  }
264 
265  if (padbottom || padright) {
266  optr = dst->data[0] + dst->linesize[0] * (height - padbottom) -
267  (padright * max_step[0]);
268  memset(optr, color[0], dst->linesize[0] * padbottom +
269  (padright * max_step[0]));
270  }
271 
272  return 0;
273  }
274 
275  for (i = 0; i < 3; i++) {
276  x_shift = i ? desc->log2_chroma_w : 0;
277  y_shift = i ? desc->log2_chroma_h : 0;
278 
279  if (padtop || padleft) {
280  memset(dst->data[i], color[i],
281  dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
282  }
283 
284  if (padleft || padright) {
285  optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
286  (dst->linesize[i] - (padright >> x_shift));
287  yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
288  for (y = 0; y < yheight; y++) {
289  memset(optr, color[i], (padleft + padright) >> x_shift);
290  optr += dst->linesize[i];
291  }
292  }
293 
294  if (src) { /* first line */
295  uint8_t *iptr = src->data[i];
296  optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
297  (padleft >> x_shift);
298  memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
299  iptr += src->linesize[i];
300  optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
301  (dst->linesize[i] - (padright >> x_shift));
302  yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
303  for (y = 0; y < yheight; y++) {
304  memset(optr, color[i], (padleft + padright) >> x_shift);
305  memcpy(optr + ((padleft + padright) >> x_shift), iptr,
306  (width - padleft - padright) >> x_shift);
307  iptr += src->linesize[i];
308  optr += dst->linesize[i];
309  }
310  }
311 
312  if (padbottom || padright) {
313  optr = dst->data[i] + dst->linesize[i] *
314  ((height - padbottom) >> y_shift) - (padright >> x_shift);
315  memset(optr, color[i],dst->linesize[i] *
316  (padbottom >> y_shift) + (padright >> x_shift));
317  }
318  }
319 
320  return 0;
321 }
322 
323 #if FF_API_DEINTERLACE
324 
325 #if HAVE_MMX_EXTERNAL
326 #define deinterlace_line_inplace ff_deinterlace_line_inplace_mmx
327 #define deinterlace_line ff_deinterlace_line_mmx
328 #else
329 #define deinterlace_line_inplace deinterlace_line_inplace_c
330 #define deinterlace_line deinterlace_line_c
331 
332 /* filter parameters: [-1 4 2 4 -1] // 8 */
333 static void deinterlace_line_c(uint8_t *dst,
334  const uint8_t *lum_m4, const uint8_t *lum_m3,
335  const uint8_t *lum_m2, const uint8_t *lum_m1,
336  const uint8_t *lum,
337  int size)
338 {
339  const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
340  int sum;
341 
342  for(;size > 0;size--) {
343  sum = -lum_m4[0];
344  sum += lum_m3[0] << 2;
345  sum += lum_m2[0] << 1;
346  sum += lum_m1[0] << 2;
347  sum += -lum[0];
348  dst[0] = cm[(sum + 4) >> 3];
349  lum_m4++;
350  lum_m3++;
351  lum_m2++;
352  lum_m1++;
353  lum++;
354  dst++;
355  }
356 }
357 
358 static void deinterlace_line_inplace_c(uint8_t *lum_m4, uint8_t *lum_m3,
359  uint8_t *lum_m2, uint8_t *lum_m1,
360  uint8_t *lum, int size)
361 {
362  const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
363  int sum;
364 
365  for(;size > 0;size--) {
366  sum = -lum_m4[0];
367  sum += lum_m3[0] << 2;
368  sum += lum_m2[0] << 1;
369  lum_m4[0]=lum_m2[0];
370  sum += lum_m1[0] << 2;
371  sum += -lum[0];
372  lum_m2[0] = cm[(sum + 4) >> 3];
373  lum_m4++;
374  lum_m3++;
375  lum_m2++;
376  lum_m1++;
377  lum++;
378  }
379 }
380 #endif /* !HAVE_MMX_EXTERNAL */
381 
382 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
383  top field is copied as is, but the bottom field is deinterlaced
384  against the top field. */
385 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
386  const uint8_t *src1, int src_wrap,
387  int width, int height)
388 {
389  const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
390  int y;
391 
392  src_m2 = src1;
393  src_m1 = src1;
394  src_0=&src_m1[src_wrap];
395  src_p1=&src_0[src_wrap];
396  src_p2=&src_p1[src_wrap];
397  for(y=0;y<(height-2);y+=2) {
398  memcpy(dst,src_m1,width);
399  dst += dst_wrap;
400  deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
401  src_m2 = src_0;
402  src_m1 = src_p1;
403  src_0 = src_p2;
404  src_p1 += 2*src_wrap;
405  src_p2 += 2*src_wrap;
406  dst += dst_wrap;
407  }
408  memcpy(dst,src_m1,width);
409  dst += dst_wrap;
410  /* do last line */
411  deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
412 }
413 
414 static int deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
415  int width, int height)
416 {
417  uint8_t *src_m1, *src_0, *src_p1, *src_p2;
418  int y;
419  uint8_t *buf;
420  buf = av_malloc(width);
421  if (!buf)
422  return AVERROR(ENOMEM);
423 
424  src_m1 = src1;
425  memcpy(buf,src_m1,width);
426  src_0=&src_m1[src_wrap];
427  src_p1=&src_0[src_wrap];
428  src_p2=&src_p1[src_wrap];
429  for(y=0;y<(height-2);y+=2) {
430  deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
431  src_m1 = src_p1;
432  src_0 = src_p2;
433  src_p1 += 2*src_wrap;
434  src_p2 += 2*src_wrap;
435  }
436  /* do last line */
437  deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
438  av_free(buf);
439  return 0;
440 }
441 
443  enum AVPixelFormat pix_fmt, int width, int height)
444 {
445  int i, ret;
446 
447  if (pix_fmt != AV_PIX_FMT_YUV420P &&
448  pix_fmt != AV_PIX_FMT_YUVJ420P &&
449  pix_fmt != AV_PIX_FMT_YUV422P &&
450  pix_fmt != AV_PIX_FMT_YUVJ422P &&
451  pix_fmt != AV_PIX_FMT_YUV444P &&
452  pix_fmt != AV_PIX_FMT_YUV411P &&
453  pix_fmt != AV_PIX_FMT_GRAY8)
454  return -1;
455  if ((width & 3) != 0 || (height & 3) != 0)
456  return -1;
457 
458  for(i=0;i<3;i++) {
459  if (i == 1) {
460  switch(pix_fmt) {
461  case AV_PIX_FMT_YUVJ420P:
462  case AV_PIX_FMT_YUV420P:
463  width >>= 1;
464  height >>= 1;
465  break;
466  case AV_PIX_FMT_YUV422P:
467  case AV_PIX_FMT_YUVJ422P:
468  width >>= 1;
469  break;
470  case AV_PIX_FMT_YUV411P:
471  width >>= 2;
472  break;
473  default:
474  break;
475  }
476  if (pix_fmt == AV_PIX_FMT_GRAY8) {
477  break;
478  }
479  }
480  if (src == dst) {
482  dst->linesize[i],
483  width, height);
484  if (ret < 0)
485  return ret;
486  } else {
487  deinterlace_bottom_field(dst->data[i],dst->linesize[i],
488  src->data[i], src->linesize[i],
489  width, height);
490  }
491  }
492  emms_c();
493  return 0;
494 }
495 
496 #endif /* FF_API_DEINTERLACE */
497 
498 #ifdef TEST
499 
500 int main(void){
501  int i;
502  int err=0;
503  int skip = 0;
504 
505  for (i=0; i<AV_PIX_FMT_NB*2; i++) {
506  const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(i);
507  if(!desc || !desc->name) {
508  skip ++;
509  continue;
510  }
511  if (skip) {
512  av_log(NULL, AV_LOG_INFO, "%3d unused pixel format values\n", skip);
513  skip = 0;
514  }
515  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));
516  if ((!(desc->flags & AV_PIX_FMT_FLAG_ALPHA)) != (desc->nb_components != 2 && desc->nb_components != 4)) {
517  av_log(NULL, AV_LOG_ERROR, "Alpha flag mismatch\n");
518  err = 1;
519  }
520  }
521  return err;
522 }
523 
524 #endif