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