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