[FFmpeg-devel] [PATCH] lavfi: USPP filter

Michael Niedermayer michaelni at gmx.at
Tue Dec 9 15:57:48 CET 2014


On Tue, Dec 09, 2014 at 04:03:56PM +0530, arwa arif wrote:
[...]

> +static void filter(USPPContext *p, uint8_t *dst[3], uint8_t *src[3],
> +                 int dst_stride[3], int src_stride[3], int width,
> +                 int height, uint8_t *qp_store, int qp_stride)
> +{
> +    int x, y, i, j;
> +    const int count = 1<<p->log2_count;
> +    for(i = 0 ; i < 3 ; i++) {
> +        int is_chroma = !!i;
> +        int w = width >>is_chroma;
> +        int h = height>>is_chroma;
> +        int stride = p->temp_stride[i];
> +        int block = BLOCK>>is_chroma;
> +
> +        if (!src[i] || !dst[i])
> +            continue;
> +        for(y = 0 ; y < h ; y++) {
> +            int index = block + block*stride + y*stride;
> +            memcpy( p->src[i] + index , src[i] + y*src_stride[i] , w );
> +            for(x = 0 ; x < block ; x++) {
> +                p->src[i][index     - x - 1]= p->src[i][index +     x    ];
> +                p->src[i][index + w + x    ]= p->src[i][index + w - x - 1];
> +            }
> +        }
> +        for(y = 0 ; y < block ; y++){
> +            memcpy(p->src[i] + (  block-1-y)*stride , p->src[i] + (  y+block  )*stride , stride);
> +            memcpy(p->src[i] + (h+block  +y)*stride , p->src[i] + (h-y+block-1)*stride , stride);
> +        }
> +
> +        p->frame->linesize[i]= stride;
> +        memset(p->temp[i] , 0 , (h + 2*block)*stride*sizeof(int16_t));
> +    }
> +
> +    if(p->qp)
> +        p->frame->quality= p->qp * FF_QP2LAMBDA;
> +    else
> +        p->frame->quality= norm_qscale(qp_store[0] , p->qscale_type) * FF_QP2LAMBDA;
> +//    init per MB qscale stuff FIXME
> +    p->frame->height = height;
> +    p->frame->width  = width;
> +
> +    for(i = 0 ; i < count ; i++) {
> +        const int x1 = offset[i+count-1][0];
> +        const int y1 = offset[i+count-1][1];
> +        int offset;
> +        p->frame->data[0] = p->src[0] + x1   + y1   * p->frame->linesize[0];
> +        p->frame->data[1] = p->src[1] + x1/2 + y1/2 * p->frame->linesize[1];
> +        p->frame->data[2] = p->src[2] + x1/2 + y1/2 * p->frame->linesize[2];
> +
> +        AVPacket pkt = {.data = p->outbuf, .size = p->outbuf_size};

the packet should be cleared with av_init_packet() before setting
data/size


> +        avcodec_encode_video2(p->avctx_enc[i] , &pkt , p->frame , &p->outbuf_size );

the last argument is not the size but a flag that indicates if a
packet was encoded. If you use outbuf_size there, it will be
overwritten and cause problems


> +
> +        p->frame_dec     = p->avctx_enc[i]->coded_frame;
> +        p->frame->format = p->avctx_enc[i]->pix_fmt;

p->frame is an input to avcodec_encode_video2()
avcodec_encode_video2() takes the AVFrame and produces a AVPacket
thus format must be set before calling avcodec_encode_video2()


[...]
> +static int config_input(AVFilterLink *inlink)
> +{
> +
> +    AVFilterContext *ctx = inlink->dst;
> +    USPPContext *uspp = ctx->priv;
> +    const int height = inlink->h;
> +    const int width = inlink->w;
> +
> +    AVCodec *enc= avcodec_find_encoder(AV_CODEC_ID_SNOW);
> +    int i;
> +
> +    if (!uspp->use_bframe_qp) {
> +        /* we are assuming here the qp blocks will not be smaller that 16x16 */
> +        uspp->non_b_qp_alloc_size = FF_CEIL_RSHIFT(width, 4) * FF_CEIL_RSHIFT(height, 4);
> +        uspp->non_b_qp_table = av_calloc(uspp->non_b_qp_alloc_size, sizeof(*uspp->non_b_qp_table));
> +
> +        if (!uspp->non_b_qp_table)
> +            return AVERROR(ENOMEM);
> +    }
> +
> +    for(i = 0 ; i < 3; i++) {
> +        int is_chroma = !!i;
> +        int w = ((width  + 4*BLOCK-1) & (~(2*BLOCK-1)))>>is_chroma;
> +        int h = ((height + 4*BLOCK-1) & (~(2*BLOCK-1)))>>is_chroma;
> +
> +        uspp->temp_stride[i] = w;
> +        uspp->temp[i] = av_malloc(uspp->temp_stride[i]*h*sizeof(int16_t));
> +        uspp->src [i] = av_malloc(uspp->temp_stride[i]*h*sizeof(uint8_t));
> +    }
> +
> +    for(i = 0 ; i < (1<<uspp->log2_count) ; i++) {
> +        AVCodecContext *avctx_enc;
> +        AVDictionary *opts = NULL;
> +        int ret;
> +
> +        avctx_enc = uspp->avctx_enc[i]= avcodec_alloc_context3(NULL);
> +        avctx_enc->width = width + BLOCK;
> +        avctx_enc->height = height + BLOCK;
> +        avctx_enc->time_base = (AVRational){1,25};  // meaningless
> +        avctx_enc->gop_size = 300;
> +        avctx_enc->max_b_frames = 0;
> +        avctx_enc->pix_fmt = AV_PIX_FMT_YUV420P;
> +        avctx_enc->flags = CODEC_FLAG_QSCALE | CODEC_FLAG_LOW_DELAY;
> +        avctx_enc->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;
> +        avctx_enc->global_quality = 123;
> +        av_dict_set(&opts, "no_bitstream", "1", 0);
> +        ret = avcodec_open2(avctx_enc, enc, &opts);
> +        if (ret < 0)
> +            return ret;
> +        av_dict_free(&opts);
> +        av_assert0(avctx_enc->codec);
> +    }

> +    uspp->frame     = av_frame_alloc();
> +    uspp->frame_dec = av_frame_alloc();

these two need to be freed with av_frame_free() in uninit


> +
> +    uspp->outbuf_size = (width + BLOCK)*(height + BLOCK)*10;
> +    uspp->outbuf      = av_malloc(uspp->outbuf_size);

outbuf has to be freed in uninit()


[...]
> +static av_cold void uninit(AVFilterContext *ctx)
> +{
> +    USPPContext *uspp = ctx->priv;
> +    if(!uspp) return;
> +
> +    av_freep(&uspp->temp);
> +    av_freep(&uspp->src);
> +

> +    if (uspp->avctx_enc) {
> +        avcodec_close(*uspp->avctx_enc);

this only deallocates the first encoder like using uspp->avctx_enc[0]
would
they all must be deallocated


you can use something like
valgrind --leak-check=full ./ffmpeg_g -f lavfi -i testsrc  -vf uspp=1 -vframes 1 -f null -

to find out what is being allocated but not deallocated

[...]

-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

You can kill me, but you cannot change the truth.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: Digital signature
URL: <https://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20141209/527eeba9/attachment.asc>


More information about the ffmpeg-devel mailing list