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