[FFmpeg-devel] [misc-filters PATCH 5/5] Factorize common code shared between pad and color source. Implement fill_line_with_color().
Stefano Sabatini
stefano.sabatini-lala
Wed Jun 30 01:42:37 CEST 2010
---
libavfilter/vf_pad.c | 164 +++++++++++++++++++-------------------------------
1 files changed, 61 insertions(+), 103 deletions(-)
diff --git a/libavfilter/vf_pad.c b/libavfilter/vf_pad.c
index b417aef..7aa82aa 100644
--- a/libavfilter/vf_pad.c
+++ b/libavfilter/vf_pad.c
@@ -29,6 +29,57 @@
#include "libavutil/pixdesc.h"
#include "libavutil/colorspace.h"
+enum { RED = 0, GREEN, BLUE, ALPHA };
+
+static int fill_line_with_color(uint8_t *line[4], int line_step[4], int w, uint8_t color[4],
+ enum PixelFormat pix_fmt, uint8_t rgba_color[4])
+{
+ uint8_t rgba_map[4] = {0};
+ int i, is_packed_rgb = 1;
+ const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
+ int hsub = pix_desc->log2_chroma_w;
+
+ switch (pix_fmt) {
+ case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
+ case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
+ case PIX_FMT_RGBA:
+ case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
+ case PIX_FMT_BGRA:
+ case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
+ default:
+ is_packed_rgb = 0;
+ }
+
+ if (is_packed_rgb) {
+ line_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
+ for (i = 0; i < 4; i++)
+ color[rgba_map[i]] = rgba_color[i];
+
+ line[0] = av_malloc(w * line_step[0]);
+ for (i = 0; i < w; i++)
+ memcpy(line[0] + i * line_step[0], color, line_step[0]);
+ } else {
+ int plane;
+
+ color[RED ] = RGB_TO_Y(rgba_color[0], rgba_color[1], rgba_color[2]);
+ color[GREEN] = RGB_TO_U(rgba_color[0], rgba_color[1], rgba_color[2], 0);
+ color[BLUE ] = RGB_TO_V(rgba_color[0], rgba_color[1], rgba_color[2], 0);
+ color[ALPHA] = rgba_color[3];
+
+ for (plane = 0; plane < 4; plane++) {
+ int line_size;
+ int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
+
+ line_step[plane] = 1;
+ line_size = (w >> hsub1) * line_step[plane];
+ line[plane] = av_malloc(line_size);
+ memset(line[plane], color[plane], line_size);
+ }
+ }
+
+ return 0;
+}
+
static void draw_rectangle(AVFilterPicRef *outpic, uint8_t *line[4], int line_step[4],
int hsub, int vsub, int x, int y, int w, int h)
{
@@ -68,8 +119,6 @@ static int query_formats(AVFilterContext *ctx)
return 0;
}
-enum { RED = 0, GREEN, BLUE, ALPHA };
-
#if CONFIG_PAD_FILTER
typedef struct {
@@ -118,29 +167,8 @@ static int config_input(AVFilterLink *inlink)
{
AVFilterContext *ctx = inlink->dst;
PadContext *pad = ctx->priv;
- const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
uint8_t rgba_color[4];
- uint8_t rgba_map[4] = {0};
- int i, is_packed_rgb = 1;
-
- switch (inlink->format) {
- case PIX_FMT_ARGB:
- rgba_map[ALPHA] = 0; rgba_map[RED] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE] = 3;
- break;
- case PIX_FMT_ABGR:
- rgba_map[ALPHA] = 0; rgba_map[BLUE] = 1; rgba_map[GREEN] = 2; rgba_map[RED] = 3;
- break;
- case PIX_FMT_RGBA:
- case PIX_FMT_RGB24:
- rgba_map[RED] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE] = 2; rgba_map[ALPHA] = 3;
- break;
- case PIX_FMT_BGRA:
- case PIX_FMT_BGR24:
- rgba_map[BLUE] = 0; rgba_map[GREEN] = 1; rgba_map[RED] = 2; rgba_map[ALPHA] = 3;
- break;
- default:
- is_packed_rgb = 0;
- }
+ const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
pad->hsub = pix_desc->log2_chroma_w;
pad->vsub = pix_desc->log2_chroma_h;
@@ -159,37 +187,12 @@ static int config_input(AVFilterLink *inlink)
pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
memcpy(rgba_color, pad->color, sizeof(rgba_color));
- if (is_packed_rgb) {
- pad->line_step[0] = (av_get_bits_per_pixel(&av_pix_fmt_descriptors[inlink->format]))>>3;
- for (i = 0; i < 4; i++)
- pad->color[rgba_map[i]] = rgba_color[i];
-
- pad->line[0] = av_malloc(pad->w * pad->line_step[0]);
- for (i = 0; i < pad->w; i++)
- memcpy(pad->line[0] + i * pad->line_step[0], pad->color, pad->line_step[0]);
- } else {
- int plane;
-
- pad->color[0] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
- pad->color[1] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
- pad->color[2] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
- pad->color[3] = rgba_color[3];
+ fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
+ inlink->format, rgba_color);
- for (plane = 0; plane < 4; plane++) {
- int line_size;
- int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
-
- pad->line_step[plane] = 1;
- line_size = (pad->w >> hsub) * pad->line_step[plane];
- pad->line[plane] = av_malloc(line_size);
- memset(pad->line[plane], pad->color[plane], line_size);
- }
- }
-
- av_log(ctx, AV_LOG_INFO, "w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
- pad->w, pad->h, pad->x, pad->y,
- pad->color[0], pad->color[1], pad->color[2], pad->color[3],
- is_packed_rgb ? "rgba" : "yuva");
+ av_log(ctx, AV_LOG_INFO, "w:%d h:%d x:%d y:%d fmt:%s color:0x%02X%02X%02X%02X\n",
+ pad->w, pad->h, pad->x, pad->y, pix_desc->name,
+ pad->color[0], pad->color[1], pad->color[2], pad->color[3]);
if (pad->x < 0 || pad->y < 0 ||
pad->w <= 0 || pad->h <= 0 ||
@@ -391,65 +394,20 @@ static int color_config_props(AVFilterLink *inlink)
AVFilterContext *ctx = inlink->src;
ColorContext *priv = ctx->priv;
uint8_t rgba_color[4];
- uint8_t rgba_map[4];
- int i, is_packed_rgb = 1;
const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
- switch (inlink->format) {
- case PIX_FMT_ARGB:
- rgba_map[ALPHA] = 0; rgba_map[RED] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE] = 3;
- break;
- case PIX_FMT_ABGR:
- rgba_map[ALPHA] = 0; rgba_map[BLUE] = 1; rgba_map[GREEN] = 2; rgba_map[RED] = 3;
- break;
- case PIX_FMT_RGBA:
- case PIX_FMT_RGB24:
- rgba_map[RED] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE] = 2; rgba_map[ALPHA] = 3;
- break;
- case PIX_FMT_BGRA:
- case PIX_FMT_BGR24:
- rgba_map[BLUE] = 0; rgba_map[GREEN] = 1; rgba_map[RED] = 2; rgba_map[ALPHA] = 3;
- break;
- default:
- is_packed_rgb = 0;
- }
-
priv->hsub = pix_desc->log2_chroma_w;
priv->vsub = pix_desc->log2_chroma_h;
- memcpy(rgba_color, priv->color, sizeof(rgba_color));
- if (is_packed_rgb) {
- priv->line_step[0] = (av_get_bits_per_pixel(&av_pix_fmt_descriptors[inlink->format]))>>3;
- for (i = 0; i < 4; i++)
- priv->color[rgba_map[i]] = rgba_color[i];
-
- priv->line[0] = av_malloc(priv->w * priv->line_step[0]);
- for (i = 0; i < priv->w; i++)
- memcpy(priv->line[0] + i * priv->line_step[0], priv->color, priv->line_step[0]);
- } else {
- int plane;
-
- priv->color[RED ] = RGB_TO_Y(rgba_color[0], rgba_color[1], rgba_color[2]);
- priv->color[GREEN] = RGB_TO_U(rgba_color[0], rgba_color[1], rgba_color[2], 0);
- priv->color[BLUE ] = RGB_TO_V(rgba_color[0], rgba_color[1], rgba_color[2], 0);
- priv->color[ALPHA] = rgba_color[3];
-
- for (plane = 0; plane < 4; plane++) {
- int line_size;
- int hsub = (plane == 1 || plane == 2) ? priv->hsub : 0;
-
- priv->line_step[plane] = 1;
- line_size = (priv->w >> hsub) * priv->line_step[plane];
- priv->line[plane] = av_malloc(line_size);
- memset(priv->line[plane], priv->color[plane], line_size);
- }
- }
-
priv->w &= ~((1 << priv->hsub) - 1);
priv->h &= ~((1 << priv->vsub) - 1);
if (avcodec_check_dimensions(ctx, priv->w, priv->h) < 0)
return AVERROR(EINVAL);
+ memcpy(rgba_color, priv->color, sizeof(rgba_color));
+ fill_line_with_color(priv->line, priv->line_step, priv->w, priv->color,
+ inlink->format, rgba_color);
+
av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x\n",
priv->w, priv->h, priv->time_base.den, priv->time_base.num,
priv->color[0], priv->color[1], priv->color[2], priv->color[3]);
--
1.7.1
More information about the ffmpeg-devel
mailing list