70e6c0b4ffe7978420c38f23f3f1c76b7b43ac48
[libav.git] / libavcodec / imgconvert.c
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 "internal.h"
36 #include "imgconvert.h"
37 #include "libavutil/colorspace.h"
38 #include "libavutil/pixdesc.h"
39 #include "libavcore/imgutils.h"
40
41 #if HAVE_MMX
42 #include "x86/mmx.h"
43 #include "x86/dsputil_mmx.h"
44 #endif
45
46 #define xglue(x, y) x ## y
47 #define glue(x, y) xglue(x, y)
48
49 #define FF_COLOR_RGB 0 /**< RGB color space */
50 #define FF_COLOR_GRAY 1 /**< gray color space */
51 #define FF_COLOR_YUV 2 /**< YUV color space. 16 <= Y <= 235, 16 <= U, V <= 240 */
52 #define FF_COLOR_YUV_JPEG 3 /**< YUV color space. 0 <= Y <= 255, 0 <= U, V <= 255 */
53
54 #define FF_PIXEL_PLANAR 0 /**< each channel has one component in AVPicture */
55 #define FF_PIXEL_PACKED 1 /**< only one components containing all the channels */
56 #define FF_PIXEL_PALETTE 2 /**< one components containing indexes for a palette */
57
58 typedef struct PixFmtInfo {
59 uint8_t nb_channels; /**< number of channels (including alpha) */
60 uint8_t color_type; /**< color type (see FF_COLOR_xxx constants) */
61 uint8_t pixel_type; /**< pixel storage type (see FF_PIXEL_xxx constants) */
62 uint8_t is_alpha : 1; /**< true if alpha can be specified */
63 uint8_t depth; /**< bit depth of the color components */
64 } PixFmtInfo;
65
66 /* this table gives more information about formats */
67 static const PixFmtInfo pix_fmt_info[PIX_FMT_NB] = {
68 /* YUV formats */
69 [PIX_FMT_YUV420P] = {
70 .nb_channels = 3,
71 .color_type = FF_COLOR_YUV,
72 .pixel_type = FF_PIXEL_PLANAR,
73 .depth = 8,
74 },
75 [PIX_FMT_YUV422P] = {
76 .nb_channels = 3,
77 .color_type = FF_COLOR_YUV,
78 .pixel_type = FF_PIXEL_PLANAR,
79 .depth = 8,
80 },
81 [PIX_FMT_YUV444P] = {
82 .nb_channels = 3,
83 .color_type = FF_COLOR_YUV,
84 .pixel_type = FF_PIXEL_PLANAR,
85 .depth = 8,
86 },
87 [PIX_FMT_YUYV422] = {
88 .nb_channels = 1,
89 .color_type = FF_COLOR_YUV,
90 .pixel_type = FF_PIXEL_PACKED,
91 .depth = 8,
92 },
93 [PIX_FMT_UYVY422] = {
94 .nb_channels = 1,
95 .color_type = FF_COLOR_YUV,
96 .pixel_type = FF_PIXEL_PACKED,
97 .depth = 8,
98 },
99 [PIX_FMT_YUV410P] = {
100 .nb_channels = 3,
101 .color_type = FF_COLOR_YUV,
102 .pixel_type = FF_PIXEL_PLANAR,
103 .depth = 8,
104 },
105 [PIX_FMT_YUV411P] = {
106 .nb_channels = 3,
107 .color_type = FF_COLOR_YUV,
108 .pixel_type = FF_PIXEL_PLANAR,
109 .depth = 8,
110 },
111 [PIX_FMT_YUV440P] = {
112 .nb_channels = 3,
113 .color_type = FF_COLOR_YUV,
114 .pixel_type = FF_PIXEL_PLANAR,
115 .depth = 8,
116 },
117 [PIX_FMT_YUV420P16LE] = {
118 .nb_channels = 3,
119 .color_type = FF_COLOR_YUV,
120 .pixel_type = FF_PIXEL_PLANAR,
121 .depth = 16,
122 },
123 [PIX_FMT_YUV422P16LE] = {
124 .nb_channels = 3,
125 .color_type = FF_COLOR_YUV,
126 .pixel_type = FF_PIXEL_PLANAR,
127 .depth = 16,
128 },
129 [PIX_FMT_YUV444P16LE] = {
130 .nb_channels = 3,
131 .color_type = FF_COLOR_YUV,
132 .pixel_type = FF_PIXEL_PLANAR,
133 .depth = 16,
134 },
135 [PIX_FMT_YUV420P16BE] = {
136 .nb_channels = 3,
137 .color_type = FF_COLOR_YUV,
138 .pixel_type = FF_PIXEL_PLANAR,
139 .depth = 16,
140 },
141 [PIX_FMT_YUV422P16BE] = {
142 .nb_channels = 3,
143 .color_type = FF_COLOR_YUV,
144 .pixel_type = FF_PIXEL_PLANAR,
145 .depth = 16,
146 },
147 [PIX_FMT_YUV444P16BE] = {
148 .nb_channels = 3,
149 .color_type = FF_COLOR_YUV,
150 .pixel_type = FF_PIXEL_PLANAR,
151 .depth = 16,
152 },
153
154
155 /* YUV formats with alpha plane */
156 [PIX_FMT_YUVA420P] = {
157 .nb_channels = 4,
158 .color_type = FF_COLOR_YUV,
159 .pixel_type = FF_PIXEL_PLANAR,
160 .depth = 8,
161 },
162
163 /* JPEG YUV */
164 [PIX_FMT_YUVJ420P] = {
165 .nb_channels = 3,
166 .color_type = FF_COLOR_YUV_JPEG,
167 .pixel_type = FF_PIXEL_PLANAR,
168 .depth = 8,
169 },
170 [PIX_FMT_YUVJ422P] = {
171 .nb_channels = 3,
172 .color_type = FF_COLOR_YUV_JPEG,
173 .pixel_type = FF_PIXEL_PLANAR,
174 .depth = 8,
175 },
176 [PIX_FMT_YUVJ444P] = {
177 .nb_channels = 3,
178 .color_type = FF_COLOR_YUV_JPEG,
179 .pixel_type = FF_PIXEL_PLANAR,
180 .depth = 8,
181 },
182 [PIX_FMT_YUVJ440P] = {
183 .nb_channels = 3,
184 .color_type = FF_COLOR_YUV_JPEG,
185 .pixel_type = FF_PIXEL_PLANAR,
186 .depth = 8,
187 },
188
189 /* RGB formats */
190 [PIX_FMT_RGB24] = {
191 .nb_channels = 3,
192 .color_type = FF_COLOR_RGB,
193 .pixel_type = FF_PIXEL_PACKED,
194 .depth = 8,
195 },
196 [PIX_FMT_BGR24] = {
197 .nb_channels = 3,
198 .color_type = FF_COLOR_RGB,
199 .pixel_type = FF_PIXEL_PACKED,
200 .depth = 8,
201 },
202 [PIX_FMT_ARGB] = {
203 .nb_channels = 4, .is_alpha = 1,
204 .color_type = FF_COLOR_RGB,
205 .pixel_type = FF_PIXEL_PACKED,
206 .depth = 8,
207 },
208 [PIX_FMT_RGB48BE] = {
209 .nb_channels = 3,
210 .color_type = FF_COLOR_RGB,
211 .pixel_type = FF_PIXEL_PACKED,
212 .depth = 16,
213 },
214 [PIX_FMT_RGB48LE] = {
215 .nb_channels = 3,
216 .color_type = FF_COLOR_RGB,
217 .pixel_type = FF_PIXEL_PACKED,
218 .depth = 16,
219 },
220 [PIX_FMT_RGB565BE] = {
221 .nb_channels = 3,
222 .color_type = FF_COLOR_RGB,
223 .pixel_type = FF_PIXEL_PACKED,
224 .depth = 5,
225 },
226 [PIX_FMT_RGB565LE] = {
227 .nb_channels = 3,
228 .color_type = FF_COLOR_RGB,
229 .pixel_type = FF_PIXEL_PACKED,
230 .depth = 5,
231 },
232 [PIX_FMT_RGB555BE] = {
233 .nb_channels = 3,
234 .color_type = FF_COLOR_RGB,
235 .pixel_type = FF_PIXEL_PACKED,
236 .depth = 5,
237 },
238 [PIX_FMT_RGB555LE] = {
239 .nb_channels = 3,
240 .color_type = FF_COLOR_RGB,
241 .pixel_type = FF_PIXEL_PACKED,
242 .depth = 5,
243 },
244 [PIX_FMT_RGB444BE] = {
245 .nb_channels = 3,
246 .color_type = FF_COLOR_RGB,
247 .pixel_type = FF_PIXEL_PACKED,
248 .depth = 4,
249 },
250 [PIX_FMT_RGB444LE] = {
251 .nb_channels = 3,
252 .color_type = FF_COLOR_RGB,
253 .pixel_type = FF_PIXEL_PACKED,
254 .depth = 4,
255 },
256
257 /* gray / mono formats */
258 [PIX_FMT_GRAY16BE] = {
259 .nb_channels = 1,
260 .color_type = FF_COLOR_GRAY,
261 .pixel_type = FF_PIXEL_PLANAR,
262 .depth = 16,
263 },
264 [PIX_FMT_GRAY16LE] = {
265 .nb_channels = 1,
266 .color_type = FF_COLOR_GRAY,
267 .pixel_type = FF_PIXEL_PLANAR,
268 .depth = 16,
269 },
270 [PIX_FMT_GRAY8] = {
271 .nb_channels = 1,
272 .color_type = FF_COLOR_GRAY,
273 .pixel_type = FF_PIXEL_PLANAR,
274 .depth = 8,
275 },
276 [PIX_FMT_MONOWHITE] = {
277 .nb_channels = 1,
278 .color_type = FF_COLOR_GRAY,
279 .pixel_type = FF_PIXEL_PLANAR,
280 .depth = 1,
281 },
282 [PIX_FMT_MONOBLACK] = {
283 .nb_channels = 1,
284 .color_type = FF_COLOR_GRAY,
285 .pixel_type = FF_PIXEL_PLANAR,
286 .depth = 1,
287 },
288
289 /* paletted formats */
290 [PIX_FMT_PAL8] = {
291 .nb_channels = 4, .is_alpha = 1,
292 .color_type = FF_COLOR_RGB,
293 .pixel_type = FF_PIXEL_PALETTE,
294 .depth = 8,
295 },
296 [PIX_FMT_UYYVYY411] = {
297 .nb_channels = 1,
298 .color_type = FF_COLOR_YUV,
299 .pixel_type = FF_PIXEL_PACKED,
300 .depth = 8,
301 },
302 [PIX_FMT_ABGR] = {
303 .nb_channels = 4, .is_alpha = 1,
304 .color_type = FF_COLOR_RGB,
305 .pixel_type = FF_PIXEL_PACKED,
306 .depth = 8,
307 },
308 [PIX_FMT_BGR565BE] = {
309 .nb_channels = 3,
310 .color_type = FF_COLOR_RGB,
311 .pixel_type = FF_PIXEL_PACKED,
312 .depth = 5,
313 },
314 [PIX_FMT_BGR565LE] = {
315 .nb_channels = 3,
316 .color_type = FF_COLOR_RGB,
317 .pixel_type = FF_PIXEL_PACKED,
318 .depth = 5,
319 },
320 [PIX_FMT_BGR555BE] = {
321 .nb_channels = 3,
322 .color_type = FF_COLOR_RGB,
323 .pixel_type = FF_PIXEL_PACKED,
324 .depth = 5,
325 },
326 [PIX_FMT_BGR555LE] = {
327 .nb_channels = 3,
328 .color_type = FF_COLOR_RGB,
329 .pixel_type = FF_PIXEL_PACKED,
330 .depth = 5,
331 },
332 [PIX_FMT_BGR444BE] = {
333 .nb_channels = 3,
334 .color_type = FF_COLOR_RGB,
335 .pixel_type = FF_PIXEL_PACKED,
336 .depth = 4,
337 },
338 [PIX_FMT_BGR444LE] = {
339 .nb_channels = 3,
340 .color_type = FF_COLOR_RGB,
341 .pixel_type = FF_PIXEL_PACKED,
342 .depth = 4,
343 },
344 [PIX_FMT_RGB8] = {
345 .nb_channels = 1,
346 .color_type = FF_COLOR_RGB,
347 .pixel_type = FF_PIXEL_PACKED,
348 .depth = 8,
349 },
350 [PIX_FMT_RGB4] = {
351 .nb_channels = 1,
352 .color_type = FF_COLOR_RGB,
353 .pixel_type = FF_PIXEL_PACKED,
354 .depth = 4,
355 },
356 [PIX_FMT_RGB4_BYTE] = {
357 .nb_channels = 1,
358 .color_type = FF_COLOR_RGB,
359 .pixel_type = FF_PIXEL_PACKED,
360 .depth = 8,
361 },
362 [PIX_FMT_BGR8] = {
363 .nb_channels = 1,
364 .color_type = FF_COLOR_RGB,
365 .pixel_type = FF_PIXEL_PACKED,
366 .depth = 8,
367 },
368 [PIX_FMT_BGR4] = {
369 .nb_channels = 1,
370 .color_type = FF_COLOR_RGB,
371 .pixel_type = FF_PIXEL_PACKED,
372 .depth = 4,
373 },
374 [PIX_FMT_BGR4_BYTE] = {
375 .nb_channels = 1,
376 .color_type = FF_COLOR_RGB,
377 .pixel_type = FF_PIXEL_PACKED,
378 .depth = 8,
379 },
380 [PIX_FMT_NV12] = {
381 .nb_channels = 2,
382 .color_type = FF_COLOR_YUV,
383 .pixel_type = FF_PIXEL_PLANAR,
384 .depth = 8,
385 },
386 [PIX_FMT_NV21] = {
387 .nb_channels = 2,
388 .color_type = FF_COLOR_YUV,
389 .pixel_type = FF_PIXEL_PLANAR,
390 .depth = 8,
391 },
392
393 [PIX_FMT_BGRA] = {
394 .nb_channels = 4, .is_alpha = 1,
395 .color_type = FF_COLOR_RGB,
396 .pixel_type = FF_PIXEL_PACKED,
397 .depth = 8,
398 },
399 [PIX_FMT_RGBA] = {
400 .nb_channels = 4, .is_alpha = 1,
401 .color_type = FF_COLOR_RGB,
402 .pixel_type = FF_PIXEL_PACKED,
403 .depth = 8,
404 },
405 };
406
407 void avcodec_get_chroma_sub_sample(enum PixelFormat pix_fmt, int *h_shift, int *v_shift)
408 {
409 *h_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
410 *v_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
411 }
412
413 const char *avcodec_get_pix_fmt_name(enum PixelFormat pix_fmt)
414 {
415 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB)
416 return NULL;
417 else
418 return av_pix_fmt_descriptors[pix_fmt].name;
419 }
420
421 #if LIBAVCODEC_VERSION_MAJOR < 53
422 enum PixelFormat avcodec_get_pix_fmt(const char *name)
423 {
424 return av_get_pix_fmt(name);
425 }
426 #endif
427
428 void avcodec_pix_fmt_string (char *buf, int buf_size, enum PixelFormat pix_fmt)
429 {
430 /* print header */
431 if (pix_fmt < 0)
432 snprintf (buf, buf_size,
433 "name " " nb_channels" " depth" " is_alpha"
434 );
435 else{
436 PixFmtInfo info= pix_fmt_info[pix_fmt];
437
438 char is_alpha_char= info.is_alpha ? 'y' : 'n';
439
440 snprintf (buf, buf_size,
441 "%-11s %5d %9d %6c",
442 av_pix_fmt_descriptors[pix_fmt].name,
443 info.nb_channels,
444 info.depth,
445 is_alpha_char
446 );
447 }
448 }
449
450 int ff_is_hwaccel_pix_fmt(enum PixelFormat pix_fmt)
451 {
452 return av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_HWACCEL;
453 }
454
455 int ff_set_systematic_pal(uint32_t pal[256], enum PixelFormat pix_fmt){
456 int i;
457
458 for(i=0; i<256; i++){
459 int r,g,b;
460
461 switch(pix_fmt) {
462 case PIX_FMT_RGB8:
463 r= (i>>5 )*36;
464 g= ((i>>2)&7)*36;
465 b= (i&3 )*85;
466 break;
467 case PIX_FMT_BGR8:
468 b= (i>>6 )*85;
469 g= ((i>>3)&7)*36;
470 r= (i&7 )*36;
471 break;
472 case PIX_FMT_RGB4_BYTE:
473 r= (i>>3 )*255;
474 g= ((i>>1)&3)*85;
475 b= (i&1 )*255;
476 break;
477 case PIX_FMT_BGR4_BYTE:
478 b= (i>>3 )*255;
479 g= ((i>>1)&3)*85;
480 r= (i&1 )*255;
481 break;
482 case PIX_FMT_GRAY8:
483 r=b=g= i;
484 break;
485 default:
486 return -1;
487 }
488 pal[i] = b + (g<<8) + (r<<16);
489 }
490
491 return 0;
492 }
493
494 int ff_fill_linesize(AVPicture *picture, enum PixelFormat pix_fmt, int width)
495 {
496 return av_fill_image_linesizes(picture->linesize, pix_fmt, width);
497 }
498
499 int ff_fill_pointer(AVPicture *picture, uint8_t *ptr, enum PixelFormat pix_fmt,
500 int height)
501 {
502 return av_fill_image_pointers(picture->data, pix_fmt, height, ptr, picture->linesize);
503 }
504
505 int avpicture_fill(AVPicture *picture, uint8_t *ptr,
506 enum PixelFormat pix_fmt, int width, int height)
507 {
508
509 if(avcodec_check_dimensions(NULL, width, height))
510 return -1;
511
512 if (ff_fill_linesize(picture, pix_fmt, width))
513 return -1;
514
515 return ff_fill_pointer(picture, ptr, pix_fmt, height);
516 }
517
518 int avpicture_layout(const AVPicture* src, enum PixelFormat pix_fmt, int width, int height,
519 unsigned char *dest, int dest_size)
520 {
521 const PixFmtInfo* pf = &pix_fmt_info[pix_fmt];
522 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
523 int i, j, w, ow, h, oh, data_planes;
524 const unsigned char* s;
525 int size = avpicture_get_size(pix_fmt, width, height);
526
527 if (size > dest_size || size < 0)
528 return -1;
529
530 if (pf->pixel_type == FF_PIXEL_PACKED || pf->pixel_type == FF_PIXEL_PALETTE) {
531 if (pix_fmt == PIX_FMT_YUYV422 ||
532 pix_fmt == PIX_FMT_UYVY422 ||
533 pix_fmt == PIX_FMT_BGR565BE ||
534 pix_fmt == PIX_FMT_BGR565LE ||
535 pix_fmt == PIX_FMT_BGR555BE ||
536 pix_fmt == PIX_FMT_BGR555LE ||
537 pix_fmt == PIX_FMT_BGR444BE ||
538 pix_fmt == PIX_FMT_BGR444LE ||
539 pix_fmt == PIX_FMT_RGB565BE ||
540 pix_fmt == PIX_FMT_RGB565LE ||
541 pix_fmt == PIX_FMT_RGB555BE ||
542 pix_fmt == PIX_FMT_RGB555LE ||
543 pix_fmt == PIX_FMT_RGB444BE ||
544 pix_fmt == PIX_FMT_RGB444LE)
545 w = width * 2;
546 else if (pix_fmt == PIX_FMT_UYYVYY411)
547 w = width + width/2;
548 else if (pix_fmt == PIX_FMT_PAL8)
549 w = width;
550 else
551 w = width * (pf->depth * pf->nb_channels / 8);
552
553 data_planes = 1;
554 h = height;
555 } else {
556 data_planes = pf->nb_channels;
557 w = (width*pf->depth + 7)/8;
558 h = height;
559 }
560
561 ow = w;
562 oh = h;
563
564 for (i=0; i<data_planes; i++) {
565 if (i == 1) {
566 w = (- ((-width) >> desc->log2_chroma_w) * pf->depth + 7) / 8;
567 h = -((-height) >> desc->log2_chroma_h);
568 if (pix_fmt == PIX_FMT_NV12 || pix_fmt == PIX_FMT_NV21)
569 w <<= 1;
570 } else if (i == 3) {
571 w = ow;
572 h = oh;
573 }
574 s = src->data[i];
575 for(j=0; j<h; j++) {
576 memcpy(dest, s, w);
577 dest += w;
578 s += src->linesize[i];
579 }
580 }
581
582 if (pf->pixel_type == FF_PIXEL_PALETTE)
583 memcpy((unsigned char *)(((size_t)dest + 3) & ~3), src->data[1], 256 * 4);
584
585 return size;
586 }
587
588 int avpicture_get_size(enum PixelFormat pix_fmt, int width, int height)
589 {
590 AVPicture dummy_pict;
591 if(avcodec_check_dimensions(NULL, width, height))
592 return -1;
593 switch (pix_fmt) {
594 case PIX_FMT_RGB8:
595 case PIX_FMT_BGR8:
596 case PIX_FMT_RGB4_BYTE:
597 case PIX_FMT_BGR4_BYTE:
598 case PIX_FMT_GRAY8:
599 // do not include palette for these pseudo-paletted formats
600 return width * height;
601 }
602 return avpicture_fill(&dummy_pict, NULL, pix_fmt, width, height);
603 }
604
605 int avcodec_get_pix_fmt_loss(enum PixelFormat dst_pix_fmt, enum PixelFormat src_pix_fmt,
606 int has_alpha)
607 {
608 const PixFmtInfo *pf, *ps;
609 const AVPixFmtDescriptor *src_desc = &av_pix_fmt_descriptors[src_pix_fmt];
610 const AVPixFmtDescriptor *dst_desc = &av_pix_fmt_descriptors[dst_pix_fmt];
611 int loss;
612
613 ps = &pix_fmt_info[src_pix_fmt];
614
615 /* compute loss */
616 loss = 0;
617 pf = &pix_fmt_info[dst_pix_fmt];
618 if (pf->depth < ps->depth ||
619 ((dst_pix_fmt == PIX_FMT_RGB555BE || dst_pix_fmt == PIX_FMT_RGB555LE ||
620 dst_pix_fmt == PIX_FMT_BGR555BE || dst_pix_fmt == PIX_FMT_BGR555LE) &&
621 (src_pix_fmt == PIX_FMT_RGB565BE || src_pix_fmt == PIX_FMT_RGB565LE ||
622 src_pix_fmt == PIX_FMT_BGR565BE || src_pix_fmt == PIX_FMT_BGR565LE)))
623 loss |= FF_LOSS_DEPTH;
624 if (dst_desc->log2_chroma_w > src_desc->log2_chroma_w ||
625 dst_desc->log2_chroma_h > src_desc->log2_chroma_h)
626 loss |= FF_LOSS_RESOLUTION;
627 switch(pf->color_type) {
628 case FF_COLOR_RGB:
629 if (ps->color_type != FF_COLOR_RGB &&
630 ps->color_type != FF_COLOR_GRAY)
631 loss |= FF_LOSS_COLORSPACE;
632 break;
633 case FF_COLOR_GRAY:
634 if (ps->color_type != FF_COLOR_GRAY)
635 loss |= FF_LOSS_COLORSPACE;
636 break;
637 case FF_COLOR_YUV:
638 if (ps->color_type != FF_COLOR_YUV)
639 loss |= FF_LOSS_COLORSPACE;
640 break;
641 case FF_COLOR_YUV_JPEG:
642 if (ps->color_type != FF_COLOR_YUV_JPEG &&
643 ps->color_type != FF_COLOR_YUV &&
644 ps->color_type != FF_COLOR_GRAY)
645 loss |= FF_LOSS_COLORSPACE;
646 break;
647 default:
648 /* fail safe test */
649 if (ps->color_type != pf->color_type)
650 loss |= FF_LOSS_COLORSPACE;
651 break;
652 }
653 if (pf->color_type == FF_COLOR_GRAY &&
654 ps->color_type != FF_COLOR_GRAY)
655 loss |= FF_LOSS_CHROMA;
656 if (!pf->is_alpha && (ps->is_alpha && has_alpha))
657 loss |= FF_LOSS_ALPHA;
658 if (pf->pixel_type == FF_PIXEL_PALETTE &&
659 (ps->pixel_type != FF_PIXEL_PALETTE && ps->color_type != FF_COLOR_GRAY))
660 loss |= FF_LOSS_COLORQUANT;
661 return loss;
662 }
663
664 static int avg_bits_per_pixel(enum PixelFormat pix_fmt)
665 {
666 int bits;
667 const PixFmtInfo *pf;
668 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
669
670 pf = &pix_fmt_info[pix_fmt];
671 switch(pf->pixel_type) {
672 case FF_PIXEL_PACKED:
673 switch(pix_fmt) {
674 case PIX_FMT_YUYV422:
675 case PIX_FMT_UYVY422:
676 case PIX_FMT_RGB565BE:
677 case PIX_FMT_RGB565LE:
678 case PIX_FMT_RGB555BE:
679 case PIX_FMT_RGB555LE:
680 case PIX_FMT_RGB444BE:
681 case PIX_FMT_RGB444LE:
682 case PIX_FMT_BGR565BE:
683 case PIX_FMT_BGR565LE:
684 case PIX_FMT_BGR555BE:
685 case PIX_FMT_BGR555LE:
686 case PIX_FMT_BGR444BE:
687 case PIX_FMT_BGR444LE:
688 bits = 16;
689 break;
690 case PIX_FMT_UYYVYY411:
691 bits = 12;
692 break;
693 default:
694 bits = pf->depth * pf->nb_channels;
695 break;
696 }
697 break;
698 case FF_PIXEL_PLANAR:
699 if (desc->log2_chroma_w == 0 && desc->log2_chroma_h == 0) {
700 bits = pf->depth * pf->nb_channels;
701 } else {
702 bits = pf->depth + ((2 * pf->depth) >>
703 (desc->log2_chroma_w + desc->log2_chroma_h));
704 }
705 break;
706 case FF_PIXEL_PALETTE:
707 bits = 8;
708 break;
709 default:
710 bits = -1;
711 break;
712 }
713 return bits;
714 }
715
716 static enum PixelFormat avcodec_find_best_pix_fmt1(int64_t pix_fmt_mask,
717 enum PixelFormat src_pix_fmt,
718 int has_alpha,
719 int loss_mask)
720 {
721 int dist, i, loss, min_dist;
722 enum PixelFormat dst_pix_fmt;
723
724 /* find exact color match with smallest size */
725 dst_pix_fmt = PIX_FMT_NONE;
726 min_dist = 0x7fffffff;
727 for(i = 0;i < PIX_FMT_NB; i++) {
728 if (pix_fmt_mask & (1ULL << i)) {
729 loss = avcodec_get_pix_fmt_loss(i, src_pix_fmt, has_alpha) & loss_mask;
730 if (loss == 0) {
731 dist = avg_bits_per_pixel(i);
732 if (dist < min_dist) {
733 min_dist = dist;
734 dst_pix_fmt = i;
735 }
736 }
737 }
738 }
739 return dst_pix_fmt;
740 }
741
742 enum PixelFormat avcodec_find_best_pix_fmt(int64_t pix_fmt_mask, enum PixelFormat src_pix_fmt,
743 int has_alpha, int *loss_ptr)
744 {
745 enum PixelFormat dst_pix_fmt;
746 int loss_mask, i;
747 static const int loss_mask_order[] = {
748 ~0, /* no loss first */
749 ~FF_LOSS_ALPHA,
750 ~FF_LOSS_RESOLUTION,
751 ~(FF_LOSS_COLORSPACE | FF_LOSS_RESOLUTION),
752 ~FF_LOSS_COLORQUANT,
753 ~FF_LOSS_DEPTH,
754 0,
755 };
756
757 /* try with successive loss */
758 i = 0;
759 for(;;) {
760 loss_mask = loss_mask_order[i++];
761 dst_pix_fmt = avcodec_find_best_pix_fmt1(pix_fmt_mask, src_pix_fmt,
762 has_alpha, loss_mask);
763 if (dst_pix_fmt >= 0)
764 goto found;
765 if (loss_mask == 0)
766 break;
767 }
768 return PIX_FMT_NONE;
769 found:
770 if (loss_ptr)
771 *loss_ptr = avcodec_get_pix_fmt_loss(dst_pix_fmt, src_pix_fmt, has_alpha);
772 return dst_pix_fmt;
773 }
774
775 void ff_img_copy_plane(uint8_t *dst, int dst_wrap,
776 const uint8_t *src, int src_wrap,
777 int width, int height)
778 {
779 if((!dst) || (!src))
780 return;
781 for(;height > 0; height--) {
782 memcpy(dst, src, width);
783 dst += dst_wrap;
784 src += src_wrap;
785 }
786 }
787
788 int ff_get_plane_bytewidth(enum PixelFormat pix_fmt, int width, int plane)
789 {
790 int bits;
791 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
792 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
793
794 pf = &pix_fmt_info[pix_fmt];
795 switch(pf->pixel_type) {
796 case FF_PIXEL_PACKED:
797 switch(pix_fmt) {
798 case PIX_FMT_YUYV422:
799 case PIX_FMT_UYVY422:
800 case PIX_FMT_RGB565BE:
801 case PIX_FMT_RGB565LE:
802 case PIX_FMT_RGB555BE:
803 case PIX_FMT_RGB555LE:
804 case PIX_FMT_RGB444BE:
805 case PIX_FMT_RGB444LE:
806 case PIX_FMT_BGR565BE:
807 case PIX_FMT_BGR565LE:
808 case PIX_FMT_BGR555BE:
809 case PIX_FMT_BGR555LE:
810 case PIX_FMT_BGR444BE:
811 case PIX_FMT_BGR444LE:
812 bits = 16;
813 break;
814 case PIX_FMT_UYYVYY411:
815 bits = 12;
816 break;
817 default:
818 bits = pf->depth * pf->nb_channels;
819 break;
820 }
821 return (width * bits + 7) >> 3;
822 break;
823 case FF_PIXEL_PLANAR:
824 if ((pix_fmt != PIX_FMT_NV12 && pix_fmt != PIX_FMT_NV21) &&
825 (plane == 1 || plane == 2))
826 width= -((-width)>>desc->log2_chroma_w);
827
828 return (width * pf->depth + 7) >> 3;
829 break;
830 case FF_PIXEL_PALETTE:
831 if (plane == 0)
832 return width;
833 break;
834 }
835
836 return -1;
837 }
838
839 void av_picture_copy(AVPicture *dst, const AVPicture *src,
840 enum PixelFormat pix_fmt, int width, int height)
841 {
842 int i;
843 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
844 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[pix_fmt];
845
846 switch(pf->pixel_type) {
847 case FF_PIXEL_PACKED:
848 case FF_PIXEL_PLANAR:
849 for(i = 0; i < pf->nb_channels; i++) {
850 int h;
851 int bwidth = ff_get_plane_bytewidth(pix_fmt, width, i);
852 h = height;
853 if (i == 1 || i == 2) {
854 h= -((-height)>>desc->log2_chroma_h);
855 }
856 ff_img_copy_plane(dst->data[i], dst->linesize[i],
857 src->data[i], src->linesize[i],
858 bwidth, h);
859 }
860 break;
861 case FF_PIXEL_PALETTE:
862 ff_img_copy_plane(dst->data[0], dst->linesize[0],
863 src->data[0], src->linesize[0],
864 width, height);
865 /* copy the palette */
866 memcpy(dst->data[1], src->data[1], 4*256);
867 break;
868 }
869 }
870
871 /* 2x2 -> 1x1 */
872 void ff_shrink22(uint8_t *dst, int dst_wrap,
873 const uint8_t *src, int src_wrap,
874 int width, int height)
875 {
876 int w;
877 const uint8_t *s1, *s2;
878 uint8_t *d;
879
880 for(;height > 0; height--) {
881 s1 = src;
882 s2 = s1 + src_wrap;
883 d = dst;
884 for(w = width;w >= 4; w-=4) {
885 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
886 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
887 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
888 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
889 s1 += 8;
890 s2 += 8;
891 d += 4;
892 }
893 for(;w > 0; w--) {
894 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
895 s1 += 2;
896 s2 += 2;
897 d++;
898 }
899 src += 2 * src_wrap;
900 dst += dst_wrap;
901 }
902 }
903
904 /* 4x4 -> 1x1 */
905 void ff_shrink44(uint8_t *dst, int dst_wrap,
906 const uint8_t *src, int src_wrap,
907 int width, int height)
908 {
909 int w;
910 const uint8_t *s1, *s2, *s3, *s4;
911 uint8_t *d;
912
913 for(;height > 0; height--) {
914 s1 = src;
915 s2 = s1 + src_wrap;
916 s3 = s2 + src_wrap;
917 s4 = s3 + src_wrap;
918 d = dst;
919 for(w = width;w > 0; w--) {
920 d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
921 s2[0] + s2[1] + s2[2] + s2[3] +
922 s3[0] + s3[1] + s3[2] + s3[3] +
923 s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
924 s1 += 4;
925 s2 += 4;
926 s3 += 4;
927 s4 += 4;
928 d++;
929 }
930 src += 4 * src_wrap;
931 dst += dst_wrap;
932 }
933 }
934
935 /* 8x8 -> 1x1 */
936 void ff_shrink88(uint8_t *dst, int dst_wrap,
937 const uint8_t *src, int src_wrap,
938 int width, int height)
939 {
940 int w, i;
941
942 for(;height > 0; height--) {
943 for(w = width;w > 0; w--) {
944 int tmp=0;
945 for(i=0; i<8; i++){
946 tmp += src[0] + src[1] + src[2] + src[3] + src[4] + src[5] + src[6] + src[7];
947 src += src_wrap;
948 }
949 *(dst++) = (tmp + 32)>>6;
950 src += 8 - 8*src_wrap;
951 }
952 src += 8*src_wrap - 8*width;
953 dst += dst_wrap - width;
954 }
955 }
956
957
958 int avpicture_alloc(AVPicture *picture,
959 enum PixelFormat pix_fmt, int width, int height)
960 {
961 int size;
962 void *ptr;
963
964 size = avpicture_fill(picture, NULL, pix_fmt, width, height);
965 if(size<0)
966 goto fail;
967 ptr = av_malloc(size);
968 if (!ptr)
969 goto fail;
970 avpicture_fill(picture, ptr, pix_fmt, width, height);
971 if(picture->data[1] && !picture->data[2])
972 ff_set_systematic_pal((uint32_t*)picture->data[1], pix_fmt);
973
974 return 0;
975 fail:
976 memset(picture, 0, sizeof(AVPicture));
977 return -1;
978 }
979
980 void avpicture_free(AVPicture *picture)
981 {
982 av_free(picture->data[0]);
983 }
984
985 /* return true if yuv planar */
986 static inline int is_yuv_planar(const PixFmtInfo *ps)
987 {
988 return (ps->color_type == FF_COLOR_YUV ||
989 ps->color_type == FF_COLOR_YUV_JPEG) &&
990 ps->pixel_type == FF_PIXEL_PLANAR;
991 }
992
993 int av_picture_crop(AVPicture *dst, const AVPicture *src,
994 enum PixelFormat pix_fmt, int top_band, int left_band)
995 {
996 int y_shift;
997 int x_shift;
998
999 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB || !is_yuv_planar(&pix_fmt_info[pix_fmt]))
1000 return -1;
1001
1002 y_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_h;
1003 x_shift = av_pix_fmt_descriptors[pix_fmt].log2_chroma_w;
1004
1005 dst->data[0] = src->data[0] + (top_band * src->linesize[0]) + left_band;
1006 dst->data[1] = src->data[1] + ((top_band >> y_shift) * src->linesize[1]) + (left_band >> x_shift);
1007 dst->data[2] = src->data[2] + ((top_band >> y_shift) * src->linesize[2]) + (left_band >> x_shift);
1008
1009 dst->linesize[0] = src->linesize[0];
1010 dst->linesize[1] = src->linesize[1];
1011 dst->linesize[2] = src->linesize[2];
1012 return 0;
1013 }
1014
1015 int av_picture_pad(AVPicture *dst, const AVPicture *src, int height, int width,
1016 enum PixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright,
1017 int *color)
1018 {
1019 uint8_t *optr;
1020 int y_shift;
1021 int x_shift;
1022 int yheight;
1023 int i, y;
1024
1025 if (pix_fmt < 0 || pix_fmt >= PIX_FMT_NB ||
1026 !is_yuv_planar(&pix_fmt_info[pix_fmt])) return -1;
1027
1028 for (i = 0; i < 3; i++) {
1029 x_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_w : 0;
1030 y_shift = i ? av_pix_fmt_descriptors[pix_fmt].log2_chroma_h : 0;
1031
1032 if (padtop || padleft) {
1033 memset(dst->data[i], color[i],
1034 dst->linesize[i] * (padtop >> y_shift) + (padleft >> x_shift));
1035 }
1036
1037 if (padleft || padright) {
1038 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1039 (dst->linesize[i] - (padright >> x_shift));
1040 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1041 for (y = 0; y < yheight; y++) {
1042 memset(optr, color[i], (padleft + padright) >> x_shift);
1043 optr += dst->linesize[i];
1044 }
1045 }
1046
1047 if (src) { /* first line */
1048 uint8_t *iptr = src->data[i];
1049 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1050 (padleft >> x_shift);
1051 memcpy(optr, iptr, (width - padleft - padright) >> x_shift);
1052 iptr += src->linesize[i];
1053 optr = dst->data[i] + dst->linesize[i] * (padtop >> y_shift) +
1054 (dst->linesize[i] - (padright >> x_shift));
1055 yheight = (height - 1 - (padtop + padbottom)) >> y_shift;
1056 for (y = 0; y < yheight; y++) {
1057 memset(optr, color[i], (padleft + padright) >> x_shift);
1058 memcpy(optr + ((padleft + padright) >> x_shift), iptr,
1059 (width - padleft - padright) >> x_shift);
1060 iptr += src->linesize[i];
1061 optr += dst->linesize[i];
1062 }
1063 }
1064
1065 if (padbottom || padright) {
1066 optr = dst->data[i] + dst->linesize[i] *
1067 ((height - padbottom) >> y_shift) - (padright >> x_shift);
1068 memset(optr, color[i],dst->linesize[i] *
1069 (padbottom >> y_shift) + (padright >> x_shift));
1070 }
1071 }
1072 return 0;
1073 }
1074
1075 /* NOTE: we scan all the pixels to have an exact information */
1076 static int get_alpha_info_pal8(const AVPicture *src, int width, int height)
1077 {
1078 const unsigned char *p;
1079 int src_wrap, ret, x, y;
1080 unsigned int a;
1081 uint32_t *palette = (uint32_t *)src->data[1];
1082
1083 p = src->data[0];
1084 src_wrap = src->linesize[0] - width;
1085 ret = 0;
1086 for(y=0;y<height;y++) {
1087 for(x=0;x<width;x++) {
1088 a = palette[p[0]] >> 24;
1089 if (a == 0x00) {
1090 ret |= FF_ALPHA_TRANSP;
1091 } else if (a != 0xff) {
1092 ret |= FF_ALPHA_SEMI_TRANSP;
1093 }
1094 p++;
1095 }
1096 p += src_wrap;
1097 }
1098 return ret;
1099 }
1100
1101 int img_get_alpha_info(const AVPicture *src,
1102 enum PixelFormat pix_fmt, int width, int height)
1103 {
1104 const PixFmtInfo *pf = &pix_fmt_info[pix_fmt];
1105 int ret;
1106
1107 /* no alpha can be represented in format */
1108 if (!pf->is_alpha)
1109 return 0;
1110 switch(pix_fmt) {
1111 case PIX_FMT_PAL8:
1112 ret = get_alpha_info_pal8(src, width, height);
1113 break;
1114 default:
1115 /* we do not know, so everything is indicated */
1116 ret = FF_ALPHA_TRANSP | FF_ALPHA_SEMI_TRANSP;
1117 break;
1118 }
1119 return ret;
1120 }
1121
1122 #if HAVE_MMX
1123 #define DEINT_INPLACE_LINE_LUM \
1124 movd_m2r(lum_m4[0],mm0);\
1125 movd_m2r(lum_m3[0],mm1);\
1126 movd_m2r(lum_m2[0],mm2);\
1127 movd_m2r(lum_m1[0],mm3);\
1128 movd_m2r(lum[0],mm4);\
1129 punpcklbw_r2r(mm7,mm0);\
1130 movd_r2m(mm2,lum_m4[0]);\
1131 punpcklbw_r2r(mm7,mm1);\
1132 punpcklbw_r2r(mm7,mm2);\
1133 punpcklbw_r2r(mm7,mm3);\
1134 punpcklbw_r2r(mm7,mm4);\
1135 paddw_r2r(mm3,mm1);\
1136 psllw_i2r(1,mm2);\
1137 paddw_r2r(mm4,mm0);\
1138 psllw_i2r(2,mm1);\
1139 paddw_r2r(mm6,mm2);\
1140 paddw_r2r(mm2,mm1);\
1141 psubusw_r2r(mm0,mm1);\
1142 psrlw_i2r(3,mm1);\
1143 packuswb_r2r(mm7,mm1);\
1144 movd_r2m(mm1,lum_m2[0]);
1145
1146 #define DEINT_LINE_LUM \
1147 movd_m2r(lum_m4[0],mm0);\
1148 movd_m2r(lum_m3[0],mm1);\
1149 movd_m2r(lum_m2[0],mm2);\
1150 movd_m2r(lum_m1[0],mm3);\
1151 movd_m2r(lum[0],mm4);\
1152 punpcklbw_r2r(mm7,mm0);\
1153 punpcklbw_r2r(mm7,mm1);\
1154 punpcklbw_r2r(mm7,mm2);\
1155 punpcklbw_r2r(mm7,mm3);\
1156 punpcklbw_r2r(mm7,mm4);\
1157 paddw_r2r(mm3,mm1);\
1158 psllw_i2r(1,mm2);\
1159 paddw_r2r(mm4,mm0);\
1160 psllw_i2r(2,mm1);\
1161 paddw_r2r(mm6,mm2);\
1162 paddw_r2r(mm2,mm1);\
1163 psubusw_r2r(mm0,mm1);\
1164 psrlw_i2r(3,mm1);\
1165 packuswb_r2r(mm7,mm1);\
1166 movd_r2m(mm1,dst[0]);
1167 #endif
1168
1169 /* filter parameters: [-1 4 2 4 -1] // 8 */
1170 static void deinterlace_line(uint8_t *dst,
1171 const uint8_t *lum_m4, const uint8_t *lum_m3,
1172 const uint8_t *lum_m2, const uint8_t *lum_m1,
1173 const uint8_t *lum,
1174 int size)
1175 {
1176 #if !HAVE_MMX
1177 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1178 int sum;
1179
1180 for(;size > 0;size--) {
1181 sum = -lum_m4[0];
1182 sum += lum_m3[0] << 2;
1183 sum += lum_m2[0] << 1;
1184 sum += lum_m1[0] << 2;
1185 sum += -lum[0];
1186 dst[0] = cm[(sum + 4) >> 3];
1187 lum_m4++;
1188 lum_m3++;
1189 lum_m2++;
1190 lum_m1++;
1191 lum++;
1192 dst++;
1193 }
1194 #else
1195
1196 {
1197 pxor_r2r(mm7,mm7);
1198 movq_m2r(ff_pw_4,mm6);
1199 }
1200 for (;size > 3; size-=4) {
1201 DEINT_LINE_LUM
1202 lum_m4+=4;
1203 lum_m3+=4;
1204 lum_m2+=4;
1205 lum_m1+=4;
1206 lum+=4;
1207 dst+=4;
1208 }
1209 #endif
1210 }
1211 static void deinterlace_line_inplace(uint8_t *lum_m4, uint8_t *lum_m3, uint8_t *lum_m2, uint8_t *lum_m1, uint8_t *lum,
1212 int size)
1213 {
1214 #if !HAVE_MMX
1215 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1216 int sum;
1217
1218 for(;size > 0;size--) {
1219 sum = -lum_m4[0];
1220 sum += lum_m3[0] << 2;
1221 sum += lum_m2[0] << 1;
1222 lum_m4[0]=lum_m2[0];
1223 sum += lum_m1[0] << 2;
1224 sum += -lum[0];
1225 lum_m2[0] = cm[(sum + 4) >> 3];
1226 lum_m4++;
1227 lum_m3++;
1228 lum_m2++;
1229 lum_m1++;
1230 lum++;
1231 }
1232 #else
1233
1234 {
1235 pxor_r2r(mm7,mm7);
1236 movq_m2r(ff_pw_4,mm6);
1237 }
1238 for (;size > 3; size-=4) {
1239 DEINT_INPLACE_LINE_LUM
1240 lum_m4+=4;
1241 lum_m3+=4;
1242 lum_m2+=4;
1243 lum_m1+=4;
1244 lum+=4;
1245 }
1246 #endif
1247 }
1248
1249 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
1250 top field is copied as is, but the bottom field is deinterlaced
1251 against the top field. */
1252 static void deinterlace_bottom_field(uint8_t *dst, int dst_wrap,
1253 const uint8_t *src1, int src_wrap,
1254 int width, int height)
1255 {
1256 const uint8_t *src_m2, *src_m1, *src_0, *src_p1, *src_p2;
1257 int y;
1258
1259 src_m2 = src1;
1260 src_m1 = src1;
1261 src_0=&src_m1[src_wrap];
1262 src_p1=&src_0[src_wrap];
1263 src_p2=&src_p1[src_wrap];
1264 for(y=0;y<(height-2);y+=2) {
1265 memcpy(dst,src_m1,width);
1266 dst += dst_wrap;
1267 deinterlace_line(dst,src_m2,src_m1,src_0,src_p1,src_p2,width);
1268 src_m2 = src_0;
1269 src_m1 = src_p1;
1270 src_0 = src_p2;
1271 src_p1 += 2*src_wrap;
1272 src_p2 += 2*src_wrap;
1273 dst += dst_wrap;
1274 }
1275 memcpy(dst,src_m1,width);
1276 dst += dst_wrap;
1277 /* do last line */
1278 deinterlace_line(dst,src_m2,src_m1,src_0,src_0,src_0,width);
1279 }
1280
1281 static void deinterlace_bottom_field_inplace(uint8_t *src1, int src_wrap,
1282 int width, int height)
1283 {
1284 uint8_t *src_m1, *src_0, *src_p1, *src_p2;
1285 int y;
1286 uint8_t *buf;
1287 buf = (uint8_t*)av_malloc(width);
1288
1289 src_m1 = src1;
1290 memcpy(buf,src_m1,width);
1291 src_0=&src_m1[src_wrap];
1292 src_p1=&src_0[src_wrap];
1293 src_p2=&src_p1[src_wrap];
1294 for(y=0;y<(height-2);y+=2) {
1295 deinterlace_line_inplace(buf,src_m1,src_0,src_p1,src_p2,width);
1296 src_m1 = src_p1;
1297 src_0 = src_p2;
1298 src_p1 += 2*src_wrap;
1299 src_p2 += 2*src_wrap;
1300 }
1301 /* do last line */
1302 deinterlace_line_inplace(buf,src_m1,src_0,src_0,src_0,width);
1303 av_free(buf);
1304 }
1305
1306 int avpicture_deinterlace(AVPicture *dst, const AVPicture *src,
1307 enum PixelFormat pix_fmt, int width, int height)
1308 {
1309 int i;
1310
1311 if (pix_fmt != PIX_FMT_YUV420P &&
1312 pix_fmt != PIX_FMT_YUV422P &&
1313 pix_fmt != PIX_FMT_YUV444P &&
1314 pix_fmt != PIX_FMT_YUV411P &&
1315 pix_fmt != PIX_FMT_GRAY8)
1316 return -1;
1317 if ((width & 3) != 0 || (height & 3) != 0)
1318 return -1;
1319
1320 for(i=0;i<3;i++) {
1321 if (i == 1) {
1322 switch(pix_fmt) {
1323 case PIX_FMT_YUV420P:
1324 width >>= 1;
1325 height >>= 1;
1326 break;
1327 case PIX_FMT_YUV422P:
1328 width >>= 1;
1329 break;
1330 case PIX_FMT_YUV411P:
1331 width >>= 2;
1332 break;
1333 default:
1334 break;
1335 }
1336 if (pix_fmt == PIX_FMT_GRAY8) {
1337 break;
1338 }
1339 }
1340 if (src == dst) {
1341 deinterlace_bottom_field_inplace(dst->data[i], dst->linesize[i],
1342 width, height);
1343 } else {
1344 deinterlace_bottom_field(dst->data[i],dst->linesize[i],
1345 src->data[i], src->linesize[i],
1346 width, height);
1347 }
1348 }
1349 emms_c();
1350 return 0;
1351 }
1352