diff --git a/tools/playvid.c b/tools/playvid.c index c400ec7..efe3897 100644 --- a/tools/playvid.c +++ b/tools/playvid.c @@ -478,7 +478,7 @@ int main (int argc, char *argv[]) thresh = thresh_dark; tparams.threshold = thresh; - olTraceReInit(&trace_ctx, &tparams); + olTraceReInit(trace_ctx, &tparams); olTraceFree(&result); obj = olTrace(trace_ctx, frame->data[0], frame->linesize[0], &result); @@ -512,6 +512,8 @@ int main (int argc, char *argv[]) } while ((time+frametime) < vidtime); } + olTraceDeinit(trace_ctx); + for(i=0;ip = *params; ctx->tracebuf = malloc(ctx->p.width * ctx->p.height * sizeof(*ctx->tracebuf)); ctx->sb_size = ctx->p.width * 16; @@ -227,14 +224,42 @@ int olTraceInit(OLTraceCtx **pctx, OLTraceParams *params) ctx->pb = malloc(ctx->pb_size * sizeof(*ctx->pb)); ctx->pbp = ctx->pb; ctx->pb_end = ctx->pb + ctx->pb_size; +} +static void free_bufs(OLTraceCtx *ctx) +{ + if (ctx->tracebuf) + free(ctx->tracebuf); + if (ctx->sb) + free(ctx->sb); + if (ctx->pb) + free(ctx->pb); +} + +int olTraceInit(OLTraceCtx **pctx, OLTraceParams *params) +{ + OLTraceCtx *ctx = malloc(sizeof(OLTraceCtx)); + + ctx->p = *params; + + alloc_bufs(ctx); *pctx = ctx; return 0; } -int olTraceReInit(OLTraceCtx **ctx, OLTraceParams *params) + +int olTraceReInit(OLTraceCtx *ctx, OLTraceParams *params) { - (*ctx)->p = *params; + if (ctx->p.mode != params->mode || + ctx->p.width != params->width || + ctx->p.height != params->height) + { + free_bufs(ctx); + ctx->p = *params; + alloc_bufs(ctx); + } else { + ctx->p = *params; + } return 0; } @@ -249,9 +274,13 @@ void olTraceFree(OLTraceResult *result) } } - void olTraceDeinit(OLTraceCtx *ctx) { + if (!ctx) + return; + + free_bufs(ctx); + free(ctx); } static void find_edges_thresh(OLTraceCtx *ctx, uint8_t *src, unsigned int stride) diff --git a/tools/trace.h b/tools/trace.h index 949eefc..0410d1e 100644 --- a/tools/trace.h +++ b/tools/trace.h @@ -51,7 +51,7 @@ typedef struct { } OLTraceResult; int olTraceInit(OLTraceCtx **ctx, OLTraceParams *params); -int olTraceReInit(OLTraceCtx **ctx, OLTraceParams *params); +int olTraceReInit(OLTraceCtx *ctx, OLTraceParams *params); int olTrace(OLTraceCtx *ctx, uint8_t *src, icoord stride, OLTraceResult *result); void olTraceFree(OLTraceResult *result);