📄 imgconvert.c
字号:
FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; b = p[4]; g = p[5]; r = p[6]; r1 += r; g1 += g; b1 += b; lum[1] = (FIX(0.29900) * r + FIX(0.58700) * g + FIX(0.11400) * b + ONE_HALF) >> SCALEBITS; cb[0] = ((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + FIX(0.50000) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128; cr[0] = ((FIX(0.50000) * r1 - FIX(0.41869) * g1 - FIX(0.08131) * b1 + 4 * ONE_HALF - 1) >> (SCALEBITS + 2)) + 128; cb++; cr++; p += -wrap4 + 2 * 4; lum += -wrap + 2; } p += wrap4; lum += wrap; }}/* XXX: use generic filter ? *//* 1x2 -> 1x1 */static void shrink2(UINT8 *dst, int dst_wrap, UINT8 *src, int src_wrap, int width, int height){ int w; UINT8 *s1, *s2, *d; for(;height > 0; height--) { s1 = src; s2 = s1 + src_wrap; d = dst; for(w = width;w >= 4; w-=4) { d[0] = (s1[0] + s2[0]) >> 1; d[1] = (s1[1] + s2[1]) >> 1; d[2] = (s1[2] + s2[2]) >> 1; d[3] = (s1[3] + s2[3]) >> 1; s1 += 4; s2 += 4; d += 4; } for(;w > 0; w--) { d[0] = (s1[0] + s2[0]) >> 1; s1++; s2++; d++; } src += 2 * src_wrap; dst += dst_wrap; }}/* 2x2 -> 1x1 */static void shrink22(UINT8 *dst, int dst_wrap, UINT8 *src, int src_wrap, int width, int height){ int w; UINT8 *s1, *s2, *d; for(;height > 0; height--) { s1 = src; s2 = s1 + src_wrap; d = dst; for(w = width;w >= 4; w-=4) { d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1; d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 1; d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 1; d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 1; s1 += 8; s2 += 8; d += 4; } for(;w > 0; w--) { d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 1; s1 += 2; s2 += 2; d++; } src += 2 * src_wrap; dst += dst_wrap; }}/* 1x1 -> 2x2 */static void grow22(UINT8 *dst, int dst_wrap, UINT8 *src, int src_wrap, int width, int height){ int w; UINT8 *s1, *d; for(;height > 0; height--) { s1 = src; d = dst; for(w = width;w >= 4; w-=4) { d[1] = d[0] = s1[0]; d[3] = d[2] = s1[1]; s1 += 2; d += 4; } for(;w > 0; w--) { d[0] = s1[0]; s1 ++; d++; } if (height%2) src += src_wrap; dst += dst_wrap; }}/* 1x2 -> 2x1. width and height are given for the source picture */static void conv411(UINT8 *dst, int dst_wrap, UINT8 *src, int src_wrap, int width, int height){ int w, c; UINT8 *s1, *s2, *d; for(;height > 0; height -= 2) { s1 = src; s2 = src + src_wrap; d = dst; for(w = width;w > 0; w--) { c = (s1[0] + s2[0]) >> 1; d[0] = c; d[1] = c; s1++; s2++; d += 2; } src += src_wrap * 2; dst += dst_wrap; }}static void img_copy(UINT8 *dst, int dst_wrap, UINT8 *src, int src_wrap, int width, int height){ for(;height > 0; height--) { memcpy(dst, src, width); dst += dst_wrap; src += src_wrap; }}#define SCALE_BITS 10#define C_Y (76309 >> (16 - SCALE_BITS))#define C_RV (117504 >> (16 - SCALE_BITS))#define C_BU (138453 >> (16 - SCALE_BITS))#define C_GU (13954 >> (16 - SCALE_BITS))#define C_GV (34903 >> (16 - SCALE_BITS))#define RGBOUT(r, g, b, y1)\{\ y = (y1 - 16) * C_Y;\ r = cm[(y + r_add) >> SCALE_BITS];\ g = cm[(y + g_add) >> SCALE_BITS];\ b = cm[(y + b_add) >> SCALE_BITS];\}/* XXX: no chroma interpolating is done */static void yuv420p_to_bgra32(AVPicture *dst, AVPicture *src, int width, int height){ UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; int w, y, cb, cr, r_add, g_add, b_add, width2; UINT8 *cm = cropTbl + MAX_NEG_CROP; d = dst->data[0]; y1_ptr = src->data[0]; cb_ptr = src->data[1]; cr_ptr = src->data[2]; width2 = width >> 1; for(;height > 0; height -= 2) { d1 = d; d2 = d + dst->linesize[0]; y2_ptr = y1_ptr + src->linesize[0]; for(w = width2; w > 0; w --) { cb = cb_ptr[0] - 128; cr = cr_ptr[0] - 128; r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); /* output 4 pixels */ RGBOUT(d1[2], d1[1], d1[0], y1_ptr[0]); RGBOUT(d1[6], d1[5], d1[4], y1_ptr[1]); RGBOUT(d2[2], d2[1], d2[0], y2_ptr[0]); RGBOUT(d2[6], d2[5], d2[4], y2_ptr[1]); d1[3] = d1[7] = d2[3] = d2[7] = 255; d1 += 8; d2 += 8; y1_ptr += 2; y2_ptr += 2; cb_ptr++; cr_ptr++; } d += 2 * dst->linesize[0]; y1_ptr += 2 * src->linesize[0] - width; cb_ptr += src->linesize[1] - width2; cr_ptr += src->linesize[2] - width2; }}/* XXX: no chroma interpolating is done */static void yuv420p_to_rgba32(AVPicture *dst, AVPicture *src, int width, int height){ UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; int w, y, cb, cr, r_add, g_add, b_add, width2; UINT8 *cm = cropTbl + MAX_NEG_CROP; d = dst->data[0]; y1_ptr = src->data[0]; cb_ptr = src->data[1]; cr_ptr = src->data[2]; width2 = width >> 1; for(;height > 0; height -= 2) { d1 = d; d2 = d + dst->linesize[0]; y2_ptr = y1_ptr + src->linesize[0]; for(w = width2; w > 0; w --) { cb = cb_ptr[0] - 128; cr = cr_ptr[0] - 128; r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); /* output 4 pixels */ RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]); RGBOUT(d1[4], d1[5], d1[6], y1_ptr[1]); RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]); RGBOUT(d2[4], d2[5], d2[6], y2_ptr[1]); d1[3] = d1[7] = d2[3] = d2[7] = 255; d1 += 8; d2 += 8; y1_ptr += 2; y2_ptr += 2; cb_ptr++; cr_ptr++; } d += 2 * dst->linesize[0]; y1_ptr += 2 * src->linesize[0] - width; cb_ptr += src->linesize[1] - width2; cr_ptr += src->linesize[2] - width2; }}/* XXX: no chroma interpolating is done */static void yuv420p_to_rgb24(AVPicture *dst, AVPicture *src, int width, int height){ UINT8 *y1_ptr, *y2_ptr, *cb_ptr, *cr_ptr, *d, *d1, *d2; int w, y, cb, cr, r_add, g_add, b_add, width2; UINT8 *cm = cropTbl + MAX_NEG_CROP; d = dst->data[0]; y1_ptr = src->data[0]; cb_ptr = src->data[1]; cr_ptr = src->data[2]; width2 = width >> 1; for(;height > 0; height -= 2) { d1 = d; d2 = d + dst->linesize[0]; y2_ptr = y1_ptr + src->linesize[0]; for(w = width2; w > 0; w --) { cb = cb_ptr[0] - 128; cr = cr_ptr[0] - 128; r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); /* output 4 pixels */ RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]); RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]); RGBOUT(d2[0], d2[1], d2[2], y2_ptr[0]); RGBOUT(d2[3], d2[4], d2[5], y2_ptr[1]); d1 += 6; d2 += 6; y1_ptr += 2; y2_ptr += 2; cb_ptr++; cr_ptr++; } d += 2 * dst->linesize[0]; y1_ptr += 2 * src->linesize[0] - width; cb_ptr += src->linesize[1] - width2; cr_ptr += src->linesize[2] - width2; }}/* XXX: no chroma interpolating is done */static void yuv422p_to_rgb24(AVPicture *dst, AVPicture *src, int width, int height){ UINT8 *y1_ptr, *cb_ptr, *cr_ptr, *d, *d1; int w, y, cb, cr, r_add, g_add, b_add, width2; UINT8 *cm = cropTbl + MAX_NEG_CROP; d = dst->data[0]; y1_ptr = src->data[0]; cb_ptr = src->data[1]; cr_ptr = src->data[2]; width2 = width >> 1; for(;height > 0; height --) { d1 = d; for(w = width2; w > 0; w --) { cb = cb_ptr[0] - 128; cr = cr_ptr[0] - 128; r_add = C_RV * cr + (1 << (SCALE_BITS - 1)); g_add = - C_GU * cb - C_GV * cr + (1 << (SCALE_BITS - 1)); b_add = C_BU * cb + (1 << (SCALE_BITS - 1)); /* output 2 pixels */ RGBOUT(d1[0], d1[1], d1[2], y1_ptr[0]); RGBOUT(d1[3], d1[4], d1[5], y1_ptr[1]); d1 += 6; y1_ptr += 2; cb_ptr++; cr_ptr++; } d += dst->linesize[0]; y1_ptr += src->linesize[0] - width; cb_ptr += src->linesize[1] - width2; cr_ptr += src->linesize[2] - width2; }}/* XXX: always use linesize. Return -1 if not supported */int img_convert(AVPicture *dst, int dst_pix_fmt, AVPicture *src, int pix_fmt, int width, int height){ int i; assert(pix_fmt != PIX_FMT_ANY && dst_pix_fmt != PIX_FMT_ANY); if (dst_pix_fmt == pix_fmt) { switch(pix_fmt) { case PIX_FMT_YUV420P: for(i=0;i<3;i++) { if (i == 1) { width >>= 1; height >>= 1; } img_copy(dst->data[i], dst->linesize[i], src->data[i], src->linesize[i], width, height); } break; default: return -1; } } else if (dst_pix_fmt == PIX_FMT_YUV420P) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -