00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00033 #include <float.h>
00034
00035 #include "libavutil/opt.h"
00036 #include "libavutil/intreadwrite.h"
00037 #include "libavutil/parseutils.h"
00038 #include "avfilter.h"
00039
00040 typedef struct {
00041 const AVClass *class;
00042 int w, h;
00043 unsigned int nb_frame;
00044 AVRational time_base;
00045 int64_t pts, max_pts;
00046 char *rate;
00047 char *duration;
00048 AVRational sar;
00049 int nb_decimals;
00050
00051 void (* fill_picture_fn)(AVFilterContext *ctx, AVFilterBufferRef *picref);
00052
00053
00054 int rgba_map[4];
00055 } TestSourceContext;
00056
00057 #define OFFSET(x) offsetof(TestSourceContext, x)
00058
00059 static const AVOption testsrc_options[]= {
00060 { "size", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
00061 { "s", "set video size", OFFSET(w), AV_OPT_TYPE_IMAGE_SIZE, {.str = "320x240"}, 0, 0 },
00062 { "rate", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
00063 { "r", "set video rate", OFFSET(rate), AV_OPT_TYPE_STRING, {.str = "25"}, 0, 0 },
00064 { "duration", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
00065 { "d", "set video duration", OFFSET(duration), AV_OPT_TYPE_STRING, {.str = NULL}, 0, 0 },
00066 { "sar", "set video sample aspect ratio", OFFSET(sar), AV_OPT_TYPE_RATIONAL, {.dbl= 1}, 0, INT_MAX },
00067 { "decimals", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
00068 { "n", "set number of decimals to show", OFFSET(nb_decimals), AV_OPT_TYPE_INT, {.dbl=0}, INT_MIN, INT_MAX },
00069 { NULL },
00070 };
00071
00072 static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
00073 {
00074 TestSourceContext *test = ctx->priv;
00075 AVRational frame_rate_q;
00076 int64_t duration = -1;
00077 int ret = 0;
00078
00079 av_opt_set_defaults(test);
00080
00081 if ((ret = (av_set_options_string(test, args, "=", ":"))) < 0) {
00082 av_log(ctx, AV_LOG_ERROR, "Error parsing options string: '%s'\n", args);
00083 return ret;
00084 }
00085
00086 if ((ret = av_parse_video_rate(&frame_rate_q, test->rate)) < 0 ||
00087 frame_rate_q.den <= 0 || frame_rate_q.num <= 0) {
00088 av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: '%s'\n", test->rate);
00089 return ret;
00090 }
00091 av_freep(&test->rate);
00092
00093 if ((test->duration) && (ret = av_parse_time(&duration, test->duration, 1)) < 0) {
00094 av_log(ctx, AV_LOG_ERROR, "Invalid duration: '%s'\n", test->duration);
00095 return ret;
00096 }
00097 av_freep(&test->duration);
00098
00099 if (test->nb_decimals && strcmp(ctx->filter->name, "testsrc")) {
00100 av_log(ctx, AV_LOG_WARNING,
00101 "Option 'decimals' is ignored with source '%s'\n",
00102 ctx->filter->name);
00103 }
00104
00105 test->time_base.num = frame_rate_q.den;
00106 test->time_base.den = frame_rate_q.num;
00107 test->max_pts = duration >= 0 ?
00108 av_rescale_q(duration, AV_TIME_BASE_Q, test->time_base) : -1;
00109 test->nb_frame = 0;
00110 test->pts = 0;
00111
00112 av_log(ctx, AV_LOG_INFO, "size:%dx%d rate:%d/%d duration:%f sar:%d/%d\n",
00113 test->w, test->h, frame_rate_q.num, frame_rate_q.den,
00114 duration < 0 ? -1 : test->max_pts * av_q2d(test->time_base),
00115 test->sar.num, test->sar.den);
00116 return 0;
00117 }
00118
00119 static int config_props(AVFilterLink *outlink)
00120 {
00121 TestSourceContext *test = outlink->src->priv;
00122
00123 outlink->w = test->w;
00124 outlink->h = test->h;
00125 outlink->sample_aspect_ratio = test->sar;
00126 outlink->time_base = test->time_base;
00127
00128 return 0;
00129 }
00130
00131 static int request_frame(AVFilterLink *outlink)
00132 {
00133 TestSourceContext *test = outlink->src->priv;
00134 AVFilterBufferRef *picref;
00135
00136 if (test->max_pts >= 0 && test->pts >= test->max_pts)
00137 return AVERROR_EOF;
00138 picref = avfilter_get_video_buffer(outlink, AV_PERM_WRITE,
00139 test->w, test->h);
00140 picref->pts = test->pts++;
00141 picref->pos = -1;
00142 picref->video->key_frame = 1;
00143 picref->video->interlaced = 0;
00144 picref->video->pict_type = AV_PICTURE_TYPE_I;
00145 picref->video->sample_aspect_ratio = test->sar;
00146 test->fill_picture_fn(outlink->src, picref);
00147 test->nb_frame++;
00148
00149 avfilter_start_frame(outlink, avfilter_ref_buffer(picref, ~0));
00150 avfilter_draw_slice(outlink, 0, picref->video->h, 1);
00151 avfilter_end_frame(outlink);
00152 avfilter_unref_buffer(picref);
00153
00154 return 0;
00155 }
00156
00157 #if CONFIG_NULLSRC_FILTER
00158
00159 static const char *nullsrc_get_name(void *ctx)
00160 {
00161 return "nullsrc";
00162 }
00163
00164 static const AVClass nullsrc_class = {
00165 .class_name = "NullSourceContext",
00166 .item_name = nullsrc_get_name,
00167 .option = testsrc_options,
00168 };
00169
00170 static void nullsrc_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref) { }
00171
00172 static av_cold int nullsrc_init(AVFilterContext *ctx, const char *args, void *opaque)
00173 {
00174 TestSourceContext *test = ctx->priv;
00175
00176 test->class = &nullsrc_class;
00177 test->fill_picture_fn = nullsrc_fill_picture;
00178 return init(ctx, args, opaque);
00179 }
00180
00181 AVFilter avfilter_vsrc_nullsrc = {
00182 .name = "nullsrc",
00183 .description = NULL_IF_CONFIG_SMALL("Null video source, return unprocessed video frames."),
00184 .init = nullsrc_init,
00185 .priv_size = sizeof(TestSourceContext),
00186
00187 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
00188 .outputs = (const AVFilterPad[]) {{ .name = "default",
00189 .type = AVMEDIA_TYPE_VIDEO,
00190 .request_frame = request_frame,
00191 .config_props = config_props, },
00192 { .name = NULL}},
00193 };
00194
00195 #endif
00196
00197 #if CONFIG_TESTSRC_FILTER
00198
00199 static const char *testsrc_get_name(void *ctx)
00200 {
00201 return "testsrc";
00202 }
00203
00204 static const AVClass testsrc_class = {
00205 .class_name = "TestSourceContext",
00206 .item_name = testsrc_get_name,
00207 .option = testsrc_options,
00208 };
00209
00222 static void draw_rectangle(unsigned val, uint8_t *dst, int dst_linesize, unsigned segment_width,
00223 unsigned x, unsigned y, unsigned w, unsigned h)
00224 {
00225 int i;
00226 int step = 3;
00227
00228 dst += segment_width * (step * x + y * dst_linesize);
00229 w *= segment_width * step;
00230 h *= segment_width;
00231 for (i = 0; i < h; i++) {
00232 memset(dst, val, w);
00233 dst += dst_linesize;
00234 }
00235 }
00236
00237 static void draw_digit(int digit, uint8_t *dst, unsigned dst_linesize,
00238 unsigned segment_width)
00239 {
00240 #define TOP_HBAR 1
00241 #define MID_HBAR 2
00242 #define BOT_HBAR 4
00243 #define LEFT_TOP_VBAR 8
00244 #define LEFT_BOT_VBAR 16
00245 #define RIGHT_TOP_VBAR 32
00246 #define RIGHT_BOT_VBAR 64
00247 struct {
00248 int x, y, w, h;
00249 } segments[] = {
00250 { 1, 0, 5, 1 },
00251 { 1, 6, 5, 1 },
00252 { 1, 12, 5, 1 },
00253 { 0, 1, 1, 5 },
00254 { 0, 7, 1, 5 },
00255 { 6, 1, 1, 5 },
00256 { 6, 7, 1, 5 }
00257 };
00258 static const unsigned char masks[10] = {
00259 TOP_HBAR |BOT_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00260 RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00261 TOP_HBAR|MID_HBAR|BOT_HBAR|LEFT_BOT_VBAR |RIGHT_TOP_VBAR,
00262 TOP_HBAR|MID_HBAR|BOT_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00263 MID_HBAR |LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00264 TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_BOT_VBAR,
00265 TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR |RIGHT_BOT_VBAR,
00266 TOP_HBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00267 TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR|LEFT_BOT_VBAR|RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00268 TOP_HBAR|BOT_HBAR|MID_HBAR|LEFT_TOP_VBAR |RIGHT_TOP_VBAR|RIGHT_BOT_VBAR,
00269 };
00270 unsigned mask = masks[digit];
00271 int i;
00272
00273 draw_rectangle(0, dst, dst_linesize, segment_width, 0, 0, 8, 13);
00274 for (i = 0; i < FF_ARRAY_ELEMS(segments); i++)
00275 if (mask & (1<<i))
00276 draw_rectangle(255, dst, dst_linesize, segment_width,
00277 segments[i].x, segments[i].y, segments[i].w, segments[i].h);
00278 }
00279
00280 #define GRADIENT_SIZE (6 * 256)
00281
00282 static void test_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
00283 {
00284 TestSourceContext *test = ctx->priv;
00285 uint8_t *p, *p0;
00286 int x, y;
00287 int color, color_rest;
00288 int icolor;
00289 int radius;
00290 int quad0, quad;
00291 int dquad_x, dquad_y;
00292 int grad, dgrad, rgrad, drgrad;
00293 int seg_size;
00294 int second;
00295 int i;
00296 uint8_t *data = picref->data[0];
00297 int width = picref->video->w;
00298 int height = picref->video->h;
00299
00300
00301 radius = (width + height) / 4;
00302 quad0 = width * width / 4 + height * height / 4 - radius * radius;
00303 dquad_y = 1 - height;
00304 p0 = data;
00305 for (y = 0; y < height; y++) {
00306 p = p0;
00307 color = 0;
00308 color_rest = 0;
00309 quad = quad0;
00310 dquad_x = 1 - width;
00311 for (x = 0; x < width; x++) {
00312 icolor = color;
00313 if (quad < 0)
00314 icolor ^= 7;
00315 quad += dquad_x;
00316 dquad_x += 2;
00317 *(p++) = icolor & 1 ? 255 : 0;
00318 *(p++) = icolor & 2 ? 255 : 0;
00319 *(p++) = icolor & 4 ? 255 : 0;
00320 color_rest += 8;
00321 if (color_rest >= width) {
00322 color_rest -= width;
00323 color++;
00324 }
00325 }
00326 quad0 += dquad_y;
00327 dquad_y += 2;
00328 p0 += picref->linesize[0];
00329 }
00330
00331
00332 p0 = p = data + picref->linesize[0] * height * 3/4;
00333 grad = (256 * test->nb_frame * test->time_base.num / test->time_base.den) %
00334 GRADIENT_SIZE;
00335 rgrad = 0;
00336 dgrad = GRADIENT_SIZE / width;
00337 drgrad = GRADIENT_SIZE % width;
00338 for (x = 0; x < width; x++) {
00339 *(p++) =
00340 grad < 256 || grad >= 5 * 256 ? 255 :
00341 grad >= 2 * 256 && grad < 4 * 256 ? 0 :
00342 grad < 2 * 256 ? 2 * 256 - 1 - grad : grad - 4 * 256;
00343 *(p++) =
00344 grad >= 4 * 256 ? 0 :
00345 grad >= 1 * 256 && grad < 3 * 256 ? 255 :
00346 grad < 1 * 256 ? grad : 4 * 256 - 1 - grad;
00347 *(p++) =
00348 grad < 2 * 256 ? 0 :
00349 grad >= 3 * 256 && grad < 5 * 256 ? 255 :
00350 grad < 3 * 256 ? grad - 2 * 256 : 6 * 256 - 1 - grad;
00351 grad += dgrad;
00352 rgrad += drgrad;
00353 if (rgrad >= GRADIENT_SIZE) {
00354 grad++;
00355 rgrad -= GRADIENT_SIZE;
00356 }
00357 if (grad >= GRADIENT_SIZE)
00358 grad -= GRADIENT_SIZE;
00359 }
00360 p = p0;
00361 for (y = height / 8; y > 0; y--) {
00362 memcpy(p+picref->linesize[0], p, 3 * width);
00363 p += picref->linesize[0];
00364 }
00365
00366
00367 seg_size = width / 80;
00368 if (seg_size >= 1 && height >= 13 * seg_size) {
00369 double time = av_q2d(test->time_base) * test->nb_frame *
00370 pow(10, test->nb_decimals);
00371 if (time > INT_MAX)
00372 return;
00373 second = (int)time;
00374 x = width - (width - seg_size * 64) / 2;
00375 y = (height - seg_size * 13) / 2;
00376 p = data + (x*3 + y * picref->linesize[0]);
00377 for (i = 0; i < 8; i++) {
00378 p -= 3 * 8 * seg_size;
00379 draw_digit(second % 10, p, picref->linesize[0], seg_size);
00380 second /= 10;
00381 if (second == 0)
00382 break;
00383 }
00384 }
00385 }
00386
00387 static av_cold int test_init(AVFilterContext *ctx, const char *args, void *opaque)
00388 {
00389 TestSourceContext *test = ctx->priv;
00390
00391 test->class = &testsrc_class;
00392 test->fill_picture_fn = test_fill_picture;
00393 return init(ctx, args, opaque);
00394 }
00395
00396 static int test_query_formats(AVFilterContext *ctx)
00397 {
00398 static const enum PixelFormat pix_fmts[] = {
00399 PIX_FMT_RGB24, PIX_FMT_NONE
00400 };
00401 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00402 return 0;
00403 }
00404
00405 AVFilter avfilter_vsrc_testsrc = {
00406 .name = "testsrc",
00407 .description = NULL_IF_CONFIG_SMALL("Generate test pattern."),
00408 .priv_size = sizeof(TestSourceContext),
00409 .init = test_init,
00410
00411 .query_formats = test_query_formats,
00412
00413 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
00414
00415 .outputs = (const AVFilterPad[]) {{ .name = "default",
00416 .type = AVMEDIA_TYPE_VIDEO,
00417 .request_frame = request_frame,
00418 .config_props = config_props, },
00419 { .name = NULL }},
00420 };
00421
00422 #endif
00423
00424 #if CONFIG_RGBTESTSRC_FILTER
00425
00426 static const char *rgbtestsrc_get_name(void *ctx)
00427 {
00428 return "rgbtestsrc";
00429 }
00430
00431 static const AVClass rgbtestsrc_class = {
00432 .class_name = "RGBTestSourceContext",
00433 .item_name = rgbtestsrc_get_name,
00434 .option = testsrc_options,
00435 };
00436
00437 #define R 0
00438 #define G 1
00439 #define B 2
00440 #define A 3
00441
00442 static void rgbtest_put_pixel(uint8_t *dst, int dst_linesize,
00443 int x, int y, int r, int g, int b, enum PixelFormat fmt,
00444 int rgba_map[4])
00445 {
00446 int32_t v;
00447 uint8_t *p;
00448
00449 switch (fmt) {
00450 case PIX_FMT_BGR444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); break;
00451 case PIX_FMT_RGB444: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b >> 4) << 8) | ((g >> 4) << 4) | (r >> 4); break;
00452 case PIX_FMT_BGR555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<10) | ((g>>3)<<5) | (b>>3); break;
00453 case PIX_FMT_RGB555: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<10) | ((g>>3)<<5) | (r>>3); break;
00454 case PIX_FMT_BGR565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((r>>3)<<11) | ((g>>2)<<5) | (b>>3); break;
00455 case PIX_FMT_RGB565: ((uint16_t*)(dst + y*dst_linesize))[x] = ((b>>3)<<11) | ((g>>2)<<5) | (r>>3); break;
00456 case PIX_FMT_RGB24:
00457 case PIX_FMT_BGR24:
00458 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8));
00459 p = dst + 3*x + y*dst_linesize;
00460 AV_WL24(p, v);
00461 break;
00462 case PIX_FMT_RGBA:
00463 case PIX_FMT_BGRA:
00464 case PIX_FMT_ARGB:
00465 case PIX_FMT_ABGR:
00466 v = (r << (rgba_map[R]*8)) + (g << (rgba_map[G]*8)) + (b << (rgba_map[B]*8)) + (255 << (rgba_map[A]*8));
00467 p = dst + 4*x + y*dst_linesize;
00468 AV_WL32(p, v);
00469 break;
00470 }
00471 }
00472
00473 static void rgbtest_fill_picture(AVFilterContext *ctx, AVFilterBufferRef *picref)
00474 {
00475 TestSourceContext *test = ctx->priv;
00476 int x, y, w = picref->video->w, h = picref->video->h;
00477
00478 for (y = 0; y < h; y++) {
00479 for (x = 0; x < picref->video->w; x++) {
00480 int c = 256*x/w;
00481 int r = 0, g = 0, b = 0;
00482
00483 if (3*y < h ) r = c;
00484 else if (3*y < 2*h) g = c;
00485 else b = c;
00486
00487 rgbtest_put_pixel(picref->data[0], picref->linesize[0], x, y, r, g, b,
00488 ctx->outputs[0]->format, test->rgba_map);
00489 }
00490 }
00491 }
00492
00493 static av_cold int rgbtest_init(AVFilterContext *ctx, const char *args, void *opaque)
00494 {
00495 TestSourceContext *test = ctx->priv;
00496
00497 test->class = &rgbtestsrc_class;
00498 test->fill_picture_fn = rgbtest_fill_picture;
00499 return init(ctx, args, opaque);
00500 }
00501
00502 static int rgbtest_query_formats(AVFilterContext *ctx)
00503 {
00504 static const enum PixelFormat pix_fmts[] = {
00505 PIX_FMT_RGBA, PIX_FMT_ARGB, PIX_FMT_BGRA, PIX_FMT_ABGR,
00506 PIX_FMT_BGR24, PIX_FMT_RGB24,
00507 PIX_FMT_RGB444, PIX_FMT_BGR444,
00508 PIX_FMT_RGB565, PIX_FMT_BGR565,
00509 PIX_FMT_RGB555, PIX_FMT_BGR555,
00510 PIX_FMT_NONE
00511 };
00512 avfilter_set_common_pixel_formats(ctx, avfilter_make_format_list(pix_fmts));
00513 return 0;
00514 }
00515
00516 static int rgbtest_config_props(AVFilterLink *outlink)
00517 {
00518 TestSourceContext *test = outlink->src->priv;
00519
00520 switch (outlink->format) {
00521 case PIX_FMT_ARGB: test->rgba_map[A] = 0; test->rgba_map[R] = 1; test->rgba_map[G] = 2; test->rgba_map[B] = 3; break;
00522 case PIX_FMT_ABGR: test->rgba_map[A] = 0; test->rgba_map[B] = 1; test->rgba_map[G] = 2; test->rgba_map[R] = 3; break;
00523 case PIX_FMT_RGBA:
00524 case PIX_FMT_RGB24: test->rgba_map[R] = 0; test->rgba_map[G] = 1; test->rgba_map[B] = 2; test->rgba_map[A] = 3; break;
00525 case PIX_FMT_BGRA:
00526 case PIX_FMT_BGR24: test->rgba_map[B] = 0; test->rgba_map[G] = 1; test->rgba_map[R] = 2; test->rgba_map[A] = 3; break;
00527 }
00528
00529 return config_props(outlink);
00530 }
00531
00532 AVFilter avfilter_vsrc_rgbtestsrc = {
00533 .name = "rgbtestsrc",
00534 .description = NULL_IF_CONFIG_SMALL("Generate RGB test pattern."),
00535 .priv_size = sizeof(TestSourceContext),
00536 .init = rgbtest_init,
00537
00538 .query_formats = rgbtest_query_formats,
00539
00540 .inputs = (const AVFilterPad[]) {{ .name = NULL}},
00541
00542 .outputs = (const AVFilterPad[]) {{ .name = "default",
00543 .type = AVMEDIA_TYPE_VIDEO,
00544 .request_frame = request_frame,
00545 .config_props = rgbtest_config_props, },
00546 { .name = NULL }},
00547 };
00548
00549 #endif