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