[FFmpeg-devel] How to correctly use init and uninit

Paul B Mahol onemda at gmail.com
Fri Apr 19 14:38:42 EEST 2019


On 4/19/19, Ulf Zibis <Ulf.Zibis at cosoco.de> wrote:
> Hi,
>
> to libavfilter/vf_fillborders.c I've added the following:
> ===================================================
> typedef struct FillBordersContext {
>     [.....]
>     uint16_t *filler;
>
>     void (*fillborders)(struct FillBordersContext *s, AVFrame *frame);
> } FillBordersContext;
>
> // following must be moved up to compile correctly with
> STOP_TIMER(testcase)
>
> #define OFFSET(x) offsetof(FillBordersContext, x)
> #define FLAGS AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_FILTERING_PARAM
>
> static const AVOption fillborders_options[] = {
>     [.....]
> };
>
> AVFILTER_DEFINE_CLASS(fillborders);
>
> [.....]
>
> static char testcase[128];
>
> static av_cold int init(AVFilterContext *ctx)
> {
>     FillBordersContext *s = ctx->priv;
>     return 0;
> }
>
> static int config_init(AVFilterLink *inlink)
> {
>     [.....]
>     int fill_sz = FFMAX3(s->left, s->right, s->top != 0 || inlink->h -
> s->bottom != 0 ? inlink->w : 0);
>     printf("fill_sz: %d\n", fill_sz);
>     s->filler = av_malloc(fill_sz * sizeof(uint16_t));
> /*
>     for (int i = 0; i < fill_sz; i++)
>         s->filler[i] = s->fill[p] << (s->depth - 8);
> */
>
>     sprintf(testcase, "fillborders=%d:%d:%d:%d:%s %dp-%dbit-%dx%d",
>             s->left, s->right, s->top, s->bottom, fillborders_options[5
> + s->mode].name,
>             s->nb_planes, s->depth, desc->log2_chroma_w,
> desc->log2_chroma_h);
>
>     return 0;
> }
>
> static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
> {
>     FillBordersContext *s = inlink->dst->priv;
>
>     START_TIMER
>
>     s->fillborders(s, frame);
>
>     STOP_TIMER(testcase)
>
>     return ff_filter_frame(inlink->dst->outputs[0], frame);
> }
>
> static av_cold void uninit(AVFilterContext *ctx)
> {
>     FillBordersContext *s = ctx->priv;
>     av_freep(s->filler);
> }
>
> [.....]
>
> AVFilter ff_vf_fillborders = {
>     [.....]
>     .init          = init,
>     .uninit        = uninit,
>     [.....]
> };
> ===================================================
>
> When running the filter with (1-plane 16-bit GRAY) I get "free():
> invalid pointer" :
>
> $ ./ffmpeg -y -i debug/16.jpg -vf
> loop=1:1:0:start=0,fillborders=25:25:0:0:fixed:green -update 1
> debug/16_fixed_25-25-0-0.jpg
> ffmpeg version N-93468-g6e6f598ae2 Copyright (c) 2000-2019 the FFmpeg
> developers
>   built with gcc 7 (Ubuntu 7.3.0-27ubuntu1~18.04)
>   configuration:
>   libavutil      56. 26.100 / 56. 26.100
>   libavcodec     58. 47.105 / 58. 47.105
>   libavformat    58. 26.101 / 58. 26.101
>   libavdevice    58.  7.100 / 58.  7.100
>   libavfilter     7. 48.100 /  7. 48.100
>   libswscale      5.  4.100 /  5.  4.100
>   libswresample   3.  4.100 /  3.  4.100
> Input #0, image2, from 'debug/16.jpg':
>   Duration: 00:00:00.04, start: 0.000000, bitrate: 103852 kb/s
>     Stream #0:0: Video: mjpeg (Lossless),
> gray16le(bt470bg/unknown/unknown), 640x480 [SAR 96:96 DAR 4:3],
> lossless, 25 tbr, 25 tbn, 25 tbc
> Stream mapping:
>   Stream #0:0 -> #0:0 (mjpeg (native) -> mjpeg (native))
> Press [q] to stop, [?] for help
> fill_sz: 640
> [swscaler @ 0x55e255ec6980] deprecated pixel format used, make sure you
> did set range correctly
>  481140 decicycles in fillborders=25:25:0:0:fixed 1p-16bit-0x0,       1
> runs,      0 skips
> Output #0, image2, to 'debug/16_fixed_25-25-0-0.jpg':
>   Metadata:
>     encoder         : Lavf58.26.101
>     Stream #0:0: Video: mjpeg, yuvj444p(pc), 640x480 [SAR 1:1 DAR 4:3],
> q=2-31, 200 kb/s, 25 fps, 25 tbn, 25 tbc
>     Metadata:
>       encoder         : Lavc58.47.105 mjpeg
>     Side data:
>       cpb: bitrate max/min/avg: 0/0/200000 buffer size: 0 vbv_delay: -1
>  480645 decicycles in fillborders=25:25:0:0:fixed 1p-16bit-0x0,       2
> runs,      0 skips
> frame=    2 fps=0.0 q=3.9 Lsize=N/A time=00:00:00.08 bitrate=N/A
> speed=1.56x
> video:75kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB
> muxing overhead: unknown
> free(): invalid pointer
> Abgebrochen (Speicherabzug geschrieben)
> ===================================================
>
> When running the filter with "-v error" AND (1-plane 8-bit GRAY) I don't
> get the error, but again with 16-bit, weird :
>
> $ ./ffmpeg -y -v error -i debug/8.jpg -vf
> loop=1:1:0:start=0,fillborders=25:25:0:0:fixed:green -update 1
> debug/8_fixed_25-25-0-0.jpg
> fill_sz: 640
>  227340 decicycles in fillborders=25:25:0:0:fixed 1p-8bit-0x0,       1
> runs,      0 skips
>  226575 decicycles in fillborders=25:25:0:0:fixed 1p-8bit-0x0,       2
> runs,      0 skips
> $ ./ffmpeg -y -v error -i debug/16.jpg -vf
> loop=1:1:0:start=0,fillborders=25:25:0:0:fixed:green -update 1
> debug/16_fixed_25-25-0-0.jpg
> fill_sz: 640
>  447750 decicycles in fillborders=25:25:0:0:fixed 1p-16bit-0x0,       1
> runs,      0 skips
>  449775 decicycles in fillborders=25:25:0:0:fixed 1p-16bit-0x0,       2
> runs,      0 skips
> free(): invalid pointer
> Abgebrochen (Speicherabzug geschrieben)
> ===================================================
>
> The complete file is in the attachment.
>
> Does one have any hints for me?

Use either av_freep(&s->filler) or av_free(s->filler).

You do not need to use loop filter on single png.

Use something like this:

ffmpeg -f lavfi -i smptebars=size=hd720 -vf fillborders=... -f null -


More information about the ffmpeg-devel mailing list