00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "avfilter.h"
00028 #include "libavutil/pixdesc.h"
00029 #include "libavutil/colorspace.h"
00030 #include "libavutil/avassert.h"
00031 #include "libavcore/imgutils.h"
00032 #include "libavcore/parseutils.h"
00033
00034 enum { RED = 0, GREEN, BLUE, ALPHA };
00035
00036 static int fill_line_with_color(uint8_t *line[4], int line_step[4], int w, uint8_t color[4],
00037 enum PixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba)
00038 {
00039 uint8_t rgba_map[4] = {0};
00040 int i;
00041 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[pix_fmt];
00042 int hsub = pix_desc->log2_chroma_w;
00043
00044 *is_packed_rgba = 1;
00045 switch (pix_fmt) {
00046 case PIX_FMT_ARGB: rgba_map[ALPHA] = 0; rgba_map[RED ] = 1; rgba_map[GREEN] = 2; rgba_map[BLUE ] = 3; break;
00047 case PIX_FMT_ABGR: rgba_map[ALPHA] = 0; rgba_map[BLUE ] = 1; rgba_map[GREEN] = 2; rgba_map[RED ] = 3; break;
00048 case PIX_FMT_RGBA:
00049 case PIX_FMT_RGB24: rgba_map[RED ] = 0; rgba_map[GREEN] = 1; rgba_map[BLUE ] = 2; rgba_map[ALPHA] = 3; break;
00050 case PIX_FMT_BGRA:
00051 case PIX_FMT_BGR24: rgba_map[BLUE ] = 0; rgba_map[GREEN] = 1; rgba_map[RED ] = 2; rgba_map[ALPHA] = 3; break;
00052 default:
00053 *is_packed_rgba = 0;
00054 }
00055
00056 if (*is_packed_rgba) {
00057 line_step[0] = (av_get_bits_per_pixel(pix_desc))>>3;
00058 for (i = 0; i < 4; i++)
00059 color[rgba_map[i]] = rgba_color[i];
00060
00061 line[0] = av_malloc(w * line_step[0]);
00062 for (i = 0; i < w; i++)
00063 memcpy(line[0] + i * line_step[0], color, line_step[0]);
00064 } else {
00065 int plane;
00066
00067 color[RED ] = RGB_TO_Y_CCIR(rgba_color[0], rgba_color[1], rgba_color[2]);
00068 color[GREEN] = RGB_TO_U_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00069 color[BLUE ] = RGB_TO_V_CCIR(rgba_color[0], rgba_color[1], rgba_color[2], 0);
00070 color[ALPHA] = rgba_color[3];
00071
00072 for (plane = 0; plane < 4; plane++) {
00073 int line_size;
00074 int hsub1 = (plane == 1 || plane == 2) ? hsub : 0;
00075
00076 line_step[plane] = 1;
00077 line_size = (w >> hsub1) * line_step[plane];
00078 line[plane] = av_malloc(line_size);
00079 memset(line[plane], color[plane], line_size);
00080 }
00081 }
00082
00083 return 0;
00084 }
00085
00086 static void draw_rectangle(AVFilterBufferRef *outpic, uint8_t *line[4], int line_step[4],
00087 int hsub, int vsub, int x, int y, int w, int h)
00088 {
00089 int i, plane;
00090 uint8_t *p;
00091
00092 for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
00093 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
00094 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
00095
00096 p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
00097 for (i = 0; i < (h >> vsub1); i++) {
00098 memcpy(p + (x >> hsub1) * line_step[plane], line[plane], (w >> hsub1) * line_step[plane]);
00099 p += outpic->linesize[plane];
00100 }
00101 }
00102 }
00103
00104 static void copy_rectangle(AVFilterBufferRef *outpic,uint8_t *line[4], int line_step[4], int linesize[4],
00105 int hsub, int vsub, int x, int y, int y2, int w, int h)
00106 {
00107 int i, plane;
00108 uint8_t *p;
00109
00110 for (plane = 0; plane < 4 && outpic->data[plane]; plane++) {
00111 int hsub1 = plane == 1 || plane == 2 ? hsub : 0;
00112 int vsub1 = plane == 1 || plane == 2 ? vsub : 0;
00113
00114 p = outpic->data[plane] + (y >> vsub1) * outpic->linesize[plane];
00115 for (i = 0; i < (h >> vsub1); i++) {
00116 memcpy(p + (x >> hsub1) * line_step[plane], line[plane] + linesize[plane]*(i+(y2>>vsub1)), (w >> hsub1) * line_step[plane]);
00117 p += outpic->linesize[plane];
00118 }
00119 }
00120 }
00121
00122 static int query_formats(AVFilterContext *ctx)
00123 {
00124 static const enum PixelFormat pix_fmts[] = {
00125 PIX_FMT_ARGB, PIX_FMT_RGBA,
00126 PIX_FMT_ABGR, PIX_FMT_BGRA,
00127 PIX_FMT_RGB24, PIX_FMT_BGR24,
00128
00129 PIX_FMT_YUV444P, PIX_FMT_YUV422P,
00130 PIX_FMT_YUV420P, PIX_FMT_YUV411P,
00131 PIX_FMT_YUV410P, PIX_FMT_YUV440P,
00132 PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P,
00133 PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P,
00134 PIX_FMT_YUVA420P,
00135
00136 PIX_FMT_NONE
00137 };
00138
00139 avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts));
00140 return 0;
00141 }
00142
00143 #if CONFIG_PAD_FILTER
00144
00145 typedef struct {
00146 int w, h;
00147 int x, y;
00148 int in_w, in_h;
00149
00150 uint8_t color[4];
00151 uint8_t *line[4];
00152 int line_step[4];
00153 int hsub, vsub;
00154 int needs_copy;
00155 } PadContext;
00156
00157 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00158 {
00159 PadContext *pad = ctx->priv;
00160 char color_string[128] = "black";
00161
00162 if (args)
00163 sscanf(args, "%d:%d:%d:%d:%s", &pad->w, &pad->h, &pad->x, &pad->y, color_string);
00164
00165 if (av_parse_color(pad->color, color_string, -1, ctx) < 0)
00166 return AVERROR(EINVAL);
00167
00168
00169 if (pad->w < 0 || pad->h < 0) {
00170 av_log(ctx, AV_LOG_ERROR, "Negative size values are not acceptable.\n");
00171 return AVERROR(EINVAL);
00172 }
00173
00174 return 0;
00175 }
00176
00177 static av_cold void uninit(AVFilterContext *ctx)
00178 {
00179 PadContext *pad = ctx->priv;
00180 int i;
00181
00182 for (i = 0; i < 4; i++) {
00183 av_freep(&pad->line[i]);
00184 pad->line_step[i] = 0;
00185 }
00186 }
00187
00188 static int config_input(AVFilterLink *inlink)
00189 {
00190 AVFilterContext *ctx = inlink->dst;
00191 PadContext *pad = ctx->priv;
00192 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00193 uint8_t rgba_color[4];
00194 int is_packed_rgba;
00195
00196 pad->hsub = pix_desc->log2_chroma_w;
00197 pad->vsub = pix_desc->log2_chroma_h;
00198
00199 if (!pad->w)
00200 pad->w = inlink->w;
00201 if (!pad->h)
00202 pad->h = inlink->h;
00203
00204 pad->w &= ~((1 << pad->hsub) - 1);
00205 pad->h &= ~((1 << pad->vsub) - 1);
00206 pad->x &= ~((1 << pad->hsub) - 1);
00207 pad->y &= ~((1 << pad->vsub) - 1);
00208
00209 pad->in_w = inlink->w & ~((1 << pad->hsub) - 1);
00210 pad->in_h = inlink->h & ~((1 << pad->vsub) - 1);
00211
00212 memcpy(rgba_color, pad->color, sizeof(rgba_color));
00213 fill_line_with_color(pad->line, pad->line_step, pad->w, pad->color,
00214 inlink->format, rgba_color, &is_packed_rgba);
00215
00216 av_log(ctx, AV_LOG_INFO, "w:%d h:%d -> w:%d h:%d x:%d y:%d color:0x%02X%02X%02X%02X[%s]\n",
00217 inlink->w, inlink->h, pad->w, pad->h, pad->x, pad->y,
00218 pad->color[0], pad->color[1], pad->color[2], pad->color[3],
00219 is_packed_rgba ? "rgba" : "yuva");
00220
00221 if (pad->x < 0 || pad->y < 0 ||
00222 pad->w <= 0 || pad->h <= 0 ||
00223 (unsigned)pad->x + (unsigned)inlink->w > pad->w ||
00224 (unsigned)pad->y + (unsigned)inlink->h > pad->h) {
00225 av_log(ctx, AV_LOG_ERROR,
00226 "Input area %d:%d:%d:%d not within the padded area 0:0:%d:%d or zero-sized\n",
00227 pad->x, pad->y, pad->x + inlink->w, pad->y + inlink->h, pad->w, pad->h);
00228 return AVERROR(EINVAL);
00229 }
00230
00231 return 0;
00232 }
00233
00234 static int config_output(AVFilterLink *outlink)
00235 {
00236 PadContext *pad = outlink->src->priv;
00237
00238 outlink->w = pad->w;
00239 outlink->h = pad->h;
00240 return 0;
00241 }
00242
00243 static AVFilterBufferRef *get_video_buffer(AVFilterLink *inlink, int perms, int w, int h)
00244 {
00245 PadContext *pad = inlink->dst->priv;
00246
00247 AVFilterBufferRef *picref = avfilter_get_video_buffer(inlink->dst->outputs[0], perms,
00248 w + (pad->w - pad->in_w),
00249 h + (pad->h - pad->in_h));
00250 int plane;
00251
00252 picref->video->w = w;
00253 picref->video->h = h;
00254
00255 for (plane = 0; plane < 4 && picref->data[plane]; plane++) {
00256 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
00257 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
00258
00259 picref->data[plane] += (pad->x >> hsub) * pad->line_step[plane] +
00260 (pad->y >> vsub) * picref->linesize[plane];
00261 }
00262
00263 return picref;
00264 }
00265
00266 static int does_clip(PadContext *pad, AVFilterBufferRef *outpicref, int plane, int hsub, int vsub, int x, int y)
00267 {
00268 int64_t x_in_buf, y_in_buf;
00269
00270 x_in_buf = outpicref->data[plane] - outpicref->buf->data[plane]
00271 + (x >> hsub) * pad ->line_step[plane]
00272 + (y >> vsub) * outpicref->linesize [plane];
00273
00274 if(x_in_buf < 0 || x_in_buf % pad->line_step[plane])
00275 return 1;
00276 x_in_buf /= pad->line_step[plane];
00277
00278 av_assert0(outpicref->buf->linesize[plane]>0);
00279
00280 y_in_buf = x_in_buf / outpicref->buf->linesize[plane];
00281 x_in_buf %= outpicref->buf->linesize[plane];
00282
00283 if( y_in_buf<<vsub >= outpicref->buf->h
00284 || x_in_buf<<hsub >= outpicref->buf->w)
00285 return 1;
00286 return 0;
00287 }
00288
00289 static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
00290 {
00291 PadContext *pad = inlink->dst->priv;
00292 AVFilterBufferRef *outpicref = avfilter_ref_buffer(inpicref, ~0);
00293 int plane;
00294
00295 for (plane = 0; plane < 4 && outpicref->data[plane]; plane++) {
00296 int hsub = (plane == 1 || plane == 2) ? pad->hsub : 0;
00297 int vsub = (plane == 1 || plane == 2) ? pad->vsub : 0;
00298
00299 av_assert0(outpicref->buf->w>0 && outpicref->buf->h>0);
00300
00301 if(outpicref->format != outpicref->buf->format)
00302 break;
00303
00304 outpicref->data[plane] -= (pad->x >> hsub) * pad ->line_step[plane]
00305 + (pad->y >> vsub) * outpicref->linesize [plane];
00306
00307 if( does_clip(pad, outpicref, plane, hsub, vsub, 0, 0)
00308 || does_clip(pad, outpicref, plane, hsub, vsub, 0, pad->h-1)
00309 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, 0)
00310 || does_clip(pad, outpicref, plane, hsub, vsub, pad->w-1, pad->h-1)
00311 )
00312 break;
00313 }
00314 pad->needs_copy= plane < 4 && outpicref->data[plane];
00315 if(pad->needs_copy){
00316 av_log(inlink->dst, AV_LOG_DEBUG, "Direct padding impossible allocating new frame\n");
00317 avfilter_unref_buffer(outpicref);
00318 outpicref = avfilter_get_video_buffer(inlink->dst->outputs[0], AV_PERM_WRITE | AV_PERM_NEG_LINESIZES,
00319 FFMAX(inlink->w, pad->w),
00320 FFMAX(inlink->h, pad->h));
00321 avfilter_copy_buffer_ref_props(outpicref, inpicref);
00322 }
00323
00324 inlink->dst->outputs[0]->out_buf = outpicref;
00325
00326 outpicref->video->w = pad->w;
00327 outpicref->video->h = pad->h;
00328
00329 avfilter_start_frame(inlink->dst->outputs[0], outpicref);
00330 }
00331
00332 static void end_frame(AVFilterLink *link)
00333 {
00334 avfilter_end_frame(link->dst->outputs[0]);
00335 avfilter_unref_buffer(link->cur_buf);
00336 }
00337
00338 static void draw_send_bar_slice(AVFilterLink *link, int y, int h, int slice_dir, int before_slice)
00339 {
00340 PadContext *pad = link->dst->priv;
00341 int bar_y, bar_h = 0;
00342
00343 if (slice_dir * before_slice == 1 && y == pad->y) {
00344
00345 bar_y = 0;
00346 bar_h = pad->y;
00347 } else if (slice_dir * before_slice == -1 && (y + h) == (pad->y + pad->in_h)) {
00348
00349 bar_y = pad->y + pad->in_h;
00350 bar_h = pad->h - pad->in_h - pad->y;
00351 }
00352
00353 if (bar_h) {
00354 draw_rectangle(link->dst->outputs[0]->out_buf,
00355 pad->line, pad->line_step, pad->hsub, pad->vsub,
00356 0, bar_y, pad->w, bar_h);
00357 avfilter_draw_slice(link->dst->outputs[0], bar_y, bar_h, slice_dir);
00358 }
00359 }
00360
00361 static void draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
00362 {
00363 PadContext *pad = link->dst->priv;
00364 AVFilterBufferRef *outpic = link->dst->outputs[0]->out_buf;
00365 AVFilterBufferRef *inpic = link->cur_buf;
00366
00367 y += pad->y;
00368
00369 y &= ~((1 << pad->vsub) - 1);
00370 h &= ~((1 << pad->vsub) - 1);
00371
00372 if (!h)
00373 return;
00374 draw_send_bar_slice(link, y, h, slice_dir, 1);
00375
00376
00377 draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
00378 0, y, pad->x, h);
00379
00380 if(pad->needs_copy){
00381 copy_rectangle(outpic,
00382 inpic->data, pad->line_step, inpic->linesize, pad->hsub, pad->vsub,
00383 pad->x, y, y-pad->y, inpic->video->w, h);
00384 }
00385
00386
00387 draw_rectangle(outpic, pad->line, pad->line_step, pad->hsub, pad->vsub,
00388 pad->x + pad->in_w, y, pad->w - pad->x - pad->in_w, h);
00389 avfilter_draw_slice(link->dst->outputs[0], y, h, slice_dir);
00390
00391 draw_send_bar_slice(link, y, h, slice_dir, -1);
00392 }
00393
00394 AVFilter avfilter_vf_pad = {
00395 .name = "pad",
00396 .description = NULL_IF_CONFIG_SMALL("Pad input image to width:height[:x:y[:color]] (default x and y: 0, default color: black)."),
00397
00398 .priv_size = sizeof(PadContext),
00399 .init = init,
00400 .uninit = uninit,
00401 .query_formats = query_formats,
00402
00403 .inputs = (AVFilterPad[]) {{ .name = "default",
00404 .type = AVMEDIA_TYPE_VIDEO,
00405 .config_props = config_input,
00406 .get_video_buffer = get_video_buffer,
00407 .start_frame = start_frame,
00408 .draw_slice = draw_slice,
00409 .end_frame = end_frame, },
00410 { .name = NULL}},
00411
00412 .outputs = (AVFilterPad[]) {{ .name = "default",
00413 .type = AVMEDIA_TYPE_VIDEO,
00414 .config_props = config_output, },
00415 { .name = NULL}},
00416 };
00417
00418 #endif
00419
00420 #if CONFIG_COLOR_FILTER
00421
00422 typedef struct {
00423 int w, h;
00424 uint8_t color[4];
00425 AVRational time_base;
00426 uint8_t *line[4];
00427 int line_step[4];
00428 int hsub, vsub;
00429 uint64_t pts;
00430 } ColorContext;
00431
00432 static av_cold int color_init(AVFilterContext *ctx, const char *args, void *opaque)
00433 {
00434 ColorContext *color = ctx->priv;
00435 char color_string[128] = "black";
00436 char frame_size [128] = "320x240";
00437 char frame_rate [128] = "25";
00438 AVRational frame_rate_q;
00439 int ret;
00440
00441 if (args)
00442 sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate);
00443
00444 if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) {
00445 av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size);
00446 return AVERROR(EINVAL);
00447 }
00448
00449 if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 ||
00450 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
00451 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate);
00452 return AVERROR(EINVAL);
00453 }
00454 color->time_base.num = frame_rate_q.den;
00455 color->time_base.den = frame_rate_q.num;
00456
00457 if ((ret = av_parse_color(color->color, color_string, -1, ctx)) < 0)
00458 return ret;
00459
00460 return 0;
00461 }
00462
00463 static av_cold void color_uninit(AVFilterContext *ctx)
00464 {
00465 ColorContext *color = ctx->priv;
00466 int i;
00467
00468 for (i = 0; i < 4; i++) {
00469 av_freep(&color->line[i]);
00470 color->line_step[i] = 0;
00471 }
00472 }
00473
00474 static int color_config_props(AVFilterLink *inlink)
00475 {
00476 AVFilterContext *ctx = inlink->src;
00477 ColorContext *color = ctx->priv;
00478 uint8_t rgba_color[4];
00479 int is_packed_rgba;
00480 const AVPixFmtDescriptor *pix_desc = &av_pix_fmt_descriptors[inlink->format];
00481
00482 color->hsub = pix_desc->log2_chroma_w;
00483 color->vsub = pix_desc->log2_chroma_h;
00484
00485 color->w &= ~((1 << color->hsub) - 1);
00486 color->h &= ~((1 << color->vsub) - 1);
00487 if (av_image_check_size(color->w, color->h, 0, ctx) < 0)
00488 return AVERROR(EINVAL);
00489
00490 memcpy(rgba_color, color->color, sizeof(rgba_color));
00491 fill_line_with_color(color->line, color->line_step, color->w, color->color,
00492 inlink->format, rgba_color, &is_packed_rgba);
00493
00494 av_log(ctx, AV_LOG_INFO, "w:%d h:%d r:%d/%d color:0x%02x%02x%02x%02x[%s]\n",
00495 color->w, color->h, color->time_base.den, color->time_base.num,
00496 color->color[0], color->color[1], color->color[2], color->color[3],
00497 is_packed_rgba ? "rgba" : "yuva");
00498 inlink->w = color->w;
00499 inlink->h = color->h;
00500
00501 return 0;
00502 }
00503
00504 static int color_request_frame(AVFilterLink *link)
00505 {
00506 ColorContext *color = link->src->priv;
00507 AVFilterBufferRef *picref = avfilter_get_video_buffer(link, AV_PERM_WRITE, color->w, color->h);
00508 picref->video->pixel_aspect = (AVRational) {1, 1};
00509 picref->pts = av_rescale_q(color->pts++, color->time_base, AV_TIME_BASE_Q);
00510 picref->pos = 0;
00511
00512 avfilter_start_frame(link, avfilter_ref_buffer(picref, ~0));
00513 draw_rectangle(picref,
00514 color->line, color->line_step, color->hsub, color->vsub,
00515 0, 0, color->w, color->h);
00516 avfilter_draw_slice(link, 0, color->h, 1);
00517 avfilter_end_frame(link);
00518 avfilter_unref_buffer(picref);
00519
00520 return 0;
00521 }
00522
00523 AVFilter avfilter_vsrc_color = {
00524 .name = "color",
00525 .description = NULL_IF_CONFIG_SMALL("Provide an uniformly colored input, syntax is: [color[:size[:rate]]]"),
00526
00527 .priv_size = sizeof(ColorContext),
00528 .init = color_init,
00529 .uninit = color_uninit,
00530
00531 .query_formats = query_formats,
00532
00533 .inputs = (AVFilterPad[]) {{ .name = NULL}},
00534
00535 .outputs = (AVFilterPad[]) {{ .name = "default",
00536 .type = AVMEDIA_TYPE_VIDEO,
00537 .request_frame = color_request_frame,
00538 .config_props = color_config_props },
00539 { .name = NULL}},
00540 };
00541
00542 #endif