void av_close_input_stream(AVFormatContext *s)
{
    int i;
    AVStream *st;
    if (s->cur_st && s->cur_st->parser)
        av_free_packet(&s->cur_pkt);
    if (s->iformat->read_close)
        s->iformat->read_close(s);
    for(i=0;i<s->nb_streams;i++) {
        st = s->streams[i];
        if (st->parser) {
            av_parser_close(st->parser);
        }
        av_free(st->index_entries);
        av_free(st->codec->extradata);
        av_free(st->codec);
        av_free(st->filename);
        av_free(st);
    }
    for(i=s->nb_programs-1; i>=0; i--) {
        av_freep(&s->programs[i]->provider_name);
        av_freep(&s->programs[i]->name);
        av_freep(&s->programs[i]->stream_index);
        av_freep(&s->programs[i]);
    }
    flush_packet_queue(s);
    av_freep(&s->priv_data);
    av_free(s);
}
['static void Process0(void *ctx,\n              AVPicture *picture,\n              enum PixelFormat pix_fmt,\n              int src_width,\n              int src_height,\n              int64_t pts)\n{\n    ContextInfo *ci = (ContextInfo *) ctx;\n    char *buf = 0;\n    AVPicture picture1;\n    AVPicture *pict = picture;\n    AVFrame *pFrameRGB;\n    int xm_size;\n    int ym_size;\n    int x;\n    int y;\n    int offs, offsm;\n    int mpoffs;\n    uint32_t *p_pixel = 0;\n    uint32_t pixel_meck;\n    uint32_t pixel;\n    uint32_t pixelm;\n    int tmp;\n    int thrR = ci->thrR;\n    int thrG = ci->thrG;\n    int thrB = ci->thrB;\n    if (pix_fmt != PIX_FMT_RGB32) {\n        int size;\n        size = avpicture_get_size(PIX_FMT_RGB32, src_width, src_height);\n        buf = av_malloc(size);\n        avpicture_fill(&picture1, buf, PIX_FMT_RGB32, src_width, src_height);\n        ci->toRGB_convert_ctx = sws_getCachedContext(ci->toRGB_convert_ctx,\n                                    src_width, src_height, pix_fmt,\n                                    src_width, src_height, PIX_FMT_RGB32,\n                                    sws_flags, NULL, NULL, NULL);\n        if (ci->toRGB_convert_ctx == NULL) {\n            av_log(NULL, AV_LOG_ERROR,\n                   "Cannot initialize the toRGB conversion context\\n");\n            return;\n        }\n        sws_scale(ci->toRGB_convert_ctx,\n                 picture->data, picture->linesize, 0, src_height,\n                 picture1.data, picture1.linesize);\n        pict = &picture1;\n    }\n    if (0 > get_watermark_picture(ci, 0)) {\n        return;\n    }\n    pFrameRGB = ci->pFrameRGB;\n    xm_size = ci->x_size;\n    ym_size = ci->y_size;\n    for (y=0; y<src_height; y++) {\n        offs = y * (src_width * 4);\n        offsm = (((y * ym_size) / src_height) * 4) * xm_size;\n        for (x=0; x<src_width; x++) {\n            mpoffs = offsm + (((x * xm_size) / src_width) * 4);\n            p_pixel = (uint32_t *)&((pFrameRGB->data[0])[mpoffs]);\n            pixelm = *p_pixel;\n            p_pixel = (uint32_t *)&((pict->data[0])[offs]);\n            pixel = *p_pixel;\n            pixel_meck = pixel & 0xff000000;\n            tmp = (int)((pixel >> 16) & 0xff) + (int)((pixelm >> 16) & 0xff) - thrR;\n            if (tmp > 255) tmp = 255;\n            if (tmp < 0) tmp = 0;\n            pixel_meck |= (tmp << 16) & 0xff0000;\n            tmp = (int)((pixel >> 8) & 0xff) + (int)((pixelm >> 8) & 0xff) - thrG;\n            if (tmp > 255) tmp = 255;\n            if (tmp < 0) tmp = 0;\n            pixel_meck |= (tmp << 8) & 0xff00;\n            tmp = (int)((pixel >> 0) & 0xff) + (int)((pixelm >> 0) & 0xff) - thrB;\n            if (tmp > 255) tmp = 255;\n            if (tmp < 0) tmp = 0;\n            pixel_meck |= (tmp << 0) & 0xff;\n            *p_pixel = pixel_meck;\n            offs += 4;\n        }\n    }\n    if (pix_fmt != PIX_FMT_RGB32) {\n        ci->fromRGB_convert_ctx = sws_getCachedContext(ci->fromRGB_convert_ctx,\n                                      src_width, src_height, PIX_FMT_RGB32,\n                                      src_width, src_height, pix_fmt,\n                                      sws_flags, NULL, NULL, NULL);\n        if (ci->fromRGB_convert_ctx == NULL) {\n            av_log(NULL, AV_LOG_ERROR,\n                   "Cannot initialize the fromRGB conversion context\\n");\n            return;\n        }\n        sws_scale(ci->fromRGB_convert_ctx,\n                 picture1.data, picture1.linesize, 0, src_height,\n                 picture->data, picture->linesize);\n    }\n    av_free(buf);\n}', 'int sws_scale(struct SwsContext *ctx, uint8_t* src[], int srcStride[],\n              int srcSliceY, int srcSliceH, uint8_t* dst[], int dstStride[])\n{\n    AVPicture src_pict, dst_pict;\n    int i, res = 0;\n    AVPicture picture_format_temp;\n    AVPicture picture_resample_temp, *formatted_picture, *resampled_picture;\n    uint8_t *buf1 = NULL, *buf2 = NULL;\n    enum PixelFormat current_pix_fmt;\n    for (i = 0; i < 4; i++) {\n        src_pict.data[i] = src[i];\n        src_pict.linesize[i] = srcStride[i];\n        dst_pict.data[i] = dst[i];\n        dst_pict.linesize[i] = dstStride[i];\n    }\n    if ((ctx->resampling_ctx->iwidth != ctx->resampling_ctx->owidth) ||\n        (ctx->resampling_ctx->iheight != ctx->resampling_ctx->oheight)) {\n        if (ctx->src_pix_fmt != PIX_FMT_YUV420P) {\n            int size;\n            size = avpicture_get_size(PIX_FMT_YUV420P, ctx->resampling_ctx->iwidth, ctx->resampling_ctx->iheight);\n            buf1 = av_malloc(size);\n            if (!buf1) {\n                res = -1;\n                goto the_end;\n            }\n            formatted_picture = &picture_format_temp;\n            avpicture_fill((AVPicture*)formatted_picture, buf1,\n                           PIX_FMT_YUV420P, ctx->resampling_ctx->iwidth, ctx->resampling_ctx->iheight);\n            if (img_convert((AVPicture*)formatted_picture, PIX_FMT_YUV420P,\n                            &src_pict, ctx->src_pix_fmt,\n                            ctx->resampling_ctx->iwidth, ctx->resampling_ctx->iheight) < 0) {\n                av_log(NULL, AV_LOG_ERROR, "pixel format conversion not handled\\n");\n                res = -1;\n                goto the_end;\n            }\n        } else {\n            formatted_picture = &src_pict;\n        }\n        if (ctx->dst_pix_fmt != PIX_FMT_YUV420P) {\n            int size;\n            size = avpicture_get_size(PIX_FMT_YUV420P, ctx->resampling_ctx->owidth, ctx->resampling_ctx->oheight);\n            buf2 = av_malloc(size);\n            if (!buf2) {\n                res = -1;\n                goto the_end;\n            }\n            resampled_picture = &picture_resample_temp;\n            avpicture_fill((AVPicture*)resampled_picture, buf2,\n                           PIX_FMT_YUV420P, ctx->resampling_ctx->owidth, ctx->resampling_ctx->oheight);\n        } else {\n            resampled_picture = &dst_pict;\n        }\n        img_resample(ctx->resampling_ctx, resampled_picture, formatted_picture);\n        current_pix_fmt = PIX_FMT_YUV420P;\n    } else {\n        resampled_picture = &src_pict;\n        current_pix_fmt = ctx->src_pix_fmt;\n    }\n    if (current_pix_fmt != ctx->dst_pix_fmt) {\n        if (img_convert(&dst_pict, ctx->dst_pix_fmt,\n                        resampled_picture, current_pix_fmt,\n                        ctx->resampling_ctx->owidth, ctx->resampling_ctx->oheight) < 0) {\n            av_log(NULL, AV_LOG_ERROR, "pixel format conversion not handled\\n");\n            res = -1;\n            goto the_end;\n        }\n    } else if (resampled_picture != &dst_pict) {\n        av_picture_copy(&dst_pict, resampled_picture, current_pix_fmt,\n                        ctx->resampling_ctx->owidth, ctx->resampling_ctx->oheight);\n    }\nthe_end:\n    av_free(buf1);\n    av_free(buf2);\n    return res;\n}', 'int get_watermark_picture(ContextInfo *ci, int cleanup)\n{\n    if (1 == ci->is_done && 0 == cleanup) return 0;\n    if (0 == ci->pFrameRGB &&\n        0 == cleanup)\n    {\n        if (av_open_input_file(&ci->pFormatCtx, ci->filename, NULL, 0, NULL) != 0) {\n            ci->i = strlen(ci->filename);\n            if (0 == ci->i) {\n                av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() No filename to watermark vhook\\n");\n                return -1;\n            }\n            while (ci->i > 0) {\n                if (ci->filename[ci->i] == \'.\') {\n                    ci->i++;\n                    break;\n                }\n                ci->i--;\n            }\n               ci->p_ext = &(ci->filename[ci->i]);\n            ci->file_iformat = av_find_input_format (ci->p_ext);\n            if (0 == ci->file_iformat) {\n                av_log(NULL, AV_LOG_INFO, "get_watermark_picture() attempt to use image2 for [%s]\\n", ci->p_ext);\n                ci->file_iformat = av_find_input_format ("image2");\n            }\n            if (0 == ci->file_iformat) {\n                av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Really failed to find iformat [%s]\\n", ci->p_ext);\n                return -1;\n            }\n            if (av_open_input_file(&ci->pFormatCtx, ci->filename, ci->file_iformat, 0, NULL)!=0) {\n                av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open input file [%s]\\n", ci->filename);\n                return -1;\n            }\n        }\n        if(av_find_stream_info(ci->pFormatCtx)<0) {\n            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find stream info\\n");\n            return -1;\n        }\n        ci->videoStream=-1;\n        for(ci->i = 0; ci->i < ci->pFormatCtx->nb_streams; ci->i++)\n            if(ci->pFormatCtx->streams[ci->i]->codec->codec_type==CODEC_TYPE_VIDEO)\n            {\n                ci->videoStream = ci->i;\n                break;\n            }\n        if(ci->videoStream == -1) {\n            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any video stream\\n");\n            return -1;\n        }\n        ci->st = ci->pFormatCtx->streams[ci->videoStream];\n        ci->x_size = ci->st->codec->width;\n        ci->y_size = ci->st->codec->height;\n        ci->pCodecCtx = ci->pFormatCtx->streams[ci->videoStream]->codec;\n        ci->pCodec = avcodec_find_decoder(ci->pCodecCtx->codec_id);\n        if(ci->pCodec == NULL) {\n            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to find any codec\\n");\n            return -1;\n        }\n        if(avcodec_open(ci->pCodecCtx, ci->pCodec)<0) {\n            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to open codec\\n");\n            return -1;\n        }\n        if (ci->pCodecCtx->time_base.den>1000 && ci->pCodecCtx->time_base.num==1)\n            ci->pCodecCtx->time_base.num=1000;\n        ci->pFrame = avcodec_alloc_frame();\n        ci->pFrameRGB=avcodec_alloc_frame();\n        if(ci->pFrameRGB==NULL) {\n            av_log(NULL, AV_LOG_ERROR, "get_watermark_picture() Failed to alloc pFrameRGB\\n");\n            return -1;\n        }\n        ci->numBytes = avpicture_get_size(PIX_FMT_RGB32, ci->pCodecCtx->width,\n            ci->pCodecCtx->height);\n        ci->buffer = av_malloc(ci->numBytes);\n        avpicture_fill((AVPicture *)ci->pFrameRGB, ci->buffer, PIX_FMT_RGB32,\n            ci->pCodecCtx->width, ci->pCodecCtx->height);\n    }\n    if (0 == cleanup)\n    {\n        while(av_read_frame(ci->pFormatCtx, &ci->packet)>=0)\n        {\n            if(ci->packet.stream_index == ci->videoStream)\n            {\n                avcodec_decode_video(ci->pCodecCtx, ci->pFrame, &ci->frameFinished,\n                    ci->packet.data, ci->packet.size);\n                if(ci->frameFinished)\n                {\n                    ci->watermark_convert_ctx =\n                        sws_getCachedContext(ci->watermark_convert_ctx,\n                            ci->pCodecCtx->width, ci->pCodecCtx->height, ci->pCodecCtx->pix_fmt,\n                            ci->pCodecCtx->width, ci->pCodecCtx->height, PIX_FMT_RGB32,\n                            sws_flags, NULL, NULL, NULL);\n                    if (ci->watermark_convert_ctx == NULL) {\n                        av_log(NULL, AV_LOG_ERROR,\n                              "Cannot initialize the watermark conversion context\\n");\n                        return -1;\n                    }\n                    sws_scale(ci->watermark_convert_ctx,\n                             ci->pFrame->data, ci->pFrame->linesize, 0, ci->pCodecCtx->height,\n                             ci->pFrameRGB->data, ci->pFrameRGB->linesize);\n                    return 0;\n                }\n            }\n            av_free_packet(&ci->packet);\n        }\n        ci->is_done = 1;\n        return 0;\n    }\n    if (0 != cleanup)\n    {\n        av_freep(&ci->buffer);\n        av_freep(&ci->pFrameRGB);\n        if (0 != ci->pCodecCtx) {\n            avcodec_close(ci->pCodecCtx);\n            ci->pCodecCtx = 0;\n        }\n        if (0 != ci->pFormatCtx) {\n            av_close_input_file(ci->pFormatCtx);\n            ci->pFormatCtx = 0;\n        }\n        ci->is_done = 0;\n    }\n    return 0;\n}', 'void av_close_input_file(AVFormatContext *s)\n{\n    ByteIOContext *pb = s->iformat->flags & AVFMT_NOFILE ? NULL : s->pb;\n    av_close_input_stream(s);\n    if (pb)\n        url_fclose(pb);\n}', 'void av_close_input_stream(AVFormatContext *s)\n{\n    int i;\n    AVStream *st;\n    if (s->cur_st && s->cur_st->parser)\n        av_free_packet(&s->cur_pkt);\n    if (s->iformat->read_close)\n        s->iformat->read_close(s);\n    for(i=0;i<s->nb_streams;i++) {\n        st = s->streams[i];\n        if (st->parser) {\n            av_parser_close(st->parser);\n        }\n        av_free(st->index_entries);\n        av_free(st->codec->extradata);\n        av_free(st->codec);\n        av_free(st->filename);\n        av_free(st);\n    }\n    for(i=s->nb_programs-1; i>=0; i--) {\n        av_freep(&s->programs[i]->provider_name);\n        av_freep(&s->programs[i]->name);\n        av_freep(&s->programs[i]->stream_index);\n        av_freep(&s->programs[i]);\n    }\n    flush_packet_queue(s);\n    av_freep(&s->priv_data);\n    av_free(s);\n}']