00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <string.h>
00023
00024 #include "libavutil/avutil.h"
00025 #include "libavutil/colorspace.h"
00026 #include "libavutil/mem.h"
00027 #include "libavutil/pixdesc.h"
00028 #include "drawutils.h"
00029 #include "formats.h"
00030
00031 enum { RED = 0, GREEN, BLUE, ALPHA };
00032
00033 int ff_fill_rgba_map(uint8_t *rgba_map, enum PixelFormat pix_fmt)
00034 {
00035 switch (pix_fmt) {
00036 case PIX_FMT_0RGB:
00037 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
00038 case PIX_FMT_0BGR:
00039 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
00040 case PIX_FMT_RGB0:
00041 case PIX_FMT_RGBA:
00042 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
00043 case PIX_FMT_BGRA:
00044 case PIX_FMT_BGR0:
00045 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
00046 default:
00047 return AVERROR(EINVAL);
00048 }
00049 return 0;
00050 }
00051
00052 int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4],
00053 enum PixelFormat pix_fmt, uint8_t rgba_color[4],
00054 int *is_packed_rgba, uint8_t rgba_map_ptr[4])
00055 {
00056 uint8_t rgba_map[4] = {0};
00057 int i;
00058 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
00059 int hsub = pix_desc->log2_chroma_w;
00060
00061 *is_packed_rgba = ff_fill_rgba_map(rgba_map, pix_fmt) >= 0;
00062
00063 if (*is_packed_rgba) {
00064 pixel_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
00065 for (i = 0; i < 4; i++)
00066 dst_color[rgba_map[i]] = rgba_color[i];
00067
00068 line[0] = av_malloc(w * pixel_step[0]);
00069 for (i = 0; i < w; i++)
00070 memcpy(line[0] + i * pixel_step[0], dst_color, pixel_step[0]);
00071 if (rgba_map_ptr)
00072 memcpy(rgba_map_ptr, rgba_map, sizeof(rgba_map[0]) * 4);
00073 } else {
00074 int plane;
00075
00076 dst_color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
00077 dst_color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00078 dst_color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00079 dst_color[3] = rgba_color[3];
00080
00081 for (plane = 0; plane < 4; plane++) {
00082 int line_size;
00083 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
00084
00085 pixel_step[plane] = 1;
00086 line_size = (w >> hsub1) * pixel_step[plane];
00087 line[plane] = av_malloc(line_size);
00088 memset(line[plane], dst_color[plane], line_size);
00089 }
00090 }
00091
00092 return 0;
00093 }
00094
00095 void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4],
00096 uint8_t *src[4], int pixelstep[4],
00097 int hsub, int vsub, int x, int y, int w, int h)
00098 {
00099 int i, plane;
00100 uint8_t *p;
00101
00102 for (plane = 0; plane < 4 && dst[plane]; plane++) {
00103 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
00104 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
00105
00106 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
00107 for (i = 0; i < (h >> vsub1); i++) {
00108 memcpy(p + (x >> hsub1) * pixelstep[plane],
00109 src[plane], (w >> hsub1) * pixelstep[plane]);
00110 p += dst_linesize[plane];
00111 }
00112 }
00113 }
00114
00115 void ff_copy_rectangle(uint8_t *dst[4], int dst_linesize[4],
00116 uint8_t *src[4], int src_linesize[4], int pixelstep[4],
00117 int hsub, int vsub, int x, int y, int y2, int w, int h)
00118 {
00119 int i, plane;
00120 uint8_t *p;
00121
00122 for (plane = 0; plane < 4 && dst[plane]; plane++) {
00123 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
00124 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
00125
00126 p = dst[plane] + (y >> vsub1) * dst_linesize[plane];
00127 for (i = 0; i < (h >> vsub1); i++) {
00128 memcpy(p + (x >> hsub1) * pixelstep[plane],
00129 src[plane] + src_linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * pixelstep[plane]);
00130 p += dst_linesize[plane];
00131 }
00132 }
00133 }
00134
00135 int ff_draw_init(FFDrawContext *draw, enum PixelFormat format, unsigned flags)
00136 {
00137 const AVPixFmtDescriptor *desc = &av_pix_fmt_descriptors[format];
00138 const AVComponentDescriptor *c;
00139 unsigned i, nb_planes = 0;
00140 int pixelstep[MAX_PLANES] = { 0 };
00141
00142 if (!desc->name)
00143 return AVERROR(EINVAL);
00144 if (desc->flags & ~(PIX_FMT_PLANAR | PIX_FMT_RGB | PIX_FMT_PSEUDOPAL))
00145 return AVERROR(ENOSYS);
00146 for (i = 0; i < desc->nb_components; i++) {
00147 c = &desc->comp[i];
00148
00149 if (c->depth_minus1 != 8 - 1)
00150 return AVERROR(ENOSYS);
00151 if (c->plane >= MAX_PLANES)
00152 return AVERROR(ENOSYS);
00153
00154 if (pixelstep[c->plane] != 0 &&
00155 pixelstep[c->plane] != c->step_minus1 + 1)
00156 return AVERROR(ENOSYS);
00157 pixelstep[c->plane] = c->step_minus1 + 1;
00158 if (pixelstep[c->plane] >= 8)
00159 return AVERROR(ENOSYS);
00160 nb_planes = FFMAX(nb_planes, c->plane + 1);
00161 }
00162 if ((desc->log2_chroma_w || desc->log2_chroma_h) && nb_planes < 3)
00163 return AVERROR(ENOSYS);
00164 memset(draw, 0, sizeof(*draw));
00165 draw->desc = desc;
00166 draw->format = format;
00167 draw->nb_planes = nb_planes;
00168 memcpy(draw->pixelstep, pixelstep, sizeof(draw->pixelstep));
00169 if (nb_planes >= 3 && !(desc->flags & PIX_FMT_RGB)) {
00170 draw->hsub[1] = draw->hsub[2] = draw->hsub_max = desc->log2_chroma_w;
00171 draw->vsub[1] = draw->vsub[2] = draw->vsub_max = desc->log2_chroma_h;
00172 }
00173 for (i = 0; i < ((desc->nb_components - 1) | 1); i++)
00174 draw->comp_mask[desc->comp[i].plane] |=
00175 1 << (desc->comp[i].offset_plus1 - 1);
00176 return 0;
00177 }
00178
00179 void ff_draw_color(FFDrawContext *draw, FFDrawColor *color, const uint8_t rgba[4])
00180 {
00181 unsigned i;
00182 uint8_t rgba_map[4];
00183
00184 if (rgba != color->rgba)
00185 memcpy(color->rgba, rgba, sizeof(color->rgba));
00186 if ((draw->desc->flags & PIX_FMT_RGB) && draw->nb_planes == 1 &&
00187 ff_fill_rgba_map(rgba_map, draw->format) >= 0) {
00188 for (i = 0; i < 4; i++)
00189 color->comp[0].u8[rgba_map[i]] = rgba[i];
00190 } else if (draw->nb_planes == 3 || draw->nb_planes == 4) {
00191
00192 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
00193 color->comp[1].u8[0] = RGB_TO_U_CCIR(rgba[0], rgba[1], rgba[2], 0);
00194 color->comp[2].u8[0] = RGB_TO_V_CCIR(rgba[0], rgba[1], rgba[2], 0);
00195 color->comp[3].u8[0] = rgba[3];
00196 } else if (draw->format == PIX_FMT_GRAY8 || draw->format == PIX_FMT_GRAY8A) {
00197 color->comp[0].u8[0] = RGB_TO_Y_CCIR(rgba[0], rgba[1], rgba[2]);
00198 color->comp[1].u8[0] = rgba[3];
00199 } else {
00200 av_log(NULL, AV_LOG_WARNING,
00201 "Color conversion not implemented for %s\n", draw->desc->name);
00202 memset(color, 128, sizeof(*color));
00203 }
00204 }
00205
00206 static uint8_t *pointer_at(FFDrawContext *draw, uint8_t *data[], int linesize[],
00207 int plane, int x, int y)
00208 {
00209 return data[plane] +
00210 (y >> draw->vsub[plane]) * linesize[plane] +
00211 (x >> draw->hsub[plane]) * draw->pixelstep[plane];
00212 }
00213
00214 void ff_copy_rectangle2(FFDrawContext *draw,
00215 uint8_t *dst[], int dst_linesize[],
00216 uint8_t *src[], int src_linesize[],
00217 int dst_x, int dst_y, int src_x, int src_y,
00218 int w, int h)
00219 {
00220 int plane, y, wp, hp;
00221 uint8_t *p, *q;
00222
00223 for (plane = 0; plane < draw->nb_planes; plane++) {
00224 p = pointer_at(draw, src, src_linesize, plane, src_x, src_y);
00225 q = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
00226 wp = (w >> draw->hsub[plane]) * draw->pixelstep[plane];
00227 hp = (h >> draw->vsub[plane]);
00228 for (y = 0; y < hp; y++) {
00229 memcpy(q, p, wp);
00230 p += src_linesize[plane];
00231 q += dst_linesize[plane];
00232 }
00233 }
00234 }
00235
00236 void ff_fill_rectangle(FFDrawContext *draw, FFDrawColor *color,
00237 uint8_t *dst[], int dst_linesize[],
00238 int dst_x, int dst_y, int w, int h)
00239 {
00240 int plane, x, y, wp, hp;
00241 uint8_t *p0, *p;
00242
00243 for (plane = 0; plane < draw->nb_planes; plane++) {
00244 p0 = pointer_at(draw, dst, dst_linesize, plane, dst_x, dst_y);
00245 wp = (w >> draw->hsub[plane]);
00246 hp = (h >> draw->vsub[plane]);
00247 if (!hp)
00248 return;
00249 p = p0;
00250
00251 for (x = 0; x < wp; x++) {
00252 memcpy(p, color->comp[plane].u8, draw->pixelstep[plane]);
00253 p += draw->pixelstep[plane];
00254 }
00255 wp *= draw->pixelstep[plane];
00256
00257 p = p0 + dst_linesize[plane];
00258 for (y = 1; y < hp; y++) {
00259 memcpy(p, p0, wp);
00260 p += dst_linesize[plane];
00261 }
00262 }
00263 }
00264
00270 static void clip_interval(int wmax, int *x, int *w, int *dx)
00271 {
00272 if (dx)
00273 *dx = 0;
00274 if (*x < 0) {
00275 if (dx)
00276 *dx = -*x;
00277 *w += *x;
00278 *x = 0;
00279 }
00280 if (*x + *w > wmax)
00281 *w = wmax - *x;
00282 }
00283
00289 static void subsampling_bounds(int sub, int *x, int *w, int *start, int *end)
00290 {
00291 int mask = (1 << sub) - 1;
00292
00293 *start = (-*x) & mask;
00294 *x += *start;
00295 *start = FFMIN(*start, *w);
00296 *w -= *start;
00297 *end = *w & mask;
00298 *w >>= sub;
00299 }
00300
00301 static int component_used(FFDrawContext *draw, int plane, int comp)
00302 {
00303 return (draw->comp_mask[plane] >> comp) & 1;
00304 }
00305
00306
00307
00308
00309 static void blend_line(uint8_t *dst, unsigned src, unsigned alpha,
00310 int dx, int w, unsigned hsub, int left, int right)
00311 {
00312 unsigned asrc = alpha * src;
00313 unsigned tau = 0x1010101 - alpha;
00314 int x;
00315
00316 src *= alpha;
00317 if (left) {
00318 unsigned suba = (left * alpha) >> hsub;
00319 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
00320 dst += dx;
00321 }
00322 for (x = 0; x < w; x++) {
00323 *dst = (*dst * tau + asrc) >> 24;
00324 dst += dx;
00325 }
00326 if (right) {
00327 unsigned suba = (right * alpha) >> hsub;
00328 *dst = (*dst * (0x1010101 - suba) + src * suba) >> 24;
00329 }
00330 }
00331
00332 void ff_blend_rectangle(FFDrawContext *draw, FFDrawColor *color,
00333 uint8_t *dst[], int dst_linesize[],
00334 int dst_w, int dst_h,
00335 int x0, int y0, int w, int h)
00336 {
00337 unsigned alpha, nb_planes, nb_comp, plane, comp;
00338 int w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
00339 uint8_t *p0, *p;
00340
00341
00342 clip_interval(dst_w, &x0, &w, NULL);
00343 clip_interval(dst_h, &y0, &h, NULL);
00344 if (w <= 0 || h <= 0 || !color->rgba[3])
00345 return;
00346
00347 alpha = 0x10203 * color->rgba[3] + 0x2;
00348 nb_planes = (draw->nb_planes - 1) | 1;
00349 for (plane = 0; plane < nb_planes; plane++) {
00350 nb_comp = draw->pixelstep[plane];
00351 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
00352 w_sub = w;
00353 h_sub = h;
00354 x_sub = x0;
00355 y_sub = y0;
00356 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
00357 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
00358 for (comp = 0; comp < nb_comp; comp++) {
00359 if (!component_used(draw, plane, comp))
00360 continue;
00361 p = p0 + comp;
00362 if (top) {
00363 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
00364 draw->pixelstep[plane], w_sub,
00365 draw->hsub[plane], left, right);
00366 p += dst_linesize[plane];
00367 }
00368 for (y = 0; y < h_sub; y++) {
00369 blend_line(p, color->comp[plane].u8[comp], alpha,
00370 draw->pixelstep[plane], w_sub,
00371 draw->hsub[plane], left, right);
00372 p += dst_linesize[plane];
00373 }
00374 if (bottom)
00375 blend_line(p, color->comp[plane].u8[comp], alpha >> 1,
00376 draw->pixelstep[plane], w_sub,
00377 draw->hsub[plane], left, right);
00378 }
00379 }
00380 }
00381
00382 static void blend_pixel(uint8_t *dst, unsigned src, unsigned alpha,
00383 uint8_t *mask, int mask_linesize, int l2depth,
00384 unsigned w, unsigned h, unsigned shift, unsigned xm0)
00385 {
00386 unsigned xm, x, y, t = 0;
00387 unsigned xmshf = 3 - l2depth;
00388 unsigned xmmod = 7 >> l2depth;
00389 unsigned mbits = (1 << (1 << l2depth)) - 1;
00390 unsigned mmult = 255 / mbits;
00391
00392 for (y = 0; y < h; y++) {
00393 xm = xm0;
00394 for (x = 0; x < w; x++) {
00395 t += ((mask[xm >> xmshf] >> ((~xm & xmmod) << l2depth)) & mbits)
00396 * mmult;
00397 xm++;
00398 }
00399 mask += mask_linesize;
00400 }
00401 alpha = (t >> shift) * alpha;
00402 *dst = ((0x1010101 - alpha) * *dst + alpha * src) >> 24;
00403 }
00404
00405 static void blend_line_hv(uint8_t *dst, int dst_delta,
00406 unsigned src, unsigned alpha,
00407 uint8_t *mask, int mask_linesize, int l2depth, int w,
00408 unsigned hsub, unsigned vsub,
00409 int xm, int left, int right, int hband)
00410 {
00411 int x;
00412
00413 if (left) {
00414 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
00415 left, hband, hsub + vsub, xm);
00416 dst += dst_delta;
00417 xm += left;
00418 }
00419 for (x = 0; x < w; x++) {
00420 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
00421 1 << hsub, hband, hsub + vsub, xm);
00422 dst += dst_delta;
00423 xm += 1 << hsub;
00424 }
00425 if (right)
00426 blend_pixel(dst, src, alpha, mask, mask_linesize, l2depth,
00427 right, hband, hsub + vsub, xm);
00428 }
00429
00430 void ff_blend_mask(FFDrawContext *draw, FFDrawColor *color,
00431 uint8_t *dst[], int dst_linesize[], int dst_w, int dst_h,
00432 uint8_t *mask, int mask_linesize, int mask_w, int mask_h,
00433 int l2depth, unsigned endianness, int x0, int y0)
00434 {
00435 unsigned alpha, nb_planes, nb_comp, plane, comp;
00436 int xm0, ym0, w_sub, h_sub, x_sub, y_sub, left, right, top, bottom, y;
00437 uint8_t *p0, *p, *m;
00438
00439 clip_interval(dst_w, &x0, &mask_w, &xm0);
00440 clip_interval(dst_h, &y0, &mask_h, &ym0);
00441 mask += ym0 * mask_linesize;
00442 if (mask_w <= 0 || mask_h <= 0 || !color->rgba[3])
00443 return;
00444
00445
00446 alpha = (0x10307 * color->rgba[3] + 0x3) >> 8;
00447 nb_planes = (draw->nb_planes - 1) | 1;
00448 for (plane = 0; plane < nb_planes; plane++) {
00449 nb_comp = draw->pixelstep[plane];
00450 p0 = pointer_at(draw, dst, dst_linesize, plane, x0, y0);
00451 w_sub = mask_w;
00452 h_sub = mask_h;
00453 x_sub = x0;
00454 y_sub = y0;
00455 subsampling_bounds(draw->hsub[plane], &x_sub, &w_sub, &left, &right);
00456 subsampling_bounds(draw->vsub[plane], &y_sub, &h_sub, &top, &bottom);
00457 for (comp = 0; comp < nb_comp; comp++) {
00458 if (!component_used(draw, plane, comp))
00459 continue;
00460 p = p0 + comp;
00461 m = mask;
00462 if (top) {
00463 blend_line_hv(p, draw->pixelstep[plane],
00464 color->comp[plane].u8[comp], alpha,
00465 m, mask_linesize, l2depth, w_sub,
00466 draw->hsub[plane], draw->vsub[plane],
00467 xm0, left, right, top);
00468 p += dst_linesize[plane];
00469 m += top * mask_linesize;
00470 }
00471 for (y = 0; y < h_sub; y++) {
00472 blend_line_hv(p, draw->pixelstep[plane],
00473 color->comp[plane].u8[comp], alpha,
00474 m, mask_linesize, l2depth, w_sub,
00475 draw->hsub[plane], draw->vsub[plane],
00476 xm0, left, right, 1 << draw->vsub[plane]);
00477 p += dst_linesize[plane];
00478 m += mask_linesize << draw->vsub[plane];
00479 }
00480 if (bottom)
00481 blend_line_hv(p, draw->pixelstep[plane],
00482 color->comp[plane].u8[comp], alpha,
00483 m, mask_linesize, l2depth, w_sub,
00484 draw->hsub[plane], draw->vsub[plane],
00485 xm0, left, right, bottom);
00486 }
00487 }
00488 }
00489
00490 int ff_draw_round_to_sub(FFDrawContext *draw, int sub_dir, int round_dir,
00491 int value)
00492 {
00493 unsigned shift = sub_dir ? draw->vsub_max : draw->hsub_max;
00494
00495 if (!shift)
00496 return value;
00497 if (round_dir >= 0)
00498 value += round_dir ? (1 << shift) - 1 : 1 << (shift - 1);
00499 return (value >> shift) << shift;
00500 }
00501
00502 AVFilterFormats *ff_draw_supported_pixel_formats(unsigned flags)
00503 {
00504 enum PixelFormat i, pix_fmts[PIX_FMT_NB + 1];
00505 unsigned n = 0;
00506 FFDrawContext draw;
00507
00508 for (i = 0; i < PIX_FMT_NB; i++)
00509 if (ff_draw_init(&draw, i, flags) >= 0)
00510 pix_fmts[n++] = i;
00511 pix_fmts[n++] = PIX_FMT_NONE;
00512 return ff_make_format_list(pix_fmts);
00513 }
00514
00515 #ifdef TEST
00516
00517 #undef printf
00518
00519 int main(void)
00520 {
00521 enum PixelFormat f;
00522 const AVPixFmtDescriptor *desc;
00523 FFDrawContext draw;
00524 FFDrawColor color;
00525 int r, i;
00526
00527 for (f = 0; f < PIX_FMT_NB; f++) {
00528 desc = &av_pix_fmt_descriptors[f];
00529 if (!desc->name)
00530 continue;
00531 printf("Testing %s...%*s", desc->name,
00532 (int)(16 - strlen(desc->name)), "");
00533 r = ff_draw_init(&draw, f, 0);
00534 if (r < 0) {
00535 char buf[128];
00536 av_strerror(r, buf, sizeof(buf));
00537 printf("no: %s\n", buf);
00538 continue;
00539 }
00540 ff_draw_color(&draw, &color, (uint8_t[]) { 1, 0, 0, 1 });
00541 for (i = 0; i < sizeof(color); i++)
00542 if (((uint8_t *)&color)[i] != 128)
00543 break;
00544 if (i == sizeof(color)) {
00545 printf("fallback color\n");
00546 continue;
00547 }
00548 printf("ok\n");
00549 }
00550 return 0;
00551 }
00552
00553 #endif