2 * Misc image convertion routines
3 * Copyright (c) 2001 Gerard Lantau.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "fastmemcpy.h"
25 /* XXX: totally non optimized */
27 static void yuv422_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
28 UINT8 *src, int width, int height)
33 for(y=0;y<height;y+=2) {
34 for(x=0;x<width;x+=2) {
44 for(x=0;x<width;x+=2) {
54 #define ONE_HALF (1 << (SCALEBITS - 1))
55 #define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5))
57 static void rgb24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
58 UINT8 *src, int width, int height)
60 int wrap, wrap3, x, y;
61 int r, g, b, r1, g1, b1;
67 for(y=0;y<height;y+=2) {
68 for(x=0;x<width;x+=2) {
75 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
76 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
83 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
84 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
94 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
95 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
102 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
103 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
105 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
106 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
107 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
108 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
120 static void bgr24_to_yuv420p(UINT8 *lum, UINT8 *cb, UINT8 *cr,
121 UINT8 *src, int width, int height)
123 int wrap, wrap3, x, y;
124 int r, g, b, r1, g1, b1;
130 for(y=0;y<height;y+=2) {
131 for(x=0;x<width;x+=2) {
138 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
139 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
146 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
147 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
157 lum[0] = (FIX(0.29900) * r + FIX(0.58700) * g +
158 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
165 lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g +
166 FIX(0.11400) * b + ONE_HALF) >> SCALEBITS;
168 cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 +
169 FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
170 cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 -
171 FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128;
183 /* XXX: use generic filter ? */
185 static void shrink2(UINT8 *dst, int dst_wrap,
186 UINT8 *src, int src_wrap,
187 int width, int height)
192 for(;height > 0; height--) {
196 for(w = width;w >= 4; w-=4) {
197 d[0] = (s1[0] + s2[0]) >> 1;
198 d[1] = (s1[1] + s2[1]) >> 1;
199 d[2] = (s1[2] + s2[2]) >> 1;
200 d[3] = (s1[3] + s2[3]) >> 1;
206 d[0] = (s1[0] + s2[0]) >> 1;
217 static void shrink22(UINT8 *dst, int dst_wrap,
218 UINT8 *src, int src_wrap,
219 int width, int height)
224 for(;height > 0; height--) {
228 for(w = width;w >= 4; w-=4) {
229 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
230 d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1;
231 d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1;
232 d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1;
238 d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1;
248 static void img_copy(UINT8 *dst, int dst_wrap,
249 UINT8 *src, int src_wrap,
250 int width, int height)
252 for(;height > 0; height--) {
253 memcpy(dst, src, width);
259 #define SCALE_BITS 10
261 #define C_Y (76309 >> (16 - SCALE_BITS))
262 #define C_RV (117504 >> (16 - SCALE_BITS))
263 #define C_BU (138453 >> (16 - SCALE_BITS))
264 #define C_GU (13954 >> (16 - SCALE_BITS))
265 #define C_GV (34903 >> (16 - SCALE_BITS))
267 #define RGBOUT(r, g, b, y1)\
269 y = (y1 - 16) * C_Y;\
270 r = cm[(y + r_add) >> SCALE_BITS];\
271 g = cm[(y + g_add) >> SCALE_BITS];\
272 b = cm[(y + b_add) >> SCALE_BITS];\
275 /* XXX: no chroma interpolating is done */
276 static void yuv420p_to_rgb24(AVPicture *dst, AVPicture *src,
277 int width, int height)
279 UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2;
280 int w, y, cb, cr, r_add, g_add, b_add, width2;
281 UINT8 *cm = cropTbl + MAX_NEG_CROP;
284 y1_ptr = src->data[0];
285 cb_ptr = src->data[1];
286 cr_ptr = src->data[2];
288 for(;height > 0; height -= 2) {
290 d2 = d + dst->linesize[0];
291 y2_ptr = y1_ptr + src->linesize[0];
292 for(w = width2; w > 0; w --) {
293 cb = cb_ptr[0] - 128;
294 cr = cr_ptr[0] - 128;
295 r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
296 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
297 b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
299 /* output 4 pixels */
300 RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
301 RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
302 RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]);
303 RGBOUT(d2[3], d2[4], d2[5], y2_ptr[1]);
312 d += 2 * dst->linesize[0];
313 y1_ptr += 2 * src->linesize[0] - width;
314 cb_ptr += src->linesize[1] - width2;
315 cr_ptr += src->linesize[2] - width2;
319 /* XXX: no chroma interpolating is done */
320 static void yuv422p_to_rgb24(AVPicture *dst, AVPicture *src,
321 int width, int height)
323 UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1;
324 int w, y, cb, cr, r_add, g_add, b_add, width2;
325 UINT8 *cm = cropTbl + MAX_NEG_CROP;
328 y1_ptr = src->data[0];
329 cb_ptr = src->data[1];
330 cr_ptr = src->data[2];
332 for(;height > 0; height --) {
334 for(w = width2; w > 0; w --) {
335 cb = cb_ptr[0] - 128;
336 cr = cr_ptr[0] - 128;
337 r_add = C_RV * cr + (1 << (SCALE_BITS - 1));
338 g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1));
339 b_add = C_BU * cb + (1 << (SCALE_BITS - 1));
341 /* output 2 pixels */
342 RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]);
343 RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]);
350 d += dst->linesize[0];
351 y1_ptr += src->linesize[0] - width;
352 cb_ptr += src->linesize[1] - width2;
353 cr_ptr += src->linesize[2] - width2;
357 /* XXX: always use linesize. Return -1 if not supported */
358 int img_convert(AVPicture *dst, int dst_pix_fmt,
359 AVPicture *src, int pix_fmt,
360 int width, int height)
364 if (dst_pix_fmt == pix_fmt) {
366 case PIX_FMT_YUV420P:
372 img_copy(dst->data[i], dst->linesize[i],
373 src->data[i], src->linesize[i],
380 } else if (dst_pix_fmt == PIX_FMT_YUV420P) {
383 case PIX_FMT_YUV420P:
385 img_copy(dst->data[i], dst->linesize[i],
386 src->data[i], src->linesize[i],
390 case PIX_FMT_YUV422P:
391 img_copy(dst->data[0], dst->linesize[0],
392 src->data[0], src->linesize[0],
397 shrink2(dst->data[i], dst->linesize[i],
398 src->data[i], src->linesize[i],
402 case PIX_FMT_YUV444P:
403 img_copy(dst->data[0], dst->linesize[0],
404 src->data[0], src->linesize[0],
409 shrink22(dst->data[i], dst->linesize[i],
410 src->data[i], src->linesize[i],
415 yuv422_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
416 src->data[0], width, height);
419 rgb24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
420 src->data[0], width, height);
423 bgr24_to_yuv420p(dst->data[0], dst->data[1], dst->data[2],
424 src->data[0], width, height);
429 } else if (dst_pix_fmt == PIX_FMT_RGB24) {
431 case PIX_FMT_YUV420P:
432 yuv420p_to_rgb24(dst, src, width, height);
434 case PIX_FMT_YUV422P:
435 yuv422p_to_rgb24(dst, src, width, height);
446 /* filter parameters: [-1 4 2 4 -1] // 8 */
447 static void deinterlace_line(UINT8 *dst, UINT8 *src, int src_wrap,
450 UINT8 *cm = cropTbl + MAX_NEG_CROP;
454 for(;size > 0;size--) {
465 dst[0] = cm[(sum + 4) >> 3];
471 /* deinterlacing : 2 temporal taps, 3 spatial taps linear filter. The
472 top field is copied as is, but the bottom field is deinterlaced
473 against the top field. */
474 static void deinterlace_bottom_field(UINT8 *dst, int dst_wrap,
475 UINT8 *src1, int src_wrap,
476 int width, int height)
482 buf= (UINT8*) malloc(5 * width);
485 for(y=0;y<height;y+=2) {
486 /* copy top field line */
487 memcpy(dst, src, width);
489 src += (1 - 2) * src_wrap;
491 if (y1 >= 0 && (y1 + 4) < height) {
492 /* fast case : no edges */
493 deinterlace_line(dst, src, src_wrap, width);
495 /* in order to use the same function, we use an intermediate buffer */
499 memcpy(ptr, src1, width);
500 else if (y1 >= height)
501 memcpy(ptr, src1 + (height - 1) * src_wrap, width);
503 memcpy(ptr, src1 + y1 * src_wrap, width);
507 deinterlace_line(dst, buf, width, width);
510 src += (2 + 1) * src_wrap;
516 /* deinterlace, return -1 if format not handled */
517 int avpicture_deinterlace(AVPicture *dst, AVPicture *src,
518 int pix_fmt, int width, int height)
522 if (pix_fmt != PIX_FMT_YUV420P &&
523 pix_fmt != PIX_FMT_YUV422P &&
524 pix_fmt != PIX_FMT_YUV444P)
526 if ((width & 1) != 0 || (height & 3) != 0)
532 case PIX_FMT_YUV420P:
536 case PIX_FMT_YUV422P:
543 deinterlace_bottom_field(dst->data[i], dst->linesize[i],
544 src->data[i], src->linesize[i],